
# Script:  Demo
#
# This script "Demo" will display graphic files previously created by
# "screen2file". This script uses "file2screen" to display the image on the
# screen from the previously created graphic files within the directory.
# This script will only display images on a Masscomp GA1000.
# "Demo" will execute two ways:  one uses the file "list" to determine what
# graphics files are to be displayed on the screen.  The other method is when
# there is no file "list" in the directory and then all the graphics files
# within that directory will displayed.  The directory to be utilized will be
# determined by either passing the "directory-name" which is located in
# directory "DEMO_DIR" or defaulting to your user id as the name of
# the directory within "DEMO_DIR".
#
#
# You may have to modify the following two variables according to your system:
DEMO_DIR="/plot/demo"
USR_SCRIPTS="/usr/scripts"
#
#
# Check if "$USR_SCRIPTS" is a directory.
if [ ! -d "$USR_SCRIPTS" ]
then
  echo "\"$USR_SCRIPTS\" is NOT a directory for variable \"USR_SCRIPTS\"."
  exit 1
fi
#
# Change directory to "$DEMO_DIR".
if [ -d "$DEMO_DIR" ]
then
  cd $DEMO_DIR
else
  echo
  echo "\"$DEMO_DIR\" is NOT a directory for variable \"DEMO_DIR\"."
  exit 1
fi
#
# If 1 or 2 arguments passed to script.
if test $# -eq 1 || test $# -eq 2 
then
# Assign argument 1 to variable "gp".
  gp=$1
# Provide message if gp is not a 0 or 1.
  if test "$gp" != "0" && test "$gp" != "1"
  then
    echo
    echo "\"$gp\" is NOT graphics processor \"0\" or \"1\"."
  fi
# Assign value to "user".
  if test $# -eq 2
  then
    user="$2"
  else
    user=`/usr/bin/whoami`
  fi
# Create directory "$user" if it does not exist in "$DEMO_DIR".
  if test ! -s "$DEMO_DIR/$user" && test ! -f "$DEMO_DIR/$user"
  then
    if [ "$user" = `/usr/bin/whoami` ]
    then
      if [ -w "$DEMO_DIR" ]
      then
        /bin/mkdir "$user"
        if [ $? -eq 0 ]
        then
          echo
          echo "Directory \"$user\" created in directory \"$DEMO_DIR\"."
          echo "Directory \"$user\" is your \"default\" directory."
          echo "Create your \"graphics files\" by executing \"screen2file\" while"
          echo "being in directory \"$DEMO_DIR/$user\"."
        else
          echo "Unsuccessful in creating directory \"$user\" in directory \"$DEMO_DIR\"."
        fi
      else
        echo
        echo "Unable to create directory \"$user\" in directory \"$DEMO_DIR\"."
      fi
      echo
      echo "usage:    Demo   gp#  [directory-name]" 
      echo "example:  Demo    0   [directory-name]"
      echo "example:  Demo    1   [directory-name]"
      echo "note:     [directory-name] must be located in directory: \"$DEMO_DIR\"."
      echo "note:     if [directory-name] is not provided then your user id will be used."
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
      exit 1
    else
      echo
      echo "Directory: \"$user\" does not exist."
      if test "$gp" != "0" && test "$gp" != "1"
      then
        echo
        echo "usage:    Demo   gp#  [directory-name]" 
        echo "example:  Demo    0   [directory-name]"
        echo "example:  Demo    1   [directory-name]"
        echo "note:     [directory-name] must be located in directory: \"$DEMO_DIR\"."
        echo "note:     if [directory-name] is not provided then your user id will be used."
      fi
    fi
    echo
    echo "Available directories in \"$DEMO_DIR\" are:"
    dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
    /bin/echo "$dirs" 
    exit 1
  else
    if [ ! -d "$DEMO_DIR/$user" ]
    then
      echo
      echo "\"$user\" is NOT a directory."
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
      exit 1
    fi
  fi
# If "gp" is NOT equal to graphics processor 0 and 1.
  if test "$gp" != "0" && test "$gp" != "1"
  then
    echo
    echo "usage:    Demo   gp#  [directory-name]" 
    echo "example:  Demo    0   [directory-name]"
    echo "example:  Demo    1   [directory-name]"
    echo "note:     [directory-name] must be located in directory: \"$DEMO_DIR\"."
    echo "note:     if [directory-name] is not provided then your user id will be used."
    echo
    echo "Available directories in \"$DEMO_DIR\" are:"
    dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
    /bin/echo "$dirs" 
    exit 1
  fi
#If "gp" equals 0 or 1.
  if test $gp -eq 0 || test $gp -eq 1
  then
    if [ -d $DEMO_DIR/$user ]
    then
      cd $DEMO_DIR/$user
    else
      echo
      echo "\"$DEMO_DIR/$user\" is NOT a directory."
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
      exit 1
    fi
