
# *****************************************************************************
#    This script installs or updates SKsh
# *****************************************************************************

options CFIclsw

clear
echo '\nPlease wait...\n'

# *****************************************************************************
# setup variables containing configuration information
# *****************************************************************************

   rel='2.1'
   arcname='SKsh021.lzh'
   relnum=9
   b="$_ANSI_P3"
   e="$_ANSI_P1"
   errorlog='ram:Install_SKsh.err'
   st_file='s:user-startup'
   ms_file='ram:ManSteps.txt'
   RECUR_DEPTH=42

# *****************************************************************************
# setup variables containing files we need to install
# *****************************************************************************

  main_files='sksh'

  script_files='scr/chext scr/cleard scr/dstack scr/emacs_mode scr/histlist
    scr/histload scr/histrm scr/histsave scr/man scr/path scr/popd scr/pushd
    scr/qrm scr/quit_serv scr/usage scr/vi_mode'

  script_s_files='contrib/Script_Src/chext.s contrib/Script_Src/cleard.s
    contrib/Script_Src/dstack.s contrib/Script_Src/emacs_mode.s
    contrib/Script_Src/histlist.s contrib/Script_Src/histload.s
    contrib/Script_Src/histrm.s contrib/Script_Src/histsave.s
    contrib/Script_Src/man.s contrib/Script_Src/path.s
    contrib/Script_Src/popd.s contrib/Script_Src/pushd.s
    contrib/Script_Src/qrm.s contrib/Script_Src/quit_serv.s
    contrib/Script_Src/usage.s contrib/Script_Src/vi_mode.s'

  bin_files='bin/bdf bin/bsplit bin/cat bin/cmp bin/cp bin/crc bin/cut
    bin/doc2man bin/du bin/encr bin/fgrep bin/find bin/fmt bin/fold
    bin/grep bin/head bin/indent bin/join bin/ln bin/mv bin/num bin/paste
    bin/printenv bin/split bin/srun bin/strings bin/tail bin/tee
    bin/usecount bin/view bin/wc bin/window bin/xargs bin/xd'

  init_files='init/.skshinit init/.skshinit.pp sksh.msg'

  config_files='init/.skshrc'

  inst_files="README.install"

  contrib_dirs='contrib'

  doc_files='doc/Addendum1.1.doc doc/Addendum1.1.doc doc/Addendum1.2.doc
    doc/Addendum1.3.doc doc/Addendum1.4.doc doc/Addendum1.5.doc
    doc/Addendum1.6.doc doc/Addendum1.7.doc doc/Addendum2.0.doc
    doc/Addendum2.1.doc doc/Bugs.doc doc/Diffs.doc doc/ErrorCodes.doc
    doc/ExtCmds.doc doc/Hints.doc doc/Install.doc doc/MiscInfo.doc
    doc/Problems.doc doc/ReadMe_1st.doc doc/Reference.doc
    doc/TechNotes.doc doc/TinySKsh.doc doc/UserMan.doc doc/View.doc'

  jm_bin_68000='jm/bin.68000/JM jm/bin.68000/pri jm/bin.68000/ps
    jm/bin.68000/stat jm/bin.68000/taskinfo jm/bin.68000/uptime'

  jm_bin_68030='jm/bin.68030/JM jm/bin.68030/pri jm/bin.68030/ps
    jm/bin.68030/stat jm/bin.68030/taskinfo jm/bin.68030/uptime'

  jm_hnd_68000='jm/bin.68000/JM-Handler'
  jm_hnd_68030='jm/bin.68030/JM-Handler'

  jm_init='jm/.jmrc'

  jm_docs='jm/Docs/JM.doc jm/Docs/JMRexx.doc jm/Docs/Patch_1.1a.txt
    jm/Docs/Patch_1.1b.txt jm/Docs/Readme_1.1.txt jm/Readme_1st.txt
    jm/Docs/Readme_Bridgeboard.txt jm/RexxExamples/queries.rexx
    jm/RexxExamples/sysload.rexx'

# *****************************************************************************
#    cleanup function to remove files we made resident
# *****************************************************************************

function cleanup {
   rm -rf ram:.cmds >nil:
}

# *****************************************************************************
#    Report an error
# *****************************************************************************

function error {
   echo -c "\n$b" "ERROR: " "$e" "$1\n"
   echo "ERROR: $1\n" >>"$errorlog"
   return 1
}

# *****************************************************************************
#    Report a warning
# *****************************************************************************

function warn {
   echo -c "$b" "WARNING: " "$e" "$1\n"
   echo "WARNING: $1\n" >>"$errorlog"
}

# *****************************************************************************
#    See if we should terminate because of errors
# *****************************************************************************

function ck_terminate {
   if [ -f "$errorlog" ]
   then
      echo "\n${b}Errors were encountered.  Check the file $errorlog.${e}\n"
      cleanup
      return 1 5
   fi
}

# *****************************************************************************
#    Checksum files
# *****************************************************************************

function crc_files {

   local fspec

   fspec=$(tackon "$1" "$2")

   echo -n '.'

   if [ ! -f "$fspec" ]
   then
      echo
      error "this file is missing: $fspec"
   else
      crcval=$(crc -q "$fspec") || error "can't crc $fspec" || ck_terminate

      if [ "$crcval" != "$3" ]
      then
         echo
         error "this file has an incorrect checksum: $fspec"
      fi

      if [ $(expr index "$4" 'p') -gt 0 -a ! -P "$fspec" ]
      then
         echo
         warn "this file should have the pure bit set: $fspec"
      fi

      if [ $(expr index "$4" 's') -gt 0 -a ! -S "$fspec" ]
      then
         echo
         warn "this file should have the script bit set: $fspec"
      fi
   fi
}

