#!/bin/csh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
#FUNCTION: setup - 
#                    
#
#SYNTAX: setup uprog
#
#PARAMETERS: 
#                uprog - file containing maple package 
#
#DESCRIPTION:
#  setups qseries maple package 
#
#EXAMPLE:
#  setup umprog
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++
set uprog = $argv[1]
echo '+++++++++++++++++++++++++++++++++++++++++++++++++'
echo "Do you wish to use the default setup ? (yes/no)"
echo '+++++++++++++++++++++++++++++++++++++++++++++++++'
set qsetup = $<
set ERR = 0
if ("$qsetup" == "yes") then
  echo "Default setup will be used"
  echo "The qseries package will be installed in the directory"
  echo "$HOME/maple/mylib"
  echo "If this directory does not exist it will be created."
  if (! -e $HOME/maple) then
     echo "Creating directory $HOME/maple as it does not exist."
     mkdir $HOME/maple
  endif
  if (! -e $HOME/maple/mylib) then
     echo "Creating directory $HOME/maple/mylib as it does not exist."
     mkdir $HOME/maple/mylib
  endif
  set LIBDIR = $HOME/maple/mylib
  echo "LIBDIR=$LIBDIR"
  sleep 1
else
  if ("$qsetup" == "no") then
     echo " "
     echo "CUSTOM SETUP:"
     echo " "
     echo "Enter directory (using FULL PATHNAME)"
     echo "where you would like the qseries package"
     echo "installed."
     set LIBDIR = $<
     if (! -e $LIBDIR) then
        echo " "
        echo "ERROR: $LIBDIR directory does not exist."
        echo "Please create this directory first or use the defaults."
        echo " "
        set ERR = 1
     endif
  else
     if("$qsetup" != "yes") then
        echo "ERROR: answer yes OR no"
     endif
  endif
endif
if ($ERR == 0) then
echo " "
echo "The MAPLE library pathname in the file .mapleinit in your home"
echo "directory has to be changed if you have not installed a package"
echo "before in $LIBDIR."
echo "The DEFAULT is to add the line"
            echo "  libname := libname, "'`'"$LIBDIR"'`;' 
echo "to your .mapleinit file."
   echo " "          
   echo "This program will change your .mapleinit file by default."
   echo "You have the option of doing this by hand."
   echo " "
   if ("$qsetup" == "no") then
   echo '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
   echo "Do you wish to proceed with automatic change to .mapleinit ? (yes/no)"
   echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
	set initchk = $<
   else
	set initchk = "yes"
   endif
   if ("$initchk" == "yes") then
         echo " "
	 echo "You have typed yes so "
         echo "Changes to your .mapleinit file will be done "
         echo "automatically."
         echo " "
         echo "We check to see if .mapleinit exists in your home diectory. "
         if (! -e $HOME/.mapleinit) then
            echo " "
            echo ".mapleinit does not exist so we create it."
            echo " " > $HOME/.mapleinit 
            echo "  libname := libname, "'`'"$LIBDIR"'`;' >> $HOME/.mapleinit
         else 
            echo " "
            echo ".mapleinit exists. ............"
            echo " " >> $HOME/.mapleinit 
            echo "  libname := libname, "'`'"$LIBDIR"'`;' >> $HOME/.mapleinit
	    echo ".mapleinit updated:.."
	    tail $HOME/.mapleinit
	    sleep 3
         endif  
   else
	echo "You typed no ... so you will need to either change"
	echo "your .mapleinit file by hand or check that it works"
	echo "in its present condition."
   endif
endif
if ($ERR == 0) then
   echo " "
   echo "To install the package we will run the MAPLE program $uprog."
   echo "Before we run this program a little modifying will be done"
   echo "and done automatically."
   set linenum = `sed -n '/LIBDIR/=' $uprog|sed -n 1p`
   @ linea = $linenum - 1
   if (-e progtmp) then
   	echo "WARNING: progtmp exists so we delete.."
   	sleep 2
   	rm progtmp
   endif
   echo "sed -n '1,'"$linea"'p' $uprog > progtmp"
   sed -n '1,'"$linea"'p' $uprog > progtmp
   echo '`'"$LIBDIR"'/qseries.m`);' >> progtmp
   echo 'quit' >> progtmp
   echo " "
   echo "Modified MAPLE program has been created."
   echo " "
   echo "We now run the MAPLE program progtmp automatically."
   echo "This program will be run on $HOST using the command line version."
   echo " "
   if ("$qsetup" == "no") then
      echo "You have the option of running this program yourself."
      echo " "
      echo '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
      echo "Shall we proceed with automatic installation ? (yes/no)"
      echo "  If you select no you will have to start maple by hand and "
      echo "  and do some other stuff."
      echo "(ERR=$ERR)"
      echo '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
      set qautom = $<
   else
	set qautom = "yes"
   endif
   if ("$qautom" == "yes" && $ERR == 0) then
      echo " "
      echo ".........installation proceeds........."
      echo "We will now run the modified version of $uprog called progtmp "
      echo " on $HOST."
      ###rsh $HOST maple -s -q < progtmp
      maple -s -q < progtmp
      echo " "
      echo "MAPLE session finished. The maple binary file qseries.m should now"
      echo "be in the directory $LIBDIR."
      if (! -e $LIBDIR/qseries.m) then
         set ERR = 1
         echo " "
         echo "ERROR: qseries.m has NOT be installed in $LIBDIR."
         echo " "
         echo "qseries.m sucessfully installed in $LIBDIR."
         echo " "
      else
         echo " "
         echo " "
         echo "qseries.m sucessfully installed in $LIBDIR."
         echo " "
      endif
      else
      if ("$qautom" == "no") then
         echo " "
         echo "You have decided to do installation by hand."
         echo "   START a maple session by hand and type in the command"
         echo "   "
         echo "   read progtmp"
         echo " "
         echo "   The maple program should run and then terminate."
         echo " "
         echo "The maple binary file qseries.m should now"
         echo "be in the directory $LIBDIR."
         if (! -e $LIBDIR/qseries.m) then
            set ERR = 1
            echo " "
            echo "ERROR: qseries.m has NOT be installed in $LIBDIR."
            echo " "
            echo "qseries.m sucessfully installed in $LIBDIR."
            echo " "
         else
            echo " "
            echo " "
            echo "qseries.m sucessfully installed in $LIBDIR."
            echo " "
         endif
       else
          set ERR = 1
          echo " "
        if("$qautom" != "yes") then
           echo "ERROR: answer yes OR no"
        endif
          echo " "
       endif
   endif
endif


if ($ERR == 0) then
  echo " "
  echo "No errors"
  echo " "
  echo "Test the package by starting a maple session and typing"
  echo " "
  echo "  with(qseries);"
  echo " "
else
  echo " "
  echo "Installation aborted"
  echo " "
endif
