
# *****************************************************************************
#    This script checks the SKsh installation for the errors noted in the
#    initial message.
# *****************************************************************************

rel='1.6'
relnum=7
b="$_ANSI_P3"
e="$_ANSI_P1"

missing="ram:.missing"
invalid="ram:.invalid"
inpath="ram:.inpath"

function clean_up {
   rm "$missing" "$invalid" "$inpath"
   cd "$orig_dir"
   rm 'ram:crc'
}

function check_files {

   if [ ! -f "$1" ]
   then
      echo -c "$b" "ERROR: " "$e" "this file is missing: $1"
      echo "$1" >> "$missing"
   else
      crcval=$("$crccmd" -q "$1")

      if [ "$crcval" != "$2" ]
      then
         echo -c "$b" "ERROR: " "$e" "this file has an incorrect checksum: $1"
         echo "$1" >> "$invalid"
      fi

      if [ "$3" = "pure" -a ! -P "$1" ]
      then
         echo -c "$b" "WARNING: " "$e" "this file should have the pure bit set: $1"
      fi

      if [ "$4" = "path" -a -z $(which -s "$1") ]
      then
         echo -c "$b" "ERROR: " "$e" "this file must be in your SKsh path: $1"
         echo "$1" >> "$inpath"
      fi
   fi
}


function read_dir {

   local in_dir; in_dir=''

   cd $orig_dir

   while [ ! -d "$in_dir" -o -z "$in_dir" ]
   do
      echo
      echo "$1"
      echo -n "$2" "$b\[$3]$e ? > "

      read read_dir
      eval "in_dir = $read_dir"     # to get filename mapping right

      if [ -z "$in_dir" ]; then in_dir = "$3"; fi

      if [ ! -d "$in_dir" ]
      then
         echo "\n$read_dir is not a valid directory name."
      fi
   done

   export -l in_dir
}

clear

if [ $(version -s) -lt "$relnum" ]
then
   echo "This script must be run under SKsh $rel or later.  Please install"
   echo "SKsh $rel, run it, and then run this script."
   echo
   return
fi

echo "
This script will verify the installation of SKsh $rel.  It will report
missing files, files which do not have the correct checksum for this
version of SKsh, files which should have the pure bit set bit don't,
and files which should be in the search path but are not.  It will prompt
you for the location of various commands.  You must enter a valid
directory name which contains the files; for example, you might
enter \"c:\" (without the quotes) for the location of the main SKsh
binaries, and \"sys:bin\" for the location of the SKsh external commands.

This script is a reporting tool only; it will not change your filesystem.
"

if [ ! -m 'ram:' ]
then
   echo "This script requires the ram: disk to be temporarily mounted.  Also,"
   echo "you can assign ram: to another location.  In any case, please fix this"
   echo "and run the tool again."
   echo
   return
fi

if [ ! -m 'sksh:' ]
then
   echo -c "$b" "ERROR: " "$e" "The volume sksh: must be mounted.\n"
   return
fi

if [ ! -m 'vmagic:' ]
then
   echo -c "$b" "WARNING: " "$e" "the volume vmagic: should be mounted.\n"
fi

crccmd=$(which -s crc)

if [ -z "$crccmd" ]
then
   echo "The 'crc' command was not found in your SKsh search path.  It must"
   echo "be there for this script to run correctly."
   echo
   return
fi

cp "$crccmd" ram:
crccmd='ram:crc'

orig_dir="$PWD"

echo -n > "$missing"
echo -n > "$invalid"
echo -n > "$inpath"

# --- Find dir containing sksh binary... -------------------------------------

read_dir "Please enter the name of the directory which contains the SKsh and" \
         "Tiny_SKsh binaries" "c:"

echo -c "\n$_ANSI_US" "Checking SKsh binaries and init files...$_ANSI_UE" "\n"
cd "$in_dir"

check_files sksh            '     69880    5656931  614374586' 'pure' ''
check_files tiny_sksh       '     51980    4115062  614154319' 'pure' ''

initfile=FALSE
if [ -f sksh:.skshinit ]
then
   initfile=TRUE
   check_files sksh:.skshinit    '      3559     212415 1621908546' ''     ''
fi
if [ -f sksh:.skshinit.pp ]
then
   initfile=TRUE
   check_files sksh:.skshinit.pp '      3698     201992 1407416923' ''     ''
fi

if [ "$initfile" = 'FALSE' ]
then
   check_files sksh:.skshinit    '      3559     212415 1621908546' ''     ''
fi

if [ ! -f 'sksh:.skshrc' ]
then
   echo -c "$b" "ERROR: " "$e" "this file is missing: sksh:.skshrc"
   echo "sksh:.skshrc" >> "$missing"
fi

if [ -m 'vmagic:' ]
   then
   if [ ! -f 'vmagic:view.magic' ]
   then
      echo -c "$b" "WARNING: " "$e" "this file is missing: vmagic:view.magic"
      echo "vmagic:view.magic" >> "$missing"
   fi
fi

if [ -s "$missing" -a -s "$invalid" -a -s "$inpath" ]
then
   echo "\nSKsh binaries and init files are valid.\n"
fi

