#! /bin/sh
# Vedit Plus 5.16 script 
#
Prod_Title="VEDIT PLUS"
Prod_Des="VEDIT PLUS 5.16 Editor"
Version=v5.16
SimpVer=5.16
#
if test "$Prod_Title" = "VEDIT"
then
    Name_Min=v
    Name_Prog=vedit
    Name_Full=vedit
else
    Name_Min=vp
    Name_Prog=vp
    Name_Full=vplus
fi
#
# Rest of script is identical for both VEDIT and VEDIT PLUS.
#
Product=/qnx4/$Name_Full
OLD_PATH=$PATH

Old_Ver="$Product/${Name_Min}.old"
New_Ver=$Product/$Version
Restore=0

echo ""

# Test if we're installing over beta or if vedit was
# installed under /qnx4/vedit50
if test -d /qnx4/${Name_Full}50
then
  if test -f /qnx4/$Name_Full
  then
    rm  /qnx4/$Name_Full
  fi
  if test -d /qnx4/$Name_Full
  then
    rm -Rf /qnx4/$Name_Full
  fi
  mv /qnx4/${Name_Full}50 /qnx4/$Name_Full
  for names in `/bin/find /qnx4/$Name_Full -type f -level 1`
  do
    rm -f $names
  done
  rm -Rf /qnx4/$Name_Full/tutor
  if test -d /qnx4/$Name_Full/compile
  then
    rm -Rf /qnx4/$Name_Full/compile
  fi
fi
#
#set mode for script to run
#
if test "`basename $0`" != "setup" -a "`basename $0`" != "setup.vedit"
then
  Restore=1
fi
#
# save script under $Product/$Version/.restore
#
if test $Restore != "1"
then
  cp -f /tmp/`basename $0` $Product/$Version/.restore
  chmod 774 $Product/$Version/.restore*
fi

###########################################################################
# check for old versions already installed                                #
###########################################################################
if test -r $Product/.restore
then
  # check restore file for version id
  if test -r $Product/.restore
  then
    OV=`cat $Product/.restore | grep -e'^[\s\t]*Version=v' | sed -e "s/Version=//"`
    Old_Ver="$Product/$OV"
  else
  for x in `ls -td $Product/v*[0-9].??*`
  do
    if test "$NODE$Product" = "`fullpath -t $x`"
    then
    Old_Ver=$x
  fi
  done
fi

else
# then must first time install
  Old_Ver=""
fi

# if current version is not the version that is currently being installed
# then move out old version and move in new version.

if test "$Old_Ver" != "$New_Ver" -a "$Old_Ver" != ""
then
  echo "The $Prod_Des install will now move your current"
  echo "installation to the directory $Old_Ver."

  mkdir -p $Old_Ver

  ( cd $Old_Ver
  for x in `find * .* -level 0 ! -name "${Name_Min}.old[0-9]" ! -name "v*[0-9].??*"`
  do
    rm -f $Old_Ver/$x
    mv -f $Product/$x $Old_Ver/
  done
  )
fi


###########################################################################
# Install this verision as the default version                            #
###########################################################################
if test "$Old_Ver" != "$New_Ver"
then
  SameVer=0
  echo "Installing $New_Ver as default version"
  echo

  ( cd $New_Ver	
    for x in `find * .* -level 0 ! -name "${Name_Min}.old[0-9]" ! -name "v*[0-9].??*"`
    do
      mv -f $x $Product/
      ln -fs  $Product/$x ./ 
    done
  )

else
  SameVer=1
  echo "The current version of the $Prod_Des installed"
  echo "on your system is the same as the current version you are installing."
  echo "Leaving default version intact."
  echo

  # pax clobbers our links... we will replace them now.
  ( cd $New_Ver	
    for x in `find * .* ! -type l -level 0 ! -name "${Name_Min}.old[0-9]" \
      ! -name "v*[0-9].??*"`
    do
      mv -f $x $Product/
      ln -fs  $Product/$x ./ 
    done
  )
fi

###########################################################################
# Setup Links                                                             #
###########################################################################

ln -fs $Product/$Name_Prog     /bin/$Name_Prog
ln -fs $Product/readme.txt     /etc/readme/$Name_Prog

# Give write access to all in tutor directory
chmod o+w $Product/tutor

# install common files if newer
if test -d "$Product/common"
then
  ( cd $Product/common
    find * ! -type d | xargs -i cp -cnf {} /{}
  )
fi

if test $Restore != "1"
then
  # concatenate the version info files
  cat /etc/version/vedver.tmp >> /etc/version/$Name_Prog
  rm -f /etc/version/vedver.tmp
fi

echo "$Prog_Title $SimpVer install is complete"

###########################################################################
# check for Products under /qnx4 that need to be setup                    #
###########################################################################
# check for add-on pacakges that need to be re-setup
Add_on_Products="`ls /qnx4/*/.restore_vedit`" 2>/dev/null

if test "$Add_on_Products" != ""
then
  echo "The setup script has detected add on products that need"
  echo "to be setup for use with the new version of ${Prog_Title}."
  echo "The following products will now be setup :"
  echo
  for x in $Add_on_Products
  do
    Package="`grep "Prod_Des=" $x | grep -v "sed" | sed -e "s/Prod_Des=//"`"
    echo "          $Package"
    done
    echo
    read Response?"Press enter to Continue"
    echo ""
    echo

    for x in $Add_on_Products
    do
      Package="`grep "Prod_Des=" $x | grep -v "sed" | sed -e "s/Prod_Des=//"`"
      echo ""
      echo
      echo "Setting up "$Package" for"
      echo "use with $Prog_Title $SimpVer"
      echo
      $x restore
      read cont?"Press Return to Continue"
      echo
    done
fi

if test $Restore != "1" -a $SameVer != "1"
then
    echo ""
    echo "Please read the files '$Product/readme.txt' and 'whatsnew.txt'."
    echo ""
fi

exit 0
