#!/bin/bash

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

. buildscripts/try

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

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

    Before calling this script './install lib'  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 './build' are located under ./tmp
    Final files are installed under ${ROOT}, but may be stored elsewhere
    by 'install'
"


# build icmake and its support programs

    for target in spch multicmp comp dep exec pp un icmake icmbuild 
    do
        cd $target
        ./build || exit 1
        cd ..
    done

    # 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 the man pages in doc/ to tmp/usr/share/man/

    cp doc/*.1 tmp/usr/share/man/man1
    cp doc/*.7 tmp/usr/share/man/man7
 
 # 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 "
Next: call ./install all ***AS ROOT***
"
