#!/bin/bash

BASE=`pwd`      # .../src/icmake

cd support

. ../buildscripts/flags
. ../buildscripts/try

# show usage if no arguments were used
    if [ "$#" == "0" ] ; then
        echo "
    Usage: buildlib <any argument>

    Before calling this script 'prepare' must have been called.
"                                     
        exit 0
    fi

# check for the completion of icm_prepare
    if [ ! -e ../tmp/INSTALL.sh ] ; then
        echo tmp/INSTALL.sh does not exist. Execute ./icm_prepare to create it
        exit 1
    fi

# define the directory names and the std. variables (AUTHOR etc.)
    . ../tmp/INSTALL.sh
        # now BINDIR, SKELDIR, .... etc have been defined
        # the @SKELDIR@ etc. names in icmbuild and icmstart are converted by
        # icm_install, calling scripts/convert

# load the root directory name
    . ../tmp/ROOT

echo "
    The final root directory is $ROOT, 
    Files constructed by icm_bootstrap are located under ./tmp
    Final files are installed under ${ROOT}, but may be stored elsewhere
    by icm_install
"

MAJOR=`echo ${VERSION} | sed 's/\..*//'`

processHeader()
{
    f_file=${1}
    dest=${2}
 
#    echo " appending ${f_file} to ${dest}"
 
    while read -r line
    do
                                                # found a .f file ?
        echo $line | grep '#include\s*".*.f"' > /dev/null   
    
                                                # if yes: cat the .f file
        if [ $? -eq 0 ] ; then                  # to tmp/hdrs/${f_file}
            file=`echo "$line" | sed 's/.*"\(.*\.f\).*/\1/'`
            processHeader ${file} ${dest}
        else                                    # no: copy the line itself
            echo "$line" >> ${dest}
        fi
    done < ${f_file}
}

prepareHeaders()
{
    ifs=$IFS
    IFS='
'
    for dir in `find ./ -mindepth 1 -maxdepth 1 -type d`
    do
        [ $dir == "./xerr" ] && continue

        cd $dir
 
        echo Preparing the bobcat/${dir} header
 
        IFS=''                              # construct the header file
        processHeader $dir ../../bobcat/${dir}  
 
        cd ..
    done
    IFS=$ifs
}

compile()
{
    echo Compiling in `pwd`

    [ "`find ./  -mindepth 1 -maxdepth 1 -type d -name ORG`" != "" ] && return

    count=0     # use o-file numbers to avoid name collisions

    for subdir in \ `find ./  -mindepth 1 -maxdepth 1 -type d |sort` ; do

        [ $subdir == "./xerr" ] && continue

        try cd $subdir

        srclist=`find -mindepth 1 -maxdepth 1 -type f -name '*.cc' \
                                  -exec basename '{}' ';' | sort`

       if [ "$srclist" != "" ]
       then
           for src in `find -mindepth 1 -maxdepth 1 -type f -name '*.cc' \
                                   -exec basename '{}' ';' | sort` ; do
               obj=../${count}${src%%.*}.o
              
               if [ $src -nt ${obj} ] ; then 
                    opts=" -o${obj} -isystem ${BASE}/tmp -c $src"
                    try ${CXX} "${CXXFLAGS} ${opts}"
                fi
          done
        fi

        try cd ..
        let count=$count+1          # next directory nr.
    done
}

    # in 'support/'
cp bobcat.tgz ../tmp/usr/share/icmake/
 
mkdir -p ../tmp/bobcat
mkdir -p ../tmp/build
cp -r xerr ../tmp/build
 
try cd ${BASE}/tmp/build
 
tar xzf ../../support/bobcat.tgz

prepareHeaders

tar xzf ../../support/support.tgz

compile

try ar rs ../libicmake.a *.o

echo "
Next: call './build all\'
"

