#!/bin/sh

# dog
# Copyright (C) 2025 Joseph Rosevear, San Diego CA, USA.

# This file is part of a distribution SAM.

# SAM 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 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/>.



# Joseph Rosevear I wrote this script.

# 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
# ZombieSlack.  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.

# Joseph Rosevear 190235 I moved code that makes dog_name 644 into the
# dog block of the case statement.  Before it came after so it applied
# to both dog and named.  Now it applies to just dog.  That was needed,
# becase named is sometimes run by users, such as student, who don't
# own /tmp/dog_name.  Such a situation would cause dog to fail, because
# student cannot make file /tmp/dog_name 644 when it is not owned by
# student.

# The typical situation is this:  A user who is not student has a link
# in an email that he wants to follow.  Rather than click on the link
# he copies the link address to file /tmp/dog_name which he creates. 
# Then as user student he does:

#    export i_browser=chromium
#    dog

# The motivation for this use of dog is that it avoids clicking on
# links from emails as an ordinary user.  Doing this as student is
# safer, because student uses the Zombie method which keeps a clean
# home dir.

# I also:

#    Changed "rm" to "rm -f".

#    Added error checking after use of "rm -f".

#    Added a new case for $1 is "name".  This case removes and remakes
#    /tmp/dog_name, makes it 644, and opens it for editing using exed.

#    Added \" in the echo statements in a few places.

# Joseph Rosevear 200903 I changed the way that script_tools is
# referenced.

# Joseph Rosevear 210523 I changed, again, the way that script_tools is
# referenced.

# Joseph Rosevear 230515 O changed, again, the way that script_tools is
# referenced.  It was:

#    bound $level /mnt/joe_root/opt/scr_tool

# now

#    bound $level $lib/scr_tool

# Joseph Rosevear 230515 I changed the way splurge was added:

# was:

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

# now:

#    bound $level $lib/tool_bas/splurge;

# Joseph Rosevear 230515 I changed this invocation:

#    splurge9 $sam_tool/pony.spl $level/data/dog/$1.dat$2 > \
#    /tmp/dog.html;

# to:

#    splurge9 $lib/tool/pony.spl $level/data/dog/$1.dat$2 > \
#    /tmp/dog.html;

# Joseph Rosevear 240101 I changed the splurge9 invocation.

# Joseph Rosevear 240106 I changed the code to look for $1 in data not
# data/dog.  In particular I changed this:

#    $level/data/dog/$1

# to:

#    $level/data/$1

# Joseph Rosevear 240109 I removed normal mode, and I changed the
# invocation of net_or_rem so it doesn't define DISPLAY.

# Joseph Rosevear 240109 I removed some commented-out lines.

# Joseph Rosevear 240702 I made one change needed to accomodate a
# change in $1.  Before $1 defined file $level/data/$1, and this file
# was used by dog.  Now $1 defines the full path of the file used by
# dog.

# Joseph Rosevear 240724 I replaced "student" with "$i_user".

# Joseph Rosevear 241230 I simplified the invocation of net_or_rem by
# removing it from inside the su invocation.

# Joseph Rosevear 250108 I removed net_or_rem from inside the begin
# invocation.



# Define functions

# bail
bail() {

   echo Error.
#   cleanup

   exit 1
   return
}


# Add scr_tools to the PATH
bound $level $lib/scr_tool



# 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 -f /tmp/dog_name;
      result=$?; bad "\"rm /tmp/dog_name\" failed";
      echo Removed /tmp/dog_name;
   fi
   
   if [ -a /tmp/dog.html ]; then
   
      rm -f /tmp/dog.html;
      result=$?; bad "\"rm /tmp/dog.html\" failed";
      echo Removed /tmp/dog.html;
   fi
   
   exit;;
   
   # Remove file /tmp/dog_name, then remake it and open it for editing.
   name)
   
   if [ -a /tmp/dog_name ]; then
   
      rm -f /tmp/dog_name;
      result=$?; bad "\"rm /tmp/dog_name\" failed";
      echo Removed /tmp/dog_name;
   fi
   
   touch /tmp/dog_name;
   
   chmod 644 /tmp/dog_name;
   
   # Bail if the above fails.
   result=$?; bad "\"chmod 644 /tmp/dog_name\" failed.";

   # Open /tmp/dog_name for editing.
   exed /tmp/dog_name;;

   # 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 $lib/tool_bas/splurge;

   splurge9 $lib/tool/pony.spl $1 > \
   /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.";

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


echo purpose is $purpose   

net_or_rem `cat /tmp/dog_name`