# *****************************************************************************
#    Verify that files exist
# *****************************************************************************

function check_files {

   local notfound file

   notfound='0'
   sty="$1"; shift

   for file in $1
   do
      if [ "$sty"  = 'DIR'  -a ! -d "$file" -o
           "$sty"  = 'PWD'  -a ! -f "$file" -o
           "$sty"  = 'PATH' -a -z $(which -s "$file") ]
      then
        error "The '$file' file must be present for this script to
       operate properly.  Please install it in the appropriate
       place and run this script again."
        notfound=1
      fi
      shift
   done

   return "$notfound"
}


# *****************************************************************************
#    Install files
# *****************************************************************************

function install_files {

   local install_dir chmod_flags overwrite fspec file

   install_dir = "$1"
   chmod_flags = "$2"
   overwrite   = "$3"
   shift 3

   for file in $1
   do
      echo -n '.'

      if [ $(expr index "$overwrite" 'B') -gt 0 ]
      then
         fspec=$(tackon "$install_dir" $(basename $(extname -v "$file")))
      else
         fspec=$(tackon "$install_dir" $(basename "$file"))
      fi

      if [ -f "$fspec" ]
      then
         if [ $(expr index "$overwrite" 'Y') -gt 0 ]
         then
            chmod +wd "$fspec" >nil: ||
               error "can't chmod +wd $fspec" || ck_terminate
            ow='Y'
         else
            ow='N'
         fi
      else
         ow='Y'
      fi

      if [ -f "$file" ]
      then
         if [ "$ow" = 'Y' ]
         then
            cp "$file" "$fspec" >nil: ||
                error "can't copy $file to $fspec" || ck_terminate
            chmod "$chmod_flags" "$fspec" >nil: ||
                  error "can't change $fspec mode to $chmod_flags" ||
                  ck_terminate
         fi
      else
         echo
         error "The file '$file' is missing from the current directory."
         return 1
      fi
   done
}


# *****************************************************************************
#    Read dir name (or file name)
# *****************************************************************************

function read_dir {

   local in_dir read_dir; in_dir=''

   while [ -z "$in_dir" -o "$4" = 'directory' -a ! -d "$in_dir" -o 
           "$4" = 'file' -a ! -f "$in_dir" ]
   do
      echo
      echo "$1"

      line read_dir "$2 $b\[$3]$e ? > " 20

      in_dir=$(pathconv -a "$read_dir")

      if [ "$4" = 'directory' -a ! -d "${in_dir:=$3}" -o
           "$4" = 'file'      -a ! -f "${in_dir:=$3}" ]
      then
         echo "\n$in_dir is not a valid $4 name."

         if [ "$4" = 'directory' ]
         then

            line yn "Do you wish to create it? $b[y/N]$e > " 20
            if [ $(expr substr $(toupper "$yn") 1 1) = 'Y' ]
            then
               mkdir -p "$in_dir"
               [ -d "$in_dir" ] || error "can't create directory $in_dir"
            fi
         fi
      fi
   done

   export -L in_dir
}

# *****************************************************************************
#   Resolve a device name
# *****************************************************************************

function resolve_dir {
   if [ $(expr index "$1" ':') -gt 0 ]
   then
      dev_name=$(car "$1" ':')
      f_name=$(cdr "$1" ':')
      tackon $(bdf -z "$dev_name:" | cut -c54-200) $f_name
   else
      echo "$1"
   fi
}


# *****************************************************************************
#   Check space on devices
# *****************************************************************************

function check_fs_space {

   local _needed_space
   
   _needed_space="$4"

   [ $(toupper $(expr substr "$1" 1 3)) = 'RAM' ] && return

   [ -f $(tackon "$1" "$2") ] && _needed_space="$5"
   [ -f $(tackon "$1" "$3") ] && _needed_space="$6"

   if [ $(bdf -a -z "$1" | cut -c36-43) -lt "$_needed_space" ]
   then
      error "not enough space in directory: $1" || ck_terminate
   fi
}

# *****************************************************************************
#   Check a device assignment
# *****************************************************************************

function check_assign {

   local device loc descr defdir change

   device = "$1"
   defdir = "$2"
   loc    = "$3"
   descr  = "$4"
   change = ''

   echo -n "Checking the $device: device..."

   if [ -m "$device:" ]
   then

      if [ $(tolower "$defdir") != $(tolower "$loc") ]
      then
         change='TRUE'
         clear
         echo "${b}Incorrect assignment of $device:${e} - should be $loc."

         echo "The '$b$device:$e' assignment currently points to the '$b$defdir$e'
directory.  You previously installed the $descr
files in '$b$loc$e' directory.

This assignment must be changed for SKsh to operate correctly."

      fi
   else
      change='TRUE'
      clear
      echo "
The '$b$device:$e' assignment does not currently exist.
This device must be assigned to the directory containing
the $descr files.

You previously installed these files in the '$b$loc$e'
directory.
"
   fi

   if [ -n "$change" ]
   then
      echo
      line yn "Press return to continue: > "

      echo -n "
If you wish, this script will now assign '$b$device:$e' to the
'$b$loc$e' directory.  ${b}NOTE:${e} This will ONLY be in affect until
you reboot your computer.  To make this change permanent, you must
modify your ${b}${st_file}${e} file and make it create these
assignments.

If you type 'Y' at the prompt below, the following assignment will be
made:

   ${b}assign $device: $loc${e}

Do you want to make this assignment now? [Y/n] > "

      read yn

      if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
      then
          echo "Executing ${b}assign $device: $loc${e}..."
          assign "$device:" "$loc"
      else
          echo "Assignment not made.  You ${b}must${e} make it manually."
          warn "Incorrect assignment of $device: - should be $loc." >nil:
      fi
      echo
      line yn "Press return to continue: > "

   else
      echo "${b}OK${e}"
   fi
}

