#!/bin/sh
# Generic installation script for Tadpole Patches
# clean option wipes this directory
#
# install system
# install kvm [optional kvm root path]
# install clean

BASEDIR=`pwd`
TREE=$BASEDIR/tree
ROOTPATH=$2
if [ "$1" = "kvm" ] ; then
    if [ "$ROOTPATH" = "" ] ; then
	ROOTPATH=/usr/kvm
    else
	ROOTPATH=$ROOTPATH/kvm
    fi
fi

# ------------------------------------------------------------
# function to do a rigorous copy
# Args in: Source file path, Target file path
do_rigorous_cp () {
OWNER=`ls -lg $1 | awk '{print $3}'`
GROUP=`ls -lg $1 | awk '{print $4}'`
cp -p $1 $2
chown $OWNER $2
chgrp $GROUP $2
}
# ------------------------------------------------------------
#

# Start here

# ------------------------------------------------------------
# Test that basic conditions for installation are valid
# We should be running from the "patch install" directory - this 
# MUST contain a patch directory.
#
if [ ! -d $BASEDIR/patch ]
then
    echo "Install script must be run from the installation directory"
    exit 1
fi
if [ ! -w /etc/passwd ]
then
    echo "Must be root to install patches"
    exit 1
fi

# ------------------------------------------------------------
# Uncompress any files that have been compressed 
# Check for presence of compression binary, can be uncompress or gzip
# if uncompress then files should have a .Z suffix
# if gzip then files should have a .gz suffix

if [ -f $BASEDIR/gzip ]
then
    UNCOMPRESS="$BASEDIR/gzip -d"
else
    if [ -f $BASEDIR/compress ]
    then
        UNCOMPRESS="$BASEDIR/compress"
    else
        UNCOMPRESS="compress"
    fi
fi

echo '---------- Checking Install Tree'
for i in `find . \( -name "*.Z" -o -name "*.gz" \) -print`
do
    $UNCOMPRESS $i
done

# Double Check to make sure no uncompressed files left
for i in `find . \( -name "*.Z" -o -name "*.gz" \) -print`
do
    echo "<$i> is still uncompressed - please check"
done




# ------------------------------------------------------------
# Find out which tree to install.
#
arch=`uname -m`
if [ $arch = "tp_s1" -o $arch = "tadpole_s1" ]
then
    SYSTEM=SPARCbook1
elif [ $arch = "tp_s2" -o $arch = "tadpole_s2" ]
then
    SYSTEM=SPARCbook2
elif [ $arch = "sun4m" ]
then
    SYSTEM=SPARCbook3
else
    echo 'unknown machine type $arch'
    exit 0
fi
	
if [ -f SPARCbook.OS.list -o -f SPARCbook.kvm.list ]
then
  SYSTEM=SPARCbook
fi

# ------------------------------------------------------------
# Copy across files from the installation tree
#
PATCH_DIR="/etc/install/tadpole_patches"
PATCH=`basename $BASEDIR`
if [ ! -f patch/Version ]
then
    echo "Cannot locate version number for this patch"
    exit 0
fi
read VERSION < patch/Version

case "$1"
in
    "system")
	LISTFILE=$SYSTEM.OS.list
	if [ ! -f $LISTFILE ]
	then
	    echo 'This patch contains no system upgrade for '$SYSTEM''
	    echo 'Please refer to your patch documentation.'
	    exit 0
	fi
	;;

    "kvm")
	LISTFILE=$SYSTEM.kvm.list
	if [ ! -f $LISTFILE ]
	then
	    echo 'This patch contains no kernel upgrade for '$SYSTEM''
	    echo 'Please refer to your patch documentation.'
	    exit 0
	fi
	;;

    "clean")
	rm -rf tree patch
	rm -f *install *list
	exit 0
	;;

    *)
	echo 'Usage: '$0' system | kvm | clean'
	exit 1
	;;
esac

if [ "$1" = "system" ]
then
    if [ -x preinstall ]
    then
	echo '---------- Running Preinstall Script'
	./preinstall $ROOTPATH
    fi
fi

# ----------------------------------------
# Install from the object tree, using the
# list of files obtained from LISTFILE
#
echo '---------- Installing'
echo "    Saving original files as <filename>.$$"
while read FILE RENAME
do
    case ${FILE} in
	'#'* | '')
	    continue;;
    esac

    if [ "$RENAME" = "" ] ; then
	RENAME=$FILE
    fi
    DIR=`dirname $ROOTPATH/$RENAME`
    if [ ! -d $DIR ] ; then
	mkdir -p $DIR
    fi

    if [ ! -f $TREE/$FILE ] ; then
	echo "    *** Cannot locate $FILE"
	echo "    *** Please contact your supplier for assistance"
	exit 1
    fi
    BASENAME=`basename $RENAME`
    if [ -f $ROOTPATH/$RENAME ] ; then
	mv $ROOTPATH/$RENAME $ROOTPATH/$RENAME.$$
    fi
    echo "    Installing $BASENAME"
    do_rigorous_cp $TREE/$FILE $ROOTPATH/$RENAME
done < $LISTFILE

# ----------------------------------------
# Run PostInstallation script
#
if [ "$1" = "system" ]
then
    if [ -x postinstall ]
    then
	echo '---------- Running Postinstall Script'
	./postinstall $ROOTPATH
    fi
fi

if [ ! -d $PATCH_DIR ] 
then
    mkdir -p $PATCH_DIR
fi

if [ ! -f $PATCH_DIR/$PATCH.$VERSION ]
then
    do_rigorous_cp patch/Info $PATCH_DIR/$PATCH.$VERSION
fi
echo "-----" >> $PATCH_DIR/$PATCH.$VERSION
echo "Installed $1 "`date` >> $PATCH_DIR/$PATCH.$VERSION
echo "Installed by process #$$" >> $PATCH_DIR/$PATCH.$VERSION

echo '---------- Installation Complete'
