#!/bin/sh
#
# Quick-n-dirty install script for custom R/3 patch deliveries
# (07-Mar-97) Martin Rex,  SAP AG Walldorf
#
#  DEC/OSF1,  Oracle,  R/3-30Fp1
#

patch="30Fp1"
archives="R3trans.CAR dw-${patch}.CAR"

newfiles="\"R3trans\" \"disp+work gwrd gwwp msg_server rsyn.bin\""

if [ "`uname`" != "OSF1" ] ; then
	echo "Sorry, this patch(-script) is for DEC Unix (OSF1) only."
	exit 1
fi

###########################
# Where are the new files ?
#
srcdir="$1"
sid="$2"
ok=0
origdir="`pwd`"
try="$origdir"
if [ ! -f "$try/dw-${patch}.CAR" ] ; then
    try=""
fi

while [ "$ok" = 0 ] ; do 

    while [ "$srcdir" = "" ] ; do
	echo "Please enter the name of the directory where you have copied"
        echo "the new files ($archives)"
	echo -n "   [${try}]?"
	read srcdir
	if [ "$srcdir" = "" -a "$try" != "" ] ; then
	    srcdir="$try"
	fi
	echo
    done

    xxdir="$srcdir"
    for file in $archives ; do
	fullname="$xxdir/$file"
	echo -n "  $fullname \t... "
	if [ -f "$fullname" ] ; then
	    echo "ok"
	else
	    echo "sorry, I can't find it!"
	    srcdir=""
	fi
    done
    if [ "$srcdir" != "" ] ; then ok=1 ; fi
done

echo "Ok, I can see the new files" ; echo

#########################
#  Find and check the destination path?
#
ok="0"
missing="0"
while [ "$ok" = 0 ] ; do 

    while [ "$sid" = "" ] ; do
	echo -n "Please enter the SAP R/3 System-ID  []?"
	read sid
    done
    SID="`echo $sid | /usr/bin/tr 'a-z' 'A-Z'`"
    sid="`echo $SID | /usr/bin/tr 'A-Z' 'a-z'`"
    if [ "`ps -ef|grep sap${SID}|grep -v grep|wc -l`" -gt 0 ] ; then
	ps -ef | grep sap${SID} | grep -v grep
	echo "Please shut down the R/3-System (${SID}) before installing the patch"
	exit 1
    fi

    me="`whoami`"
    if [ "${me}" != "${sid}adm" ] ; then
	echo
	echo "You are user \"${me}\","
	echo "but you should definitely be \"${sid}adm\" to install the patch"
	echo -n "Are you sure you want to continue (y/n) ? "
	read answer
	case "$answer" in
	   y|Y|yes|YES|Yes)	:
				;;
	   *)			echo "ok -- aborting."
				exit 1;
	esac
    fi

    dstdir="/usr/sap/${SID}/SYS/exe/run"

    echo "Changing current directory to \"$dstdir/\""
    cd "$dstdir"
    if [ "$?" != 0 ] ; then
	echo "HUH??! Error when trying \"cd $destdir\""
	echo
	exit 1
    fi

    patchlock="installed-${patch}"
    if [ -f "$patchlock" ] ; then
	echo
	echo "Ooops, the file \"$dstdir/$patchlock\" exists!"
	echo "Looks like the patch was already installed -- aborting"
	echo
	exit 1
    fi

    echo "Searching for original files ... "
    for set in $newfiles ; do
	eval xset=$set
	for file in $xset ; do 
	    fullname="$dstdir/$file"
	    echo -n "  $fullname \t... "
	    if [ -f "$fullname" ] ; then
		echo "ok"
	    else
		if [ "$file" = "R3INST" ] ; then
		    echo "not found -- still ok"
		else
		    echo "not found -- fatal!"
		    missing=1
		fi
	    fi
	done
    done
    if [ "$missing" = "0" ] ; then
	ok=1
    else
	sid=""
    fi
done

echo "Ok, I can see the original files.\n"