# *****************************************************************************
#    Verify a line in the startup sequence
# *****************************************************************************

function check_ss {
   if eval "$1"
   then return 0
   else
      echo "   $stepnum) $2"
      echo "   $stepnum) $2" >> "$ms_file"
      if [ -n "$3" ]
      then
         inc stepnum
         echo "   $stepnum) $3"
         echo "   $stepnum) $3" >> "$ms_file"
      fi
      return 1
   fi
}

# *****************************************************************************
#   start of "main" script - check system setup and prerequisites
# *****************************************************************************

if [ ! -m 'ram:' ]
then
   echo "${b}ERROR${e}: This script requires the ram: disk to be temporarily mounted.
       Also, you can assign ram: to another location.  In any case, please
       fix this and run this script again.\n"
   cleanup
   return 0
fi

if [ "$LINES" -lt 22 -o "$COLUMNS" -lt 75 ]
then
   error "This script requires a larger window size.  Please make
       your window at least 22 lines by 75 columns and run this
       script again."
   ck_terminate
fi

#if [ $(cdr $(stack) ':') -lt 8000 ]
#then
#   error "Please set the stack size to at least 8000 bytes and run
#       this script again."
#   ck_terminate
#fi

if [ $(version -s) -lt "$relnum" ]
then
   error "This script must be run under SKsh $rel or later.  Please
       un-archive SKsh $rel, and type 'sksh -n Install_$rel.sksh' to
       run this script."
   ck_terminate
fi

if [ ! -d 'bin' -o ! -d 'doc' -o ! -d 'init' -o ! -d 'contrib' -o ! -d 'scr' ]
then
   error "This script must be invoked from the directory in which
       you unarchived the main $arcname file.  You can use the -m
       option to lharc to automatically create directories:

          lharc -m -a -x x $arcname"
   ck_terminate
fi

if [ $(bin/usecount) -gt 1 ]
then
   error "Please terminate all other invocations of SKsh before
       running this script."
   ck_terminate
fi


if [ ! -m 't:' ]
then
   mkdir -p ram:tmp  || error "can't create ram:tmp"       || ck_terminate
   assign t: ram:tmp || error "can't assign t: to ram:tmp" || ck_terminate
   no_tdir=TRUE
fi

check_files 'PATH' assign || ck_terminate
check_files 'PWD' 'bin/grep bin/cp bin/cat bin/crc bin/cut bin/tail bin/bdf' ||
    ck_terminate

chmod +p bin/cp bin/crc bin/grep bin/cat bin/cut bin/tail bin/bdf ||
   error "can't set pure bits on commands" || ck_terminate

[ -d ram:.cmds ] || mkdir -p ram:.cmds ||
   error "can't create directory ram:.cmds" || ck_terminate

bin/cp bin/cp bin/crc bin/grep bin/cat bin/cut bin/tail bin/bdf ram:.cmds

alias cp='ram:.cmds/cp'
alias crc='ram:.cmds/crc'
alias grep='ram:.cmds/grep'
alias cat='ram:.cmds/cat'
alias cut='ram:.cmds/cut'
alias tail='ram:.cmds/tail'
alias bdf='ram:.cmds/bdf'

# *****************************************************************************
#    End of checking.  Start actual steps
# *****************************************************************************

clear

echo "${b}SKsh $rel installation script ----------------------------------${e}

This script will assist you in installing SKsh $rel on your HD by
performing tasks which can be easily automated.  It will do the
following:

   1) Check that all files are present from archive.
   2) Input destination directory names for install.
   3) Install SKsh files into above dirs (this step can be skipped).
   4) Install JM (optional)
   5) check device assignments (sksh:, etc)
   6) check integrity of installed SKsh & JM commands
   7) Update or create .pp (faster script) files (optional)
   8) Install SKsh & JM man pages (optional)
   9) Report additional manual activities which are necessary

To run this script, the main SKsh archive (called $arcname) must have
been de-archived in the current directory.  You should have invoked
this by typing 'sksh -n Install_$rel.sksh' from an AmigaDos CLI.
"

rm -f "$errorlog" >nil:
chmod +s scr/emacs_mode
scr/emacs_mode >nil: || error "unable to set editing mode" || ck_terminate

line yn "Do you wish to continue? $b[y/N]$e > " 20
echo

if [ $(expr substr $(toupper "$yn") 1 1) != 'Y' ]
then
  echo "Ok, bye."
  cleanup
  return
fi


# -------------- step 1 -----------------------------------------------

clear
echo "${b}STEP ONE: Check unarchived files ---------------------------------${e}"

echo "
The files in the current working directory will now be checked to
insure that they all have been unarchived in the proper locations.
"

echo -n "Checking main sksh binary..."
check_files 'PWD' "$main_files"      || ck_terminate

echo -n "${b}OK${e}\nChecking external binary commands..."
check_files 'PWD' "$bin_files"       || ck_terminate

