#!/bin/bash

curdir=$(pwd)
basedir=$(cd $(dirname $0) ; pwd)
basename=$(basename $0)
OS_ARCH=$(uname -p)
if [ "$OS_ARCH" == "athlon" ] ; then
    OS_ARCH=i686
fi
OS=$(uname)
DD=$(date +%Y%m%d.%H%M%S)
linklistver=1.2.0.00

if [ $OS == SunOS ] ; then
    AWK=nawk
else
    AWK=awk
fi
for arg in $* ; do
    if [ $arg = overwrite ] ; then
        overwrite=Y
    fi
done
if [ -f "param_bash.ini" ] ; then
    . ./param_bash.ini
elif [ -f ./ikit.properties ] ; then
    . ./ikit.properties
    if [ -f ./global.properties ] ; then
        . ./global.properties
    fi
fi
PRODUCT_DIR=/exlibris/product
FTP_DIR=${IKIT_FTP_DIR:-$curdir}
log=${ikit_log:-$curdir/log/${basename%.*}.log}
LOGDIR=$(dirname $log)
if [ ! -d $LOGDIR ] ; then
    mkdir -p $LOGDIR || exit
fi
exl_prod="$1"
productfile=${2:-$FTP_DIR/linklist}
perlmodfilename="$FTP_DIR/perlmod.$version.list"
RC=0



LOG() {
    echo "`date` : $*" >> $log
    if [ "$DEBUG" = "Y" ] ; then
        echo "DEBUG: $*"
    fi
}

ERROR() {
    unset DEBUG
    echo "ERROR: $*"
    LOG  "ERROR: $*"
    RC=1
}

EXIT() {
    ERROR $*
    exit 1
}


INFO() {
    DBSTAT="$DEBUG"
    DEBUG=""
    if [  "$SILENT" != "Y" ] ; then
        echo "INFO:  $*"
    fi
    LOG  "INFO:  $*"
    DEBUG=$DBSTAT
}

LOG "$0 started"

for TAR in /exlibris/product/bin/tar /exlibris/product/util/bin/tar /usr/sfw/bin/gtar /opt/sfw/bin/gtar /opt/freeware/bin/tar /usr/bin/gtar $basedir/bin/tar.`uname`.`uname -p` /bin/tar null
do
    if [ -x $TAR ] ; then
        break
    fi
done
if [ $TAR = null ] ; then
    EXIT "No tar utility"
fi 
if [ "`$TAR --version | grep GNU`" = "" ] ; then
    EXIT "Could not find GNU tar"
fi
LOG "TAR is $TAR"
#test -w $FTP_DIR  || ERROR "No write permissions on $FTP_DIR"
#LOG "Write permissions on $FTP_DIR OK"
if [ -d $PRODUCT_DIR ] ; then
    test -w $PRODUCT_DIR || EXIT "No write permissions on $PRODUCT_DIR"
    LOG "Write permissions on $PRODUCT_DIR OK"
else
    mkdir -p $PRODUCT_DIR/local $PRODUCT_DIR/bin 2>> $log || EXIT "Could not create $PRODUCT_DIR"
    LOG "$PRODUCT_DIR created"
fi

if [ -e $productfile ] ; then
    LOG "found $productfile"
else
    EXIT "Product file list not found at $productfile"
fi
prodlist=$(egrep  "$exl_prod|all" $productfile | grep $OS_ARCH | grep -v '^#' |  $AWK -F"[,/]" '!x[$1]++  {print $1}')

cd $FTP_DIR
for perlmodfilename in perlmod.*.list ; do
    LOG "Found $perlmodfilename"
    version=$(echo $perlmodfilename | sed 's/perlmod\.\(.*\)\.list/\1/')
    perl_base=$PRODUCT_DIR/perl-$version
    PERL=$perl_base/bin/perl
    LOG "Perl base is $perl_base"
    if [ ! -d $perl_base ] ; then
        LOG "$perl_base does not exist here"
        continue
    fi
    eval $($PERL -V:archname)
    for perlmod in $(cat $perlmodfilename) ; do
        perlmodfile=$perlmod.$version.$archname.tgz
        if [ ! -e $perlmodfile ] ; then
            ERROR "Perl module $perlmod not found for $archname"
            continue
        fi
        INFO "Extracting $perlmodfile..."
        $TAR zxf $perlmodfile -C $perl_base
        if [ $? -gt 1 ] ; then
            ERROR "$perlmod extraction failed"
            continue
        fi
    done
done

for product in $prodlist ; do
    if [ -d $PRODUCT_DIR/$product ] ; then
        INFO "$PRODUCT_DIR/$product exists"
        if [ -z $overwrite ] ; then
            LOG "$product Skipped"
            continue
        else
            INFO "$product will be overwritten"
        fi
    fi

    for prefix in $product.$OS.$OS_ARCH $product.$OS $product NONE; do
        if [ $prefix == NONE ] ; then
            ERROR "No package found for $product"
        fi
        if [ -f $prefix.tar.gz ] ; then
            filename="$prefix.tar.gz"
            INFO "Extracting $product..."
            $TAR zxf  $filename -C $PRODUCT_DIR
            if [ $? -gt 1 ] ; then
                ERROR "$product extraction failed"
            fi
            break
        fi
    done
done

if [ -e $FTP_DIR/linklist ] ; then
    if [[ -e $PRODUCT_DIR/linklist ]] ; then
        eval $(grep ^#linklistver $PRODUCT_DIR/linklist | sed 's/#//')
    fi
    lastver=$linklistver
    LOG "Existing product version: $linklistver"
    line=$(grep ^#linklistver $FTP_DIR/linklist 2>/dev/null | sed 's/#//')
    if [ ! -z $line ] ; then
        eval $line
    fi
    if [[ $linklistver > $lastver  ]] ; then
        if [[ -e $PRODUCT_DIR/linklist ]] ; then
            LOG "Replacing linklist version $lastver with $linklistver"
            cp -p $PRODUCT_DIR/linklist $PRODUCT_DIR/linklist.$lastver
        fi
        cp -p $FTP_DIR/linklist $PRODUCT_DIR/linklist
        if (( $? )) ; then
            ERROR "Failed to update $PRODUCT_DIR/linklist"
        else
            INFO "$PRODUCT_DIR/linklist updated"
        fi
    else
        LOG "Existing linklist version $lastver is equal or newer than $linklistver"
    fi
else
    ERROR "$FTP_DIR/linklist not found"
fi


LOG "Fixing product permissions in the background"
nohup chmod -R g=u $PRODUCT_DIR 2>>$log &
if [ -w /etc/passwd ] ; then
    nohup chown -Rh ${exl_prod}:exlibris $PRODUCT_DIR 2>>$log &
fi
INFO "Third party product installation done"
if (( $RC )) ; then
    INFO "Some errors occured during the installation please check logs for more info"
fi
exit $RC
