#!/bin/bash

dlist='4.0.3 4.0.3'

flist='so40sp3_lnx_01.tar.gz so40sp3_lnx_49.tar.gz'

def_nsv=none

vfound=none

let j=0
for i in $flist
do
  let j=j+1
  dfound=$(echo $dlist | cut -f$j -d' ')
  if [ -e /tmp/$i ]; then
    vfound=$dfound
    cfound=$(echo $dfound | grep 4.0 | wc -w)
    echo Unpacking version $vfound. This will take a while ...
    nstarfn=$i
    # now determine the location of the archive
    if [ -L /tmp/$nstarfn ]; then
      # found symbolic link
      tmpfile=`file /tmp/$nstarfn | gawk '{print $5}'`
    else
      # found regular file
      tmpfile=`file /tmp/$nstarfn | gawk '{print $1}' | sed s/://`
    fi
    echo $tmpfile
    thelocation=`find $tmpfile -printf "%h\n"`
    (cd $thelocation;tar xzf /tmp/$nstarfn)
    # replace setup script
    # replace setup script
    echo Replacing original setup script
    \sed -e "s:@RPM_INSTALL_PREFIX@:$1:" < $1/setup > $thelocation/Office40_Install/setup
    # and install the thingie
    echo
    echo
    echo
    echo "            Choose any of the installation methods but"
    echo "            change the path to $1    "
    echo "            This is IMPORTANT!.                       "
    echo
    echo
    echo
    echo
    echo
    echo Installing version $vfound
    $thelocation/Office40_Install/setup /net
    # remove old stuff
    rm -rf $thelocation/Office40_Install/
    # change permissions
    echo Changing permissions on files ...
    \find $1 -type d -exec chmod a+rx {} \;
    \chmod -R a+r $1
    echo Done.
    echo
    echo
    echo
    echo "You can now remove the file /tmp/$nstarfn"
    echo "Remember, each user must"
    echo
    echo "	+   move his ~/Office40 to ~/Office40.old"
    echo
    echo "	+   run"
    echo	
    echo "		$1/bin/setup"
    echo
    echo "	+   choose 'Installation from Net or CD'"
    echo
    echo "before using StarOffice."
    echo
  else
    if [ -f $1/bin ]; then
      vfound=$i
      echo Found previous staroffice version, $1/bin exists
    fi
  fi
done

if [ $vfound = none ]; then

  echo Error during install, staroffice tar file not found 
  echo Looking for any of the following files or directories

  let j=0
  for i in $flist
  do
    let j=j+1
    echo /tmp/$i
  done

  exit

fi