# If file "list" exists in directory "$DEMO_DIR/$user".
    if test -f $DEMO_DIR/$user/list && test -s $DEMO_DIR/$user/list
    then
      files=`/bin/cat $DEMO_DIR/$user/list`
      shift $#
      lines=`/bin/wc -l $DEMO_DIR/$user/list | awk '{print $1}'`
      last=$lines
      if [ $lines -le 0 ]
      then
        echo
        echo "File:  \"$DEMO_DIR/$user/list\" does NOT have any lines."
        echo
        echo "Available directories in \"$DEMO_DIR\" are:"
        dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
        /bin/echo "$dirs" 
        exit 1
      else
        echo
        echo "Available directories in \"$DEMO_DIR\" are:"
        dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
        /bin/echo "$dirs" 
      fi
    else
      files=`/bin/ls -a`
      set `/bin/ls -a`
      if [ $# -le 2 ]
      then
        echo
        echo "There are no files in directory \"$DEMO_DIR/$user\"."
        echo
        echo "Available directories in \"$DEMO_DIR\" are:"
        dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
        /bin/echo "$dirs" 
        exit 1
      fi
      last=$#
      shift $last
      echo
      echo "NOTE:  You may create the file \"list\" in directory: \"$DEMO_DIR/$user\","
      echo "and edit in the names of the files you want displayed on the screen."
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
    fi
    displayed=0
    count=0
    for i in `/bin/echo "$files"`
    do 
      count=`expr $count + 1`
      if test "$i" != '.' && test "$i" != '..'
      then
        if [ -f "$i" ]
        then
          echo "$i" | grep -s 'R$'
          if [ $? -ne 0 ]
          then
            bytes=`ls -l "$i" | awk '{print $5}'`
            if [ $bytes -eq 1310400 ]
            then
              $USR_SCRIPTS/file2screen -r "$gp" "$i"
              if [ $? -eq 0 ]
              then
                displayed=`expr $displayed + 1`
                if [ $count -ne $last ]
                then
                  echo "[PRESS RETURN] to continue"
                  read return
                fi
              else
                echo "Execution of file: \"$i\" unsuccessful."
              fi
            fi
          fi
        else
          if [ -s "$i" ]
          then
            echo "File:  \"$i\" is NOT a regular file."
          else
            echo "File:  \"$i\" does NOT exist."
          fi
        fi
      fi
    done
  else
    echo
    echo "usage:    Demo   gp#  [directory-name]" 
    echo "example:  Demo    0   [directory-name]"
    echo "example:  Demo    1   [directory-name]"
    echo "note:     [directory-name] must be located in directory: \"$DEMO_DIR\"."
    echo "note:     if [directory-name] is not provided then your user id will be used."
    echo
    echo "Available directories in \"$DEMO_DIR\" are:"
    dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
    /bin/echo "$dirs" 
    exit 1
  fi
else
  echo
  echo "usage:    Demo   gp#  [directory-name]" 
  echo "example:  Demo    0   [directory-name]"
  echo "example:  Demo    1   [directory-name]"
  echo "note:     [directory-name] must be located in directory: \"$DEMO_DIR\"."
  echo "note:     if [directory-name] is not provided then your user id will be used."
  user=`/usr/bin/whoami`
# If "$user" does not exist then try to create it.
  if test ! -s "$DEMO_DIR/$user" &&  test ! -f "$DEMO_DIR/$user" 
  then
    if [ -w "$DEMO_DIR" ]
    then
      /bin/mkdir "$user"
      if [ $? -eq 0 ]
      then
        echo
        echo "Directory \"$user\" created in directory \"$DEMO_DIR\"."
        echo "Directory \"$user\" is your \"default\" directory."
        echo "Create your \"graphics files\" by executing \"screen2file\" while"
        echo "being in directory \"$DEMO_DIR/$user\"."
      else
        echo "Unsuccessful in creating directory \"$user\" in directory \"$DEMO_DIR\"."
      fi
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
      exit 1
    else
      echo
      echo "Unable to create directory \"$user\" in directory \"$DEMO_DIR\"."
      echo
      echo "Available directories in \"$DEMO_DIR\" are:"
      dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
      /bin/echo "$dirs" 
      exit 1
    fi
  else
    echo
    echo "Available directories in \"$DEMO_DIR\" are:"
    dirs=`/bin/ls -F "$DEMO_DIR"|/bin/grep '\/$'|/bin/sed 's/\///g'|/usr/bin/tr "\012" "\011"`
    /bin/echo "$dirs" 
  fi
  exit 1
fi
if [ $displayed -eq 0 ]
then
  echo
  echo "No files within directory \"$DEMO_DIR/$user\" were displayed"
  echo "on the screen."
  exit 1
fi
exit 0
