#!/bin/csh -f
# check if download succeeded - cksums
set basedir=`dirname $0`
cd $basedir
# determine which Ex Libris application
source util_sp_set_app


if (! -e $sp_conf) then
    echo "initial setup missing - run configuration utility"
    exit 1
else
    source $util_proc/util_sp_get_conf
endif

if (! -d $sp_dir) then
   echo "Download dir not detected"
   exit 1
endif

cd $sp_dir
set platform="`uname`"
set OS=`uname`-`uname -p`
if (! $?COBDIR) then
	set COBDIR
endif
if (-e $COBDIR/cobver) then
    set cobtype=old
else
    set cobtype=new
endif
if ($platform == SunOS) then
    set platform="$platform-$cobtype"
endif
if (-e ftp.pid) then
    ps -fp `cat ftp.pid` >& /dev/null
    if ($status) then
        rm ftp.pid
        echo "Removing non relevant pid file"
    else
        echo "FTP still running"
	grep  "gz_" ftp.in >& /dev/null
	if (! $status) then
		set totalfiles=`cat ftp.in | grep gz_ | wc -l`
		@ dlfiles=`ls -1 | grep '.'gz_ | wc -l` - 1
		if ($dlfiles < 0) then
			@ dlfiles = 0
		endif
		@ percent = ( $dlfiles  * 100 ) / $totalfiles
		echo "$dlfiles file(s)  of total $totalfiles completed ($percent %) "
        endif
        exit 1
    endif
endif
if (-e $sp_util) then
    echo "installation utility detected" >> sp.log
   if ($bin_sp) then
        foreach platform ($OS-$cobtype `uname`-$cobtype $OS `uname`)
            if (-e sp.bin.$platform.tar) then
                echo "Binary SP file found for $platform"
                break
            endif
        end
        if (! -e sp.bin.$platform.tar) then
            echo "Binary installation file missing"
            exit 1
        endif
   endif
   echo "Download successful"
    ./$sp_util -h < /dev/null
   exit 0
endif


if ( -z ftp.list && -e ftp1.log ) then
	echo "FTP  problems detected - check FTP user/password configuration"
	exit 1
endif
(cat sp.split.list | sed 's/.gz_.*//' | sort -u > sp.ver) >& /dev/null
if (-z sp.ver) then
    echo "No download files detected"
    exit 1
endif
if (`wc -l sp.ver | awk '{print $1}'` != 1) then
    echo "Multiple SP versions found - cannot continue"
    exit 1
endif

if (-e tar.pid) then
    /bin/kill -0 `cat tar.pid` >& /dev/null
    if ($status) then
        rm tar.pid # forgotten file
    else
        echo "The SP download is in process. Please retry in a few minutes."
        exit 1
    endif
endif
echo "Extracting SP package..."
echo $$ > tar.pid
(cat sp.split.list | xargs cat | tar zxv) >& sp.extract.log
if ($status > 2 || ! -f $sp_util ) then
    rm tar.pid
    echo "Error extracting SP files:"
    echo "Some files were not extracted or may be corrupt"
    echo "Please check sp.extract.log"
    rm $sp_util >& /dev/null
    exit 1
endif
rm tar.pid

if ($bin_sp) then
	cat sp.bin.list | sed 's/gz_.*//' | sort -u > sp.bin.ver
	if ($status) then
	    echo "SP binary files not downloaded"
	    exit 1
	endif
	if (`wc -l sp.bin.ver | awk '{print $1}'` != 1) then
	    echo "Multiple binary versions found - cannot continue"
	    exit 1
	endif
	(cat sp.bin.list | xargs cat | tar zxv) >>&  sp.extract.log
        if ($status > 2) then
            echo "Errors extracting binary SP files"
            echo "SP integrity suspiceous"
	    exit 1
        endif
endif

echo "SP download successfull"
rm sp.log.* >& /dev/null
./$sp_util -h < /dev/null

rm  ${app}*.gz_* >& /dev/null
exit 0


