#!/bin/bash

# constructed files are all below ./tmp, in fixed locations, like
# ./tmp/usr/bin/  

# absolute (final) installation directories are specified in INSTALL.im and
# are prefixed by the path specified by this script's (optional) 2nd argument
# (not counting the optional initial 'strip' argument) for storage purposes
# only.

. ./buildscripts/try

usage()
{
    echo "
Usage: $0 [strip] all|progs|scripts|man|skel|doc|etc [storageDir]
"
    exit 1
}

# optionally strip the binaries
    if [ "$1" == "strip" ] ; then
        STRIP=strip
        shift
    fi

[ -e tmp/INSTALL.sh ] || usage  # no tmp/INSTALL.sh then probably 
                                # ./prepare wasn't called

. tmp/INSTALL.sh                # get the installation locations

. scripts/setstoragedir         # maybe set the storagedir

# the storageDir is unrelated to the root directory specified by tmp/ROOT
# but is used to store the resulting files in a separate directory like
# debian/icmake, which is used when constructing a Debian package
# show the actual root directory, and the storageDir if it differs from root

    if [ "${storageDir}" != "" ] ; then
        echo "    storageDir: ${storageDir}
"
        if [ ! -e ${storageDir} ] ; then
            mkdir -p ${storageDir}
        elif [ ! -d ${storageDir} ] ; then
            echo $storageDir must be a directory
            exit 1
        fi
    fi

tarInstall()
{
    try mkdir -p ${2}
    echo "  installing tmp/${1} in ${2}"
    (cd tmp/${1}; tar cf - .) | (cd ${2}; tar xf -)
    [ "$?" -eq "0" ] || exit 1
}

progs()
{
    # at this point the BINDIR etc. variables define absolute paths.
    # No need to prefix ${storagedir}

    echo programs:
    # maybe strip the binaries

    if [ "$STRIP" == "strip" ] ; then
        try strip tmp/usr/bin/icmake tmp/usr/bin/icmbuild \
                  tmp/usr/bin/icmodmap tmp/usr/libexec/icmake/*
        echo
    fi

    #  prepare the directories for the binaries
    try mkdir -p ${BINDIR} ${LIBDIR}

    #install the usr/bin  binaries
    tarInstall usr/bin ${BINDIR}

    # a possibly running icm-exec program must first be removed before
    # a new icm-exec program can be installed
    if [ -e ${LIBDIR}/icm-exec ] ; then
        try rm ${LIBDIR}/icm-exec 
    fi

    #install the libexec binaries
    tarInstall usr/libexec/icmake ${LIBDIR}
    echo
}

scripts()
{
    echo scripts:
    # prepare the directories used by scripts:
        try mkdir -p ${BINDIR} ${LIBDIR} ${DOCDIR} 

    # convert icmstart.sh to storageDir/usr/bin/icmstart
        try scripts/convert tmp/icmstart.sh ${BINDIR}/icmstart
        try chmod +x ${BINDIR}/icmstart

    # convert icmbuild.in to icmbuild
        try scripts/convert tmp/icmbuild.in ${LIBDIR}/icmbuild

    # convert icmstart.in to docdir/icmstart.im
    # compress the icmstart.im script in the documentation directory:
        try scripts/convert tmp/icmstart.in tmp/icmstart.im
        echo "    gzip -9cn tmp/icmstart.im > ${DOCDIR}/icmstart.im.gz"
        gzip -9cn tmp/icmstart.im > ${DOCDIR}/icmstart.im.gz || exit 1

    # compile icmstart.im to $storageDir$LIBDIR/icmstart.bim so icmstart can
    # immediately be used w/o having to compile it first.
        try tmp/usr/libexec//icmake/icm-pp tmp/icmstart.im tmp/icmstart.pim
        try tmp/usr/libexec/icmake/icm-comp tmp/icmstart.pim ${LIBDIR}/icmstart.bim

    echo
}


# argument: the directory to store, files are stored under $storageDir
#   (called below, e.g., to install the man-pages)

case $1 in
    (all)
        progs
        scripts
        echo "man, skeletons, docs, etc:"
        tarInstall usr/share/man        ${MANDIR}
        tarInstall usr/share/icmake     ${SKELDIR}
        tarInstall usr/share/doc/icmake ${DOCDIR}
        tarInstall etc/icmake           ${CONFDIR}
        echo
        echo "Done. Consider calling ./clean"
        echo
    ;;

    (progs)
        progs
    ;;

    (scripts)
        scripts
    ;;

    (man)
        tarInstall usr/share/man ${MANDIR}
    ;;

    (skel)
        tarInstall usr/share/icmake ${SKELDIR}
    ;;

    (doc)
        tarInstall usr/share/doc/icmake ${DOCDIR}
    ;;

    (etc)
        tarInstall etc/icmake ${CONFDIR}
    ;;

    (*)
        usage
    ;;
esac