echo -n "${b}OK${e}\nChecking external script commands..."
check_files 'PWD' "$script_files"    || ck_terminate
check_files 'PWD' "$script_s_files"  || ck_terminate

echo -n "${b}OK${e}\nChecking initialization files..."
check_files 'PWD' "$init_files"      || ck_terminate
check_files 'PWD' "$config_files"    || ck_terminate

echo -n "${b}OK${e}\nChecking documentation..."
check_files 'PWD' "$doc_files"       || ck_terminate

echo -n "${b}OK${e}\nChecking installation files..."
check_files 'PWD' "$inst_files"      || ck_terminate

echo -n "${b}OK${e}\nChecking JM files..."
check_files 'PWD' "$jm_bin_68000"    || ck_terminate
check_files 'PWD' "$jm_bin_68030"    || ck_terminate
check_files 'PWD' "$jm_hnd_68000"    || ck_terminate
check_files 'PWD' "$jm_hnd_68030"    || ck_terminate
check_files 'PWD' "$jm_init"         || ck_terminate
check_files 'PWD' "$jm_docs"         || ck_terminate

echo -n "${b}OK${e}\nChecking contrib directory..."
check_files 'DIR' "$contrib_dirs"    || ck_terminate

echo -n "${b}OK${e}\n\n\nThe necessary files were found.  Press return to continue: > "
read yn

# -------------- step 2 -----------------------------------------------

clear
echo "${b}STEP TWO: input directories --------------------------------------${e}"

echo -n "
This script must know the locations of several directories in
order to properly install the SKsh files.  Defaults will given in
brackets for each directory - you may press <return> to accept the
default, or type in a legal directory name.
"

read_dir "Please enter the directory which will contain the main SKsh " \
         "binary" "c:" 'directory'

sksh_dir=$(resolve_dir "$in_dir")

[ -m sksh: ] && devsksh=$(resolve_dir 'sksh:')

skshcpu=68000
CPU check 68030 >nil: && skshcpu=68030

echo "\nIf you have a 68030 or 68040, and you never boot from a 68000,
then you can install the 68030 version of SKsh.  If you have
a 68000 or 68020, or ever plan to boot from one, then you _must_
install the 68000 version of SKsh.  By default, the

    ${b}$skshcpu${e}

version will be installed on this system.
"

line yn "Do you wish to accept this default? [Y/n] > "

if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
then true
else
   if [ "$skshcpu" = '68030' ]
   then skshcpu=68000
   else skshcpu=68030
   fi
fi

[ "$skshcpu" = 68000 ] && sksh_files='sksh'
[ "$skshcpu" = 68030 ] && sksh_files='sksh.68030'

read_dir "Please enter the directory which will contain the .skshinit " \
         "and .skshrc startup files" "${devsksh:-s:}" 'directory'

init_dir=$(resolve_dir "$in_dir")

[ -m skshbin: ] && devbin=$(resolve_dir 'skshbin:')

read_dir "Please enter the directory which will contain the SKsh " \
         "external binary commands" "${devbin:-sys:bin}" 'directory'

bin_dir=$(resolve_dir "$in_dir")

[ -m skshscr: ] && devscr=$(resolve_dir 'skshscr:')

read_dir "Please enter the directory which will contain the SKsh " \
         "external scripts" "${devscr:-sys:scr}" 'directory'

scr_dir=$(resolve_dir "$in_dir")

echo
line yn "Do you wish to install the documentation files? $b[Y/n]$e > " 20

doc_dir=''

if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
then
   read_dir "Please enter the directory which will contain the SKsh " \
            "documentation files." "sys:doc" 'directory'
   doc_dir=$(resolve_dir "$in_dir")
fi

echo
line yn "Do you wish to install the contrib files? $b[Y/n]$e > " 20

contrib_dir=''

if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
then
   read_dir "Please enter the directory which will contain the SKsh " \
            "contrib directory." "sys:usr" 'directory'
   contrib_dir=$(resolve_dir "$in_dir")
fi

echo -n "\nChecking disk space..."
check_fs_space "$sksh_dir" sksh               sksh  120  20   20
check_fs_space "$init_dir" .skshinit     .skshinit   20   2    2
check_fs_space "$bin_dir"  xargs          usecount  200  75   25
check_fs_space "$scr_dir"  vi_mode       quit_serv   20   6    2

if [ -n "$doc_dir" ] then
   check_fs_space "$doc_dir"  Addendum1.7.doc Addendum2.0.doc \
                  800  250 50
fi

if [ -n "$contrib_dir" ] then
   check_fs_space "$contrib_dir"  koren/whereis koren/whereis \
                  20  20 20
fi

echo "${b}OK${e}\n"

# -------------- step 3 -----------------------------------------------

clear
echo "${b}STEP THREE: install files ------------------------------------${e}"

echo -n "
The SKsh files will now be installed in the locations you chose in
${b}step 2${e}.  Configuration files such as .skshrc will ${b}not${e} be
overwritten if they exist so that this script can be used for either
a new install, or an update to a later SKsh version.  You may choose to
skip this step, although no files will be installed if you do.  Choose
Y or press return to perform the installation.

"

line yn "Do you wish to perform this step? $b[Y/n]$e > " 20
echo

if [ $(expr substr $(toupper "$yn") 1 1) = 'N' ]
then
   echo "File installation skipped - No files installed."