echo -n > "$missing"
echo -n > "$invalid"
echo -n > "$inpath"

# --- Find dir containing sksh external commands... --------------------------

read_dir "Please enter the name of the directory which contains the SKsh" \
         "external commands" "c:"

echo -c "\n$_ANSI_US" "Checking SKsh external commands...$_ANSI_UE" "\n"
cd "$in_dir"

check_files cat             '      2640     194202   76668405' 'pure' 'path'
check_files cmp             '      3092     227924  536903169' 'pure' 'path'
check_files cp              '      4532     362396 1917344647' 'pure' 'path'
check_files crc             '      2668     193827  536898298' 'pure' 'path'
check_files cut             '      3560     273017 1764043124' 'pure' 'path'
check_files du              '      3432     246205   76660976' 'pure' 'path'
check_files encr            '      3944     303081 1303786364' 'pure' 'path'
check_files fgrep           '      5316     389047   76640570' 'pure' 'path'
check_files find            '      6552     492821  460104968' 'pure' 'path'
check_files grep            '     11628     922091 1303697934' 'pure' 'path'
check_files head            '      3032     210930 1917366285' 'pure' 'path'
check_files indent          '      3312     230719 1917363458' 'pure' 'path'
check_files join            '      2920     216395  536901522' 'pure' 'path'
check_files num             '      2784     202126   76667273' 'pure' 'path'
check_files srun            '      2884     209028   76666287' 'pure' 'path'
check_files strings         '      2736     205271 1764033446' 'pure' 'path'
check_files tail            '      3588     259491   76659078' 'pure' 'path'
check_files tee             '      2880     222196 1303797919' 'pure' 'path'
check_files view            '      6200     511324 1303756615' 'pure' 'path'
check_files wc              '      3212     235859   76662454' 'pure' 'path'
check_files window          '      4840     347343 1994042642' 'pure' 'path'
check_files xd              '      3408     258472  536907533' 'pure' 'path'

if [ -s "$missing" -a -s "$invalid" -a -s "$inpath" ]
then
   echo "\nSKsh external commands are valid.\n"
fi

echo -n > "$missing"
echo -n > "$invalid"
echo -n > "$inpath"

# --- Find dir containing sksh documentation... ------------------------------

echo -n "\nDo you wish to check the documentation files [y/n] ? > "; read yn
echo

if [ "$yn" = 'y' -o "$yn" = 'Y' ]
then

read_dir "Please enter the name of the directory which contains the SKsh" \
         "documentation files" "MAN:"

echo -c "\n$_ANSI_US" "Checking SKsh documentation files...$_ANSI_UE" "\n"
cd "$in_dir"

check_files Addendum1.1.doc '      7777     547578  846267691' '' ''
check_files Addendum1.2.doc '     10166     719184  846287525' '' ''
check_files Addendum1.3.doc '     21523    1579874  232839043' '' ''
check_files Addendum1.4.doc '     27115    1931513  232884596' '' ''
check_files Addendum1.5.doc '     31858    2358666 1607759333' '' ''
check_files Addendum1.6.doc '     15417    1114231  994375034' '' ''
check_files Bugs.doc        '      1854     122801 1295233284' '' ''
check_files Diffs.doc       '      6201     443660 1314849289' '' ''
check_files ErrorCodes.doc  '      2405     151213 1753026272' '' ''
check_files ExtCmds.doc     '     51090    3342542 1063441335' '' ''
check_files Hints.doc       '      7882     563767  376124102' '' ''
check_files Install.doc     '      6732     452605 1677387440' '' ''
check_files MiscInfo.doc    '      5904     437686  740472207' '' ''
check_files ReadMe_1st.doc  '      5303     363846 1354028415' '' ''
check_files Reference.doc   '    152511    9929090 1701119901' '' ''
check_files TechNotes.doc   '      5451     404613 1267203720' '' ''
check_files TinySKsh.doc    '      3176     208417  774303476' '' ''
check_files UserMan.doc     '    102292    7283198 1676411641' '' ''
check_files View.doc        '     13037     952692 1088835487' '' ''

if [ -s "$missing" -a -s "$invalid" -a -s "$inpath" ]
then
   echo "\nSKsh documentation files are valid.\n"
fi

fi # end of doc file check

# --- Find dir containing stuff.sksh... -----------------------------------

echo -n "\nDo you wish to check the Stuff.sksh file [y/n] ? > "; read yn
echo

if [ "$yn" = 'y' -o "$yn" = 'Y' ]
then

read_dir "Please enter the name of the directory which contains the" \
         "Stuff.sksh file" "sksh:"

echo -c "\n$_ANSI_US" "Checking Stuff.sksh file...$_ANSI_UE" "\n"
cd "$in_dir"

check_files Stuff.sksh      '      8644     574008 1424267139' '' ''

if [ -f Stuff.sksh ]
then
   check_files Stuff.sksh.pp   '      6226     338179 1654313710' '' ''
fi

if [ -s "$missing" -a -s "$invalid" -a -s "$inpath" ]
then
   echo "\nStuff.sksh file is valid.\n"
fi

fi # end of Stuff.sksh file check


echo "\nSKsh installation checking complete."
clean_up
