#!/bin/sh

# dog
# Copyright (C) 2006, 2007, 2012, 2013, 2018, 2019 Joseph Rosevear, San
# Diego CA

# This file is part of an application of SAM for GNU/Linux Slackware
# known as SAM-GLS.

# SAM-GLS is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.

# SAM-GLS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# (Slackware is a registered trademark of Patrick Volkerding and
# Slackware Linux, Inc.)



# Joseph Rosevear 120727 I changed the code to look for $1.dat this
# way:
#
#    $level/data/dog/$1.dat
#
# it was
#
#    $level/data/$1.dat

# Joseph Rosevear 130519 I removed use of var3.  I changed the temp
# file to $env_scratch/dog.html.

# Joseph Rosevear 150107 I added "$2" after "$1.dat" to allow the use
# of
#
#   "dog notes 1"
# This is a syntax similar to "notes 1" which is already in use.

# Joseph Rosevear 180219 I changed the code to make it compatible with
# ZombieSlak.  It detects Zombie mode by the non-blank value of
# ZombieDisplay.  As part of this change I changed the place where
# dog.html is stored--now in /tmp instead of $env_scratch.

# Joseph Rosevear 180401 I changed $place to $sam_root, because that is
# what it is called now.

# Joseph Rosevear 181117 I changed the code to have two different
# purposes.  I call them dog and named.  Purpose dog is the original
# purpose and the code performs in a way which is equivalent to the
# original way.  Purpose named uses the name of an existing, normally
# remote, URL saved in file /tmp/dog_name.

# Note that purpose dog also uses file /tmp/dog_name which it didn't do
# originally.  This is new but equivalent to the original way.

# Joseph Rosevear 190216 I changed the way that the permissions are
# managed for dog.html and dog_name.  They are now made 644, and if
# that fails the code exits with an error message.

# And also errors when writing to dog_name and updating of dog.html
# will trigger failures.

# And I added a cleanup feature.  You clean up with "dog gone".  To
# code this I changed the if to a case.

# And I fixed the line that adds the splurge menu.  Before the change
# it reference /home/joe/config.  Bad.  student doesn't have that.
# I changed it to a path using $sam_root and not using config.

# Note that the functions bail and those of script_tools are new to
# this script.  I added them, so I could use function bad of
# script_tools.



# Define functions

# bail
bail() {

   echo Error.
#   cleanup

   exit 1
   return
}


# Script Tools
. /boot/tool/script_tools



# Determine purpose and take appropriate action:
case $1 in

   # Remove files /tmp/dog_name and /tmp/dog.html, then exit.
   gone)

   if [ -a /tmp/dog_name ]; then

      rm /tmp/dog_name;
      echo Removed /tmp/dog_name;
   fi
   
   if [ -a /tmp/dog.html ]; then
   
      rm /tmp/dog.html;
      echo Removed /tmp/dog.html;
   fi
   
   exit;;
   
   # Purpose is named.
   "")
   purpose=named;
   break;;
   
   # Purpose is dog.
   *)
   purpose=dog;
   
   # Bail if /tmp/dog_name or /tmp/dog.html exist and are not writeable
   if [ \( -a /tmp/dog_name \) -a \
      ! \( -w /tmp/dog_name \) ]; then
   
      echo /tmp/dog_name exists, but is not writeable.;
      bail;
   fi

   if [ \( -a /tmp/dog.html \) -a \
      ! \( -w /tmp/dog.html \) ]; then
   
      echo /tmp/dog.html exists, but is not writeable.;
      bail;
   fi

   echo /tmp/dog.html > /tmp/dog_name;
   # Bail if the above fails.
   result=$?; bad "Writing to /tmp/dog_name failed.";

   bound $level $sam_root/opt/jopublic/tool_bas/splurge;

   splurge9 $sam_tool/pony.spl $level/data/dog/$1.dat$2 > \
   /tmp/dog.html;
   # Bail if the above fails.
   result=$?; bad "Updating file /tmp/dog.html failed.";

   chmod 644 /tmp/dog.html;
   
   # Bail if the above fails.
   result=$?; bad "chmod 644 /tmp/dog.html failed.";;
esac

#chmod 644 /tmp/dog_name

# Bail if the above fails.
#result=$?; bad "chmod 644 /tmp/dog_name failed."

echo purpose is $purpose   

# Normal mode:
if [ "$ZombieDisplay" = "" ]; then

   net_or_rem `cat /tmp/dog_name`

# Zombie mode:
else

   echo "Login as student"
   su -c "DISPLAY=$ZombieDisplay $sam_root/begin \"net_or_rem \\\`cat /tmp/dog_name\\\`; bye\"" student
fi
