#!/bin/bash

LANG=C

function extract_defines()
{
  sed -e '/DEFINES       =/!d' \
      -e 's/DEFINES       = /QT_DEFINES += /' Makefile > defines.inc

  echo -e "" >> defines.inc
}

function extract_incpath()
{
  sed -e '/INCPATH       =/!d' \
      -e 's/INCPATH       = //' \
      -e 's/ / \\\n/g' \
      Makefile > incpath.inc.tmp

  echo -e "QT_INCPATH += \\" > incpath.inc

  sed -e '/\/qt-everywhere-opensource-src.*\//!d' \
      -e 's/.*\/qt-everywhere-opensource-src-[^\/]*\//  /' \
      -e 's/linux-g++/genode-g++/' \
      incpath.inc.tmp >> incpath.inc

  echo -e "" >> incpath.inc
}


function extract_sources()
{
  sed \
      -e ':a;N;$!ba;s/\n//g' \
      -e 's/OBJECTS .*//' \
      -e 's/.*SOURCES       =/QT_SOURCES += \\/' \
      -e 's/\\/\\\n/g' \
      -e 's/\.cpp \.moc/\.cpp \\\n\t\t\.moc/g' \
      -e 's/\.cpp \.rcc/\.cpp \\\n\t\t\.rcc/g' \
      -e 's/\.cc generated/\.cc \\\n\t\tgenerated/g' \
      Makefile > sources.inc.tmp

  sed -e 's/.*\//  /' \
      sources.inc.tmp > sources.inc

  echo -e "" >> sources.inc
}


function extract_vpath()
{
  sed \
      -e ':a;N;$!ba;s/\n//g' \
      -e 's/OBJECTS .*//' \
      -e 's/.*SOURCES       =//' \
      -e 's/\\/\\\n/g' \
      -e 's/\.cpp \.moc/\.cpp \\\n\t\t\.moc/' \
      -e 's/\.cpp \.rcc/\.cpp \\\n\t\t\.rcc/' \
      -e 's/\.cc generated/\.cc \\\n\t\tgenerated/g' \
      Makefile > vpath.inc.tmp

  echo -e "QT_VPATH += \\" > vpath.inc

  sed -e '/\/qt-everywhere-opensource-src.*\//!d' \
      -e 's/.*\/qt-everywhere-opensource-src-[^\/]*\//  /' \
      -e 's/\/[^\/]* [\\]*$/ \\/' \
      vpath.inc.tmp | sort -u >> vpath.inc

  echo -e "" >> vpath.inc
}


function extract_compiler_moc_header_make_all()
{
  sed -e '/compiler_moc_header_make_all:/!d' \
      -e 's/compiler_moc_header_make_all:/COMPILER_MOC_HEADER_MAKE_ALL_FILES = \\\n /' \
      -e 's/\.moc\/release-static-emb-x86_64\///g' \
      -e 's/\.cpp /\.cpp \\\n  /g' \
      Makefile > compiler_moc_header_make_all.inc.tmp

  sed -e 's/.*\//  /' \
      compiler_moc_header_make_all.inc.tmp > compiler_moc_header_make_all.inc

  echo -e "" >> compiler_moc_header_make_all.inc
}


function extract_compiler_moc_source_make_all()
{
  sed -e '/compiler_moc_source_make_all:/!d' \
      -e 's/compiler_moc_source_make_all:/COMPILER_MOC_SOURCE_MAKE_ALL_FILES = \\\n /' \
      -e 's/\.moc\/release-static-emb-x86_64\///g' \
      -e 's/\.moc /\.moc \\\n  /g' \
      Makefile > compiler_moc_source_make_all.inc.tmp

  sed -e 's/.*\//  /' \
      compiler_moc_source_make_all.inc.tmp > compiler_moc_source_make_all.inc

  echo -e "" >> compiler_moc_source_make_all.inc
}


extract_defines
extract_incpath
extract_sources
extract_vpath
extract_compiler_moc_header_make_all
extract_compiler_moc_source_make_all

cat defines.inc > $1_generated.inc

cat incpath.inc >> $1_generated.inc

cat sources.inc >> $1_generated.inc

cat vpath.inc >> $1_generated.inc

echo -e "# some source files need to be generated by moc from other source/header files before" >> $1_generated.inc
echo -e "# they get #included again by the original source file in the compiling stage\n" >> $1_generated.inc

echo -e "# source files generated from existing header files ('moc_%.cpp: %.h' rule in import-qt5.inc)" >> $1_generated.inc
echo -e "# extracted from 'compiler_moc_header_make_all' target\n" >> $1_generated.inc

cat compiler_moc_header_make_all.inc >> $1_generated.inc

echo -e "# source files generated from existing source files ('%.moc: %.cpp' rule in import-qt5.inc)" >> $1_generated.inc
echo -e "# extracted from 'compiler_moc_source_make_all' rule\n" >> $1_generated.inc

cat compiler_moc_source_make_all.inc >> $1_generated.inc