else
   echo -n "Installing sksh binary."
   install_files "$sksh_dir" "rep" 'YB' "$sksh_files"    || ck_terminate

   echo -n "${b}OK${e}\nInstalling external binary commands."
   install_files "$bin_dir" "rep" 'Y' "$bin_files"       || ck_terminate

   echo -n "${b}OK${e}\nInstalling external script commands."
   install_files "$scr_dir" "res" 'Y' "$script_files"    || ck_terminate

   echo -n "${b}OK${e}\nInstalling initialization files."
   install_files "$init_dir" "res" 'Y' "$init_files"     || ck_terminate
   install_files "$init_dir" "res" 'N' "$config_files"   || ck_terminate

   if [ -n "$doc_dir" ]
   then
      echo -n "${b}OK${e}\nInstalling documentation."
      install_files "$doc_dir" "r" 'Y' "$doc_files"      || ck_terminate
   fi

   if [ -n "$contrib_dir" ]
   then
      echo -n "${b}OK${e}\nInstalling contrib directory..."
      [ -d "$contrib_dir" ]
      cp -r $contrib_dirs "$contrib_dir" ||
          error "Error installing contrib directory in $contrib_dir" ||
          ck_terminate
   fi

   echo -n "${b}OK${e}\n\nFile installation complete.  "
fi

echo -n "Press return to continue: > "; read yn


# -------------- step 4 -----------------------------------------------

clear
echo "${b}STEP FOUR: Install JM ---------------------------------------${e}"

echo -n "
To use the uptime, ps, taskinfo, and other similar commands,
JM must be installed.  By performing this step, the JM files will be
installed in the appropriate location on your hard disk.  If you
already have a recent version of JM installed, or do not wish to use
these features, then this step can be skipped.

To use JM, it is necessary to start the JM server from your
startup-sequence file.

"

line yn "Do you wish to perform this step? $b[Y/n]$e > " 20
echo


if [ $(expr substr $(toupper "$yn") 1 1) = 'N' ]
then
   echo "\nStep skipped, JM not installed."
   jm_installed=''
else
   jm_installed='TRUE'
   jmproc=68000
   CPU check 68030 >nil: && jmproc=68030

   echo "If you have a 68030 or 68040, and you never boot from a 68000,
then you can install the standard 68030 version of JM.  If you have
a 68000 or 68020, or ever plan to boot from one, then you _must_
install the 68000 version of JM.  By default, the

    ${b}$jmproc${e}

version will be installed on this system.
"

   line yn "Do you wish to accept this default? [Y/n] > "

   if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
   then true
   else
      if [ "$jmproc" = '68030' ]
      then jmproc=68000
      else jmproc=68030
      fi
   fi

   if [ "$jmproc" = '68000' ]
   then jm_bin="$jm_bin_68000"; jm_hnd="$jm_hnd_68000"
   else jm_bin="$jm_bin_68030"; jm_hnd="$jm_hnd_68030"
   fi

   echo "Installing ${b}${jmproc}${e} version of JM."

   if [ -f $(tackon "$bin_dir" 'JM') ]
   then
      echo -n "JM already installed - Updating files..."
   else
      echo -n "Performing new JM installation..."
   fi

   install_files "$bin_dir" "rep"  'Y' "$jm_bin"        || ck_terminate
   install_files "L:"       "re"   'Y' "$jm_hnd"        || ck_terminate
   install_files "S:"       "rewd" 'N' "$jm_init"       || ck_terminate

   if [ -n "$doc_dir" ]
   then
      install_files "$doc_dir" "r"    'Y' "$jm_docs"       || ck_terminate
   fi

   echo "\nProcess complete."
fi

echo -n "\nPress return to continue: > "
read yn

# -------------- step 5 -----------------------------------------------

clear
echo "${b}STEP FIVE: check device assignments --------------------------${e}"

echo -n "
SKsh needs several AmigaDos device assignments in order to operate
correctly.  This step will check that those assignments have been made
correctly.

Press return to continue: > "
read yn

echo

check_assign 'sksh'    "$devsksh" "$init_dir" 'SKsh binary'
check_assign 'skshbin' "$devbin"  "$bin_dir"  'SKsh external binary'
check_assign 'skshscr' "$devscr"  "$scr_dir"  'SKsh external script'

echo -n "\nPress return to continue: > "
read yn

# -------------- step 6 -----------------------------------------------

clear
echo "${b}STEP SIX: check integrity of installed files ----------------${e}"

echo -n "
This step will verify that the installed files are in place and that
they have proper checksums.  No errors should be reported.

Press return to continue: > "

read yn;

echo -n "\nChecking sksh binary."

if [ "$skshcpu" = 68000 ]
then
  crc_files "$sksh_dir" sksh          '     71504    5124640 1227865301' 'p'
else
  crc_files "$sksh_dir" sksh          '     70944    5077988 1841425393' 'p'
fi

echo -n "${b}OK${e}\nChecking initialization files."

crc_files "$init_dir" sksh.msg      '      6861     556979 1995857194' 's'
crc_files "$init_dir" .skshinit     '      1470      87543 1445907398' 's'
crc_files "$init_dir" .skshinit.pp  '       659      39779 1442492494' 's'

echo -n "${b}OK${e}\nChecking external binary commands."

