#!/bin/sh

#
# Make sure we have all the tools necessary to run.
#
NO_PRG="Could not find required program: "
for p in "gzip tar uname diff ldconfig lilo rpm"
do
    if ! which $p > /dev/null
    then
        echo "$NO_PRG $p"
        exit 1
    fi
done

if ! which mkinitrd > /dev/null && grep initrd /etc/lilo.conf > /dev/null
then
    echo "$NO_PRG mkinitrd"
    exit 1
fi

# We need to version 2.9 of libstdc++ installed.  On RedHat 6.0 there should
# be a link named /usr/lib/libstdc++-libc6.1-1.so.2.  On SUSE 6.1 there should
# be a link named /usr/lib/libstdc++.so.2.9
if [ ! -e /usr/lib/libstdc++-libc6.1-1.so.2 -a ! -e /usr/lib/libstdc++.so.2.9 ]
then
    echo "This package requires libstdc++ version 2.9 to be installed."
    echo "On RedHat 6.x install the libstdc++ rpm.  On SUSE 6.x install the"
    echo "libgpp rpm.  Then, run this install program again."
    exit 1
fi

# On SUSE we need to create a symbolic link to the 2.9 shared library
if [ -e /usr/lib/libstdc++.so.2.9 -a ! -e /usr/lib/libstdc++-libc6.1-1.so.2 ]
then
    cd /usr/lib
    ln -s libstdc++.so.2.9 libstdc++-libc6.1-1.so.2
fi

echo '----- Stopping any DPT daemons that may be loaded -----'
kill -SIGKILL `ps x | grep dptscom | head -1 | awk '{print $1}'` 2> /dev/null
kill -SIGKILL `ps x | grep dptcom | head -1 | awk '{print $1}'` 2> /dev/null
kill -SIGKILL `ps x | grep dpteng | head -1 | awk '{print $1}'` 2> /dev/null
sleep 1

I2OSOURCE=108patch.gz

echo '----- Copying the necessary files for DPT I2O -----'
if [ "`echo $0 | cut -c1`" = "." ]
then
    FILES_DIR=`pwd`
else
    if echo $0 | grep "/"
    then
        FILES_DIR=`echo $0 | sed "s/\/install//g"`
    else
        FILES_DIR=`pwd`
    fi
fi
cd /tmp
gzip -cd $FILES_DIR/files.tgz | tar -Ppxvf - > /dev/null

# Create a symbolic link for the DPT pixmap if pixmaps directory exists
if [ -d /usr/X11/include/X11/pixmaps ]
then
    cd /usr/X11/include/X11/pixmaps
    ln -s /usr/share/icons/dpt.xpm dpt.xpm
    cd -
fi

gzip -cd $FILES_DIR/kernel.tgz | tar pxvf - > /dev/null
cp -fp $FILES_DIR/$I2OSOURCE . 2> /dev/null

echo '----- Rebuilding module dependencies file -----'
depmod -a > /dev/null 2> /dev/null

echo "***** DPT I2O source code patch ($I2OSOURCE) copied to /tmp *****"
sync

KERNEL=`uname -r`
TARGET_MODULE="/lib/modules/$KERNEL/scsi/dpt_i2o.o"
DRIVER_COPIED="1"

#
# Check to see if the kernel version of the OS is supported by our module
#
SUPPORTED="0"
for K in `gzip -cd $FILES_DIR/kernel.tgz | tar tf - | sed "s/dpt_i2o\.//g"`
do
    if [ "$KERNEL" = "$K" ]
    then
        SUPPORTED="1"
    fi
done

if [ "$SUPPORTED" = "1" ]
then
    if echo "$KERNEL" | grep -i smp > /dev/null
    then
        # For the SMP case, copy the non SMP one first
        NOSMP=`echo "$KERNEL" | sed "s/smp//g"`
        cp -fp dpt_i2o.$NOSMP /lib/modules/$NOSMP/scsi/dpt_i2o.o
    fi
    cp -fp dpt_i2o.$KERNEL $TARGET_MODULE
else
    echo '************************************************************'
    echo '***** Please apply the i2o driver source code patch    *****'
    echo "***** (/tmp/$I2OSOURCE) and recompile your kernel      *****"
    echo '***** with DPT I2O set to be built into the kernel or  *****'
    echo '***** as a module.                                     *****'
    echo '************************************************************'
    DRIVER_COPIED="0"
fi

echo '----- Setting up shared library links -----'
if ! grep /usr/local/LessTif/Motif1.2/lib /etc/ld.so.conf > /dev/null
then
    echo /usr/local/LessTif/Motif1.2/lib > /etc/ld.so.conf.bak
    cat /etc/ld.so.conf >> /etc/ld.so.conf.bak
    mv -f /etc/ld.so.conf.bak /etc/ld.so.conf
fi
ldconfig

echo
echo 'Do you want the Communications Engine/Server to load on boot? [y/N]'
read LOADCOM
INITRUNLEVEL=`grep ".*:.*:initdefault" /etc/inittab | cut -f2 -d':'`
if [ "$LOADCOM" = "y" -o "$LOADCOM" = "Y" ]
then
    cd /etc/rc.d/rc$INITRUNLEVEL.d
    if [ ! -e S99dpt ]
    then
        ln -s ../init.d/dpt S99dpt
    fi
    echo '----- DPT Communication Engine/Server set to load on next boot -----'
elif [ ! -e S99dpt ]
then
    echo 'If you later decide to start the Communication Engine/Server on boot, do:'
    echo "cd /etc/rc.d/rc$INITRUNLEVEL.d"
    echo 'ln -s ../init.d/dpt S99dpt'
fi
echo 
echo 'You can manually start/stop the Communications Engine/Server using:'
echo 'sh /etc/rc.d/init.d/dpt start|stop'
echo

INITRD="`grep initrd /etc/lilo.conf | head -n 1 | cut -f2 -d'='`"
if [ "$INITRD" != "" ] && grep -i dpt_i2o /etc/conf.modules > /dev/null
then 
    echo '----- Creating initial ramdisk with DPT I2O driver -----'
    mkinitrd -f $INITRD `uname -r`
    lilo
elif [ "$DRIVER_COPIED" = "1" ]
then
    echo '    *'
    echo '    * Install did not detect an initial ramdisk with dpt_i2o loaded.'
    echo '    * You can start the DPT I2O driver by typing: insmod dpt_i2o'
    echo '    *'
    echo '    * However, it is recommended that you create an initial ramdisk'
    echo '    * with the dpt_i2o driver loaded (see: man mkinitrd) or '
    echo '    * compile the driver into the kernel using the'
    echo "    * /tmp/$I2OSOURCE patch file."
    echo '    *'
fi

echo '----- Cleaning up install files -----'
rm -f `gzip -cd $FILES_DIR/kernel.tgz | tar tf -`

echo '----- Installation complete -----'
echo 
echo '<<<<< You need to reboot in order for changes to take effect >>>>>'
echo 

