#!/bin/sh

# $PLUGIN_NAME must match PKGNAME in the file "rev"
PLUGIN_NAME=tools
PLUGIN_VERSION=010000

PLUGIN_FILE=${PLUGIN_NAME}_${PLUGIN_VERSION}.ppg

PLUGIN_DIR=plugin
PKG_DIR=${PLUGIN_NAME}
TOP_DIR=${PLUGIN_DIR}/${PKG_DIR}
TREE_DIR=$TOP_DIR/TOOLS
SBIN_DIR=$TREE_DIR/sbin

# These are (at the time of writing) the locations of the most current
# source packages of these utilities.  To drop any of them (I suggest
# that you at least build the first two which are libraries) just
# delete or comment out its definition line.  To add a new package,
# you can add a new XYZ_SRC variable, add it to the build list, and make
# an XYZ_doit.sh script to this directory.

NCURSES_SRC="http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.7.tar.gz"
PCRE_SRC="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz"
BASH_SRC="http://ftp.gnu.org/gnu/bash/bash-4.0.tar.gz"
FILE_SRC="ftp://ftp.astron.com/pub/file/file-5.00.tar.gz"
GAWK_SRC="ftp://mirrors.kernel.org/gnu/gawk/gawk-3.1.6.tar.gz"
GREP_SRC="ftp://mirrors.kernel.org/gnu/grep/grep-2.5.4.tar.gz"
LESS_SRC="http://www.greenwoodsoftware.com/less/less-429.tar.gz"
SED_SRC="ftp://mirrors.kernel.org/gnu/sed/sed-4.1.5.tar.gz"
TCSH_SRC="ftp://ftp.astron.com/pub/tcsh/tcsh-6.16.00.tar.gz"
WGET_SRC="http://ftp.gnu.org/gnu/wget/wget-1.11.4.tar.gz"
WHICH_SRC="ftp://mirrors.kernel.org/gnu/which/which-2.20.tar.gz"

BUILD_LIST="${NCURSES_SRC-} ${PCRE_SRC-} ${BASH_SRC-} \
            ${FILE_SRC-} ${GAWK_SRC-} ${GREP_SRC-}    \
            ${LESS_SRC-} ${SED_SRC-} ${TCSH_SRC-}     \
            ${WGET_SRC-} ${WHICH_SRC-}"

if test "${1+set}" = set; then
    if test $1 = clean; then
        \rm .*.done > /dev/null 2>&1
        \rm -rf $PLUGIN_DIR > /dev/null 2>&1
        \rm $PLUGIN_FILE > /dev/null 2>&1
        exit 0
    fi
fi

# Build the destination directory
mkdir -p $SBIN_DIR

for PROJECT_SOURCE in $BUILD_LIST ; do
    # Extract the archive file name by stripping the URL off
    PROJECT_FILE=${PROJECT_SOURCE/#*\/}
    # Exract the directory name by stripping off the archive extension
    PROJECT_DIR=${PROJECT_FILE/%.tar.gz/}
    # Extract the project name by stripping off version info
    PROJECT_NAME=${PROJECT_DIR/-[.0-9]*/}
    # Generate the name of the config & make script for project
    PROJECT_DOIT=${PROJECT_NAME}_doit.sh
    # Generate at sentinel name to check if project completed
    SENTINEL=.${PROJECT_DIR}.done

    if test ! -f ${SENTINEL}; then

        echo
        echo "************************************************************************"
        echo "************************* Building $PROJECT_DIR ************************"
        echo "************************************************************************"
        (test -f $PROJECT_FILE ) || wget $PROJECT_SOURCE    || {(echo "COULD NOT FIND SOURCE FOR $PROJECT_DIR" ; exit 1;); exit 1;}
        (test -d $PROJECT_DIR )  || tar -xvzf $PROJECT_FILE || {(echo "COULD NOT EXTRACT $PROJECT_DIR" ; exit 1;); exit 1;}
        cp $PROJECT_DOIT $PROJECT_DIR/doit.sh
        if test ${PROJECT_NAME} = tcsh; then
            cp tcsh_ns4300.defs $PROJECT_DIR/ns4300.defs
        fi

        cd $PROJECT_DIR
        ./doit.sh || {(echo "BUILD FAILED" ; exit 1;); exit 1;}
        cd ..
        \rm -rf $PROJECT_DIR
#       \rm $PROJECT_FILE
        touch ${SENTINEL}
        echo "####################### Built $PROJECT_DIR Successfully ##################"
    fi
done

echo
echo "####################### Built All Projects Successfully ##################"

cp rev $PLUGIN_DIR
cp upgrade_script $TOP_DIR
chmod 755 $TOP_DIR/upgrade_script
cp plugin.conf $TREE_DIR
chmod 755 $TREE_DIR/plugin.conf
cp tools_start $SBIN_DIR
chmod 755 $SBIN_DIR/tools_start

sudo chown -R root:root $TOP_DIR $PLUGIN_DIR/rev
tar -czf ${PLUGIN_NAME}.tar.gz -C $PLUGIN_DIR $PKG_DIR rev
sudo chown -R $USER:$GROUP $TOP_DIR $PLUGIN_DIR/rev
dd if=/dev/zero of=$PLUGIN_FILE bs=1k count=97
cat ${PLUGIN_NAME}.tar.gz >> $PLUGIN_FILE
\rm ${PLUGIN_NAME}.tar.gz