crc_files "$bin_dir"  bdf           '      3412     265727  613604414' 'p'
crc_files "$bin_dir"  bsplit        '      3056     234556  613599961' 'p'
crc_files "$bin_dir"  cat           '      2208     169825 1227109556' 'p'
crc_files "$bin_dir"  cmp           '      2556     193692 1840672903' 'p'
crc_files "$bin_dir"  cp            '      4108     319329 1227178828' 'p'
crc_files "$bin_dir"  crc           '      2256     169303  613542874' 'p'
crc_files "$bin_dir"  cut           '      3360     240045  613532768' 'p'
crc_files "$bin_dir"  doc2man       '      2948     233319 1840667242' 'p'
crc_files "$bin_dir"  du            '      2792     207365      29320' 'p'
crc_files "$bin_dir"  encr          '      3428     258693  613530104' 'p'
crc_files "$bin_dir"  fgrep         '      4168     299296      42453' 'p'
crc_files "$bin_dir"  find          '      6252     468431 1227066898' 'p'
crc_files "$bin_dir"  fmt           '      3292     242539 1227167858' 'p'
crc_files "$bin_dir"  fold          '      2612     198726 1227161599' 'p'
crc_files "$bin_dir"  grep          '     12304     908425  613696228' 'p'
crc_files "$bin_dir"  head          '      2160     167767 1227109850' 'p'
crc_files "$bin_dir"  indent        '      2684     186286 1840673961' 'p'
crc_files "$bin_dir"  join          '      2344     176349 1227108624' 'p'
crc_files "$bin_dir"  ln            '      3160     241738  613600987' 'p'
crc_files "$bin_dir"  mv            '      4792     382314 1840645957' 'p'
crc_files "$bin_dir"  num           '      2616     181866 1840725947' 'p'
crc_files "$bin_dir"  paste         '      2516     194457      27476' 'p'
crc_files "$bin_dir"  printenv      '      2940     208280 1840670819' 'p'
crc_files "$bin_dir"  split         '      2400     181829      25672' 'p'
crc_files "$bin_dir"  srun          '      2448     183680  613592693' 'p'
crc_files "$bin_dir"  strings       '      2160     163787  613543662' 'p'
crc_files "$bin_dir"  tail          '      3260     248338 1840735443' 'p'
crc_files "$bin_dir"  tee           '      2248     171979 1227157778' 'p'
crc_files "$bin_dir"  usecount      '      2316     171685 1227157736' 'p'
crc_files "$bin_dir"  view          '      5468     434944 1840762101' 'p'
crc_files "$bin_dir"  wc            '      2788     210561  613536980' 'p'
crc_files "$bin_dir"  window        '      3884     297013 1227175640' 'p'
crc_files "$bin_dir"  xargs         '      3652     276584  613605965' 'p'
crc_files "$bin_dir"  xd            '      2740     193739  613594130' 'p'

echo -n "${b}OK${e}\nChecking external script commands."

crc_files "$scr_dir"  chext         '       724      45784 1383471787' 's'
crc_files "$scr_dir"  cleard        '       239      14098  725409077' 's'
crc_files "$scr_dir"  dstack        '       374      22388  735247577' 's'
crc_files "$scr_dir"  emacs_mode    '      1664      86892  513992993' 's'
crc_files "$scr_dir"  histlist      '       543      35192 1630015051' 's'
crc_files "$scr_dir"  histload      '       445      28406  166778055' 's'
crc_files "$scr_dir"  histrm        '       286      17220  128700041' 's'
crc_files "$scr_dir"  histsave      '       435      27819 1447378390' 's'
crc_files "$scr_dir"  man           '      1083      67778 1576236955' 's'
crc_files "$scr_dir"  path          '      1623      93676 1797724833' 's'
crc_files "$scr_dir"  popd          '       594      36282  716618013' 's'
crc_files "$scr_dir"  pushd         '       418      24979  111840766' 's'
crc_files "$scr_dir"  qrm           '       573      35577 1415213548' 's'
crc_files "$scr_dir"  quit_serv     '      1463      96899 1389170798' 's'
crc_files "$scr_dir"  usage         '       351      22123 2127304086' 's'
crc_files "$scr_dir"  vi_mode       '      2083     107986 1127562763' 's'

if [ -n "$doc_dir" ]
then
  echo -n "${b}OK${e}\nChecking documentation."

  crc_files "$doc_dir" Addendum1.1.doc '      7417     536165  232699304' ''
  crc_files "$doc_dir" Addendum1.2.doc '      9686     703931  846285346' ''
  crc_files "$doc_dir" Addendum1.3.doc '     20545    1548621 2073534848' ''
  crc_files "$doc_dir" Addendum1.4.doc '     25855    1891236  994254671' ''
  crc_files "$doc_dir" Addendum1.5.doc '     30436    2313205 2073634712' ''
  crc_files "$doc_dir" Addendum1.6.doc '     14721    1092002  380811453' ''
  crc_files "$doc_dir" Addendum1.7.doc '     35719    2690215  380587818' ''
  crc_files "$doc_dir" Addendum2.0.doc '     28663    2143239  232633974' ''
  crc_files "$doc_dir" Addendum2.1.doc '     19036    1425166  232526711' ''
  crc_files "$doc_dir" Bugs.doc        '      2037     145822 1159037031' ''
  crc_files "$doc_dir" Diffs.doc       '      5756     422227   87712714' ''
  crc_files "$doc_dir" ErrorCodes.doc  '      2307     150406 1446243009' ''
  crc_files "$doc_dir" ExtCmds.doc     '     72944    4913370 1063665739' ''
  crc_files "$doc_dir" Hints.doc       '      7039     525317  851014904' ''
  crc_files "$doc_dir" Install.doc     '     11743     867803 1677328126' ''
  crc_files "$doc_dir" MiscInfo.doc    '      5521     419500  740469609' ''
  crc_files "$doc_dir" Problems.doc    '      3999     271535  434914958' ''
  crc_files "$doc_dir" Readme_1st.doc  '      7357     519431 1967617398' ''
  crc_files "$doc_dir" Reference.doc   '    201326   13447096  140082941' ''
  crc_files "$doc_dir" TechNotes.doc   '      4943     374730 1267199451' ''
  crc_files "$doc_dir" TinySKsh.doc    '       647      31282  774328781' ''
  crc_files "$doc_dir" UserMan.doc     '    120916    8996398  449033385' ''
  crc_files "$doc_dir" View.doc        '     12443     933726 1702404953' ''