##################
#  Saving old executables
#  
backupdir="../before-${patch}"


if [ -d "$backupdir" ] ; then
    ok=1
    echo "Backup directory $backupdir already exist."

    for set in $newfiles ; do
	eval xset=$set
	for file in $xset ; do 
	    fullname="$backupdir/$file"
	    if [ -f "$fullname" ] ; then
		echo "file $fullname already present??"
		ok=0
	    fi
        done
    done

    if [ "$ok" = 0 ] ; then
	echo
	echo "Some files are already present in the backup directory"
        echo "I am confused -- aborting!"
	echo
	exit 1
    fi
else
    echo "Creating a backup directory: $backupdir"

    cmd="mkdir $backupdir"
    $cmd
    if [ "$?" != 0 ] ; then
	echo "$cmd failed -- aborting!"
	exit 1
    fi

fi


############################
#  Move old binaries into backup directory
#
ok=1
reverse=""
umask 022
undo="$backupdir/undo.sh"

/bin/echo "#!/bin/sh"   >"${undo}"
if [ "$?" != 0 ] ; then
    echo "Failed to create undo script \"$undo\""
    echo "Aborting!"
    echo
    exit 1
fi
chmod +x "${undo}"
echo "Creating shell-script $undo to undo the changes if this script breaks"
echo "#"            >>"${undo}"
echo "set -x"       >>"${undo}"
echo "cd $dstdir"   >>"${undo}"

 
for set in $newfiles ; do
    eval xset=$set
    for file in $xset ; do 
	fullsrc="$file"
	fulldst="$backupdir/$file"
	if [ "$file" = R3INST -a ! -f "$fullsrc" ] ; then
	    echo "   $fullsrc not present, still ok"
	else
	    cmd="mv $fullsrc $fulldst"
	    echo -n "$cmd \t ... "
	    $cmd
	    if [ "$?" = 0 ] ; then
	        echo "ok"
		rcmd="mv -f $fulldst $fullsrc"
	        reverse="$reverse '${rcmd}'"
	        echo "${rcmd}" >>"${undo}"
	    else
	        echo "failed!"
	        ok=0
	        break
	    fi
	fi
    done
    if [ "$ok" = 0 ] ; then break; fi
done

#############################
#  if moving the original files into the backup directory
#  failed, then try to put the files that were move back into place
#  (undo our work)
#

if [ "$ok" = 0 ] ; then
    echo "There was a problem, trying to restore original state"
    revok=1
    for cmd in $reverse ; do
	eval xcmd=$cmd
	echo -n "$cmd \t ... "
	$cmd
	if [ "$?" = 0 ] ; then
	    echo "ok"
	else
	    echo "failed -- uh-oh!!"
	    revok=0
	fi
    done
    if [ "$revok" != 1 ] ; then
	echo "This install-script failed to undo its work"
        echo "I'm sorry, you might have to manually check what went wrong"
	exit 1
    fi

    echo "The patch has not been installed because of problems"
    exit 1
fi


##################################
#  the old files are now in the backup directory,
#  now start unwrapping the archives
#

ok=1

for car in $archives ; do
    carfile="$srcdir/$car"
    cmd="CAR -vxf $carfile"
    echo "$cmd"
    $cmd
    if [ "$?" != 0 ] ; then
	echo "$cmd FAILED !"
	ok=0
	break
    fi
done

if [ "$ok" = 0 ] ; then
    echo
    echo "Uh-oh!"
    echo "Installation of Patch failed"
    echo "trying to undo the previous work"
    sh "${undo}"
    rm -f "${patchlock}"
    mv "${undo}" "${undo}.undone.deleteme"
    exit 1
fi

touch "$patchlock"
if [ "$?" != 0 ] ; then
    echo "OUCH! unable to create \"$dstdir/${patchlock}\" !"
fi

echo "rm -f $patchlock"                     >>"${undo}"
echo "mv ${undo} ${undo}.undone.deleteme"   >>"${undo}"
echo
echo "Patch ${patch} successfully installed."
echo
exit 0


