#!/bin/bash

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

. bootstrap/try

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

    Parser and scanner files generated by bisonc++ and flexc++ must already
    be available (which holds true for the standard distribution)

    If the manpages (.1, .7) are already available in the ./doc
    subdirectory then Yodl isn't used to build the man-pages.

    Before calling this script icm_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
"

# build icmake and its support programs

    for target in support spch multicmp comp dep exec pp \
        un icmake icmbuild 
    do
        cd $target
        ./icm_bootstrap || exit 1
        cd ..
    done
     # libsupport.a in ./tmp
     # icm_comp and other support binaries in tmp/usr/libexec/icmake
     # icmbuild in tmp/usr/bin/icmbuild
     # icmake in tmp/usr/bin/icmake
 
# install scripts to ./tmp
    echo "./scripts to ./tmp/{icmbuild.in,icmstart.in}
"
    # conversions of the .sh and .in files by icm_install
    cd scripts
    cp icmstart.sh ../tmp/icmstart.sh
    ./catim ib <icmbuild.in >../tmp/icmbuild.in
    ./catim is <icmstart.in >../tmp/icmstart.in
    cd ..

# cp the default skeleton files to tmp/usr/share/icmake
    echo "default skeleton files in usr/share/icmake/ to tmp/usr/share/icmake
"
    try cp -r usr/share/icmake/* tmp/usr/share/icmake/

# cp the config files in ./etc/icmake to tmp/etc/icmake
    echo "configuration files in etc/icmake to tmp/etc/icmake
"
    try cp -r etc/icmake/* tmp/etc/icmake/

# manpages: write doc/manpage/release.yo
    if [ -e doc/icmake.1 ] ; then
        cp doc/*.1 tmp/usr/share/man/man1
        cp doc/*.7 tmp/usr/share/man/man7
    else
        cd doc/manpage
        echo "
SUBST(_CurVers_)(${VERSION})
SUBST(_CurYrs_)(${YEARS})" > release.yo
 
    # manpages: "construct the uncompressed man-pages below tmp/usr/share/man/

        echo "construct the uncompressed man-pages in tmp/usr/share/man/man1
"
        MAN=../../tmp/usr/share/man/man1
        yodl2man --no-warnings -o ${MAN}/icmake.1 -r 20000 icmake.yo
        yodl2man --no-warnings -o ${MAN}/icmstart.1        icmstart.yo
        yodl2man --no-warnings -o ${MAN}/icmbuild.1        icmbuild.yo
        
        MAN=../../tmp/usr/share/man/man7
        
        yodl2man --no-warnings -o ${MAN}/icmstart.rc.7     icmstart.rc.yo
        yodl2man --no-warnings -o ${MAN}/icmscript.7       icmscript.yo
        yodl2man --no-warnings -o ${MAN}/icmconf.7         icmconf.yo
        
        rm release.yo
        cd ${BASE}
    fi
 
 # manpages: "compress the man-pages below tmp/usr/share/man/
     echo "compress man-pages .1 in tmp/usr/share/man/man1
 "
     cd tmp/usr/share/man/man1
     for x in *.1 ; do
         gzip -9cn $x > $x.gz || exit 1
     done
     rm -f *.1
     
     echo "compress man-pages .7 in tmp/usr/share/man/man7
     "
     cd ../man7
     for x in *.7 ; do
         gzip -9cn $x > $x.gz  || exit 1
     done
     rm -f *.7
 
     cd ${BASE}

# install the doc/icmake files
    echo "compress changelog in tmp/usr/share/doc/icmake
"
    gzip -9cn changelog > tmp/usr/share/doc/icmake/changelog.gz || exit 1
    
    echo "cp examples files to tmp/usr/share/doc/icmake
"
    try cp -r examples tmp/usr/share/doc/icmake
    
    echo "cp ./etc files to tmp/etc
"
    try cp -r etc  tmp/ 

echo Done.