fi

if [ -n "$jm_installed" ]
then
  echo -n "${b}OK${e}\nChecking JM files."

  if [ "$jmproc" = "68030" ]
  then

    crc_files "$bin_dir"  JM            '      7216     593965 1534001440' 'p'
    crc_files "$bin_dir"  pri           '      4284     350000  613616453' 'p'
    crc_files "$bin_dir"  ps            '      8048     625365  306694344' 'p'
    crc_files "$bin_dir"  stat          '      4124     332973 2147436384' 'p'
    crc_files "$bin_dir"  taskinfo      '      4916     395436 1380469015' 'p'
    crc_files "$bin_dir"  uptime        '      3152     240702 1533882809' 'p'

  else

    crc_files "$bin_dir"  jm            '      7292     609792 1534003701' 'p'
    crc_files "$bin_dir"  pri           '      4296     352002  613616739' 'p'
    crc_files "$bin_dir"  ps            '      8160     647012  306875505' 'p'
    crc_files "$bin_dir"  stat          '      4396     355220  920400577' 'p'
    crc_files "$bin_dir"  taskinfo      '      4984     408809  766900348' 'p'
    crc_files "$bin_dir"  uptime        '      3436     265770 1533954555' 'p'
  fi

fi

echo -n "${b}OK${e}\n\nIntegrity check complete.  Press return to continue: > "
read yn

# -------------- step 7 -----------------------------------------------

clear
echo "${b}STEP SEVEN: Re-create .skshrc.pp -------------------------------${e}"

echo -n "
Sksh permits a special form of script file, known as a .pp file, to
be executed faster than a plain script file.  If you use one for
your .skshrc init file, invocation time will be improved.

If you wish to use a .skshrc.pp, it must match have been created
by SKsh version $rel.  This step will allow you to create a new
.skshrc.pp file if you wish.

"

line yn "Do you wish to perform this step? $b[Y/n]$e > " 20
echo

rcfile=$(tackon "$init_dir" .skshrc)

if [ $(expr substr $(toupper "$yn") 1 1) = 'N' ]
then
   echo "\nStep skipped, no .skshrc.pp file created."
else
   [ -f "$rcfile" ] || error "No .skshrc file found in $init_dir" ||
                       ck_terminate

   if [ -f "${rcfile}.pp" ]
   then echo "Re-creating .skshrc.pp from .skshrc..."
   else echo "No .skshrc.pp file found, creating new one..."
   fi
   preparse < "$rcfile" > "${rcfile}.pp" ||
       error "Unable to create .skshrc.pp file in $init_dir" ||
       ck_terminate
       
   echo "Process complete."
fi

echo -n "\nPress return to continue: > "
read yn

# -------------- step 8 -----------------------------------------------

clear
echo "${b}STEP EIGHT: Install SKsh manual pages ------------------------${e}"

echo "
If you wish to use the SKsh 'man' program to access the manual pages
for commands, variables, and aliases supplied by SKsh, you will need
to perform this step.  It is automated.  SKsh manpages reside in the
MAN:SKsh directory, where man points to any valid AmigaDos directory.
This step will create such a directory if it does not exist, and copy
the manpages there.  The manpages are extracted from the Reference.doc
and ExtCmds.doc files in the doc directory.  You must have enough disk
space to store the manpages.

You may choose to skip this step, but if you do, you will be unable to
use the SKsh manpages.
"

perf_man=FALSE
line yn "Do you wish to perform this step? $b[Y/n]$e > " 20

if [ $(expr substr $(toupper "$yn") 1 1) != 'N' ]
then
   perf_man=TRUE
   clear
   if [ ! -m 'MAN:' ]
   then
      echo "You do not currently have a MAN: assignment.  You must make
one now in order to install the manpages.\n"

      man_dir='sys:man'
   else
      man_dir=$(resolve_dir "MAN:")
      echo "Your current MAN: directory is $man_dir.  Press return to
use this directory, or type in a new one."
   fi

   read_dir "Please enter the directory which will contain the SKsh" \
            "manpage entries" "$man_dir" 'directory'
   man_dir=$(resolve_dir "$in_dir")

   assign MAN: "$man_dir"

   check_fs_space "$man_dir"  view.MAN usecount.MAN 250  75 50
   
   sman_dir=$(tackon "$man_dir" SKsh)
   mkdir -p "$sman_dir"
   [ -d "$sman_dir" ] || error "Can't create $sman_dir" || ck_terminate
   
   check_files 'PWD' 'bin/doc2man' || ck_terminate
   
   clear
   echo "Extracting SKsh manpages.....\n"
   [ -f man:sksh/echo.MAN ] && chmod +wd man:sksh/*
   bin/doc2man doc/Reference.doc doc/ExtCmds.doc

   if [ -n "$jm_installed" ]
   then
      echo "\nExtracting JM manpages.....\n"
      bin/doc2man jm/Docs/JMRef.doc
   fi

   echo
   line yn "Done extracting SKsh manpages.  Press return > " 0
else
   echo
   line yn "Skipping manpage extraction.  Press return > " 0
fi

# -------------- step 9 -----------------------------------------------

clear
echo "${b}STEP NINE: manual steps -------------------------------------${e}"

echo "
Some manual steps may be necessary to finish installing SKsh.  A
list of these steps will be generated here."

read_dir "Please enter the file name which contains your AmigaDos " \
         "startup instructions" "$st_file" 'file'

st_file="$in_dir"

touch s:Shell-Startup
s1_file='s:shell-startup'

echo -n "Press return to continue: > "

read yn;

stepnum=1
rm -f "$ms_file" >nil:

clear
echo '\n--- Manual Steps Necessary for SKsh Installation --------------\n'
echo '\n--- Manual Steps Necessary for SKsh Installation --------------\n' \
     >> "$ms_file"

ss_cont=$(cat "$st_file")    # store away startup-sequence in variable
s1_cont=$(cat "$s1_file")    # s:shell-startup

if check_ss 'echo "$ss_cont" | grep -is "JM -init"' \
   "Insert 'skshbin:JM -init' in $st_file _after_ the
      SetPatch line"
then true; else inc stepnum; fi

if check_ss 'echo "$s1_cont" | grep -is "stack "' \
   "Insert 'stack 12000' (or larger) in $s1_file"
then true; else inc stepnum; fi

if check_ss '[ "$no_tdir" != "TRUE" ]' \
   "Insert 'makedir >nil: ram:T' in $st_file" \
   "Insert 'assign T: ram:T' in $st_file"
then true; else inc stepnum 2; fi

if check_ss '[ -m env: ]' \
   "Insert 'makedir >nil: T:env' in $st_file" \
   "Insert 'assign env: T:env' in $st_file"
then true; else inc stepnum 2; fi

if check_ss 'echo "$ss_cont" | grep -i "sksh: " | grep -si "assign "' \
   "Insert 'assign sksh: $init_dir' in $st_file"
then
   if check_ss 'echo "$ss_cont" | grep -i "sksh: " | grep -i "assign " | \
      grep -si "$init_dir"' \
      "modify the the 'assign sksh:' statement in $st_file
      to read 'assign sksh: $init_dir'"
   then true; else inc stepnum; fi
else inc stepnum; fi

if check_ss 'echo "$ss_cont" | grep -i "skshbin: " | grep -si "assign "' \
   "Insert 'assign skshbin: $bin_dir' in $st_file"
then
   if check_ss 'echo "$ss_cont" | grep -i "skshbin: " | grep -i "assign " | \
      grep -si "$bin_dir"' \
      "modify the the 'assign skshbin:' statement in $st_file
      to read 'assign skshbin: $bin_dir'"
   then true; else inc stepnum; fi
else inc stepnum; fi

if check_ss 'echo "$ss_cont" | grep -i "skshscr: " | grep -si "assign "' \
   "Insert 'assign skshscr: $scr_dir' in $st_file"
then
   if check_ss 'echo "$ss_cont" | grep -i "skshscr: " | grep -i "assign " | \
      grep -si "$scr_dir"' \
      "modify the the 'assign skshscr:' statement in $st_file
       to read 'assign skshscr: $scr_dir'"
   then true; else inc stepnum; fi
else inc stepnum; fi

if check_ss 'echo "$ss_cont" | grep -i "vmagic: " | grep -si "assign "' \
   "Insert 'assign vmagic: <dir containing view magic files>'
       in $st_file (use step $stepnum only if you wish
       to use the SKsh 'view' utility)"
then true
else inc stepnum; fi


if [ "$perf_man" = 'TRUE' ]
then
if check_ss 'echo "$ss_cont" | grep -i "man: " | grep -si "assign "' \
   "Insert 'assign MAN: $man_dir' in $st_file"
then
   if check_ss 'echo "$ss_cont" | grep -i "skshscr: " | grep -i "assign " | \
      grep -si "$scr_dir"' \
      "modify the the 'assign MAN:' statement in $st_file
       to read 'assign MAN: $man_dir'"
   then true; else inc stepnum; fi
else inc stepnum; fi
fi

mypath=$(tackon "$sksh_dir" 'sksh')

if check_ss 'echo "$ss_cont" | grep -i "resident " | grep -si "sksh"' \
   "Insert 'resident $mypath' in $st_file (this step
       optional, but recommended, as it will save memory
        when SKsh is invoked)"
then true
else inc stepnum; fi

if [ "$stepnum" -le 1 ]
then
   echo "\n** No manual steps necessary **\n"
   rm -f "$ms_file" >nil:
else
   echo "\nA listing of the manual steps has been placed in '$ms_file'\n"
fi

line yn "Press return to continue: > "

# -------------- done -------------------------------------------------

clear
echo "${b}DONE: installation complete ----------------------------------${e}"

echo "
The SKsh installation is complete.  If you performed step 3 (install
files), then the files in this directory have been copied to their
proper installation directories, and you may delete the files in this
directory.

${b}SKsh is freely distributable software.  It is copyrighted, and is
_not_ public domain.${e}  Please see UserMan.doc for copyright details.

${b}READ THE DOCUMENTATION!${e}
"

if [ -f "$ms_file" ]
then
   echo "Don't forget to perform the manual steps which are listed in the
'$ms_file' file.
"
fi

cleanup
ck_terminate
