#!/bin/sh

# sam1
# 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 100113 I made this from sam1-5.  It is essentially
# the same code with the addition of a bit of generalization.  So I
# kept the previous copyright dates in the notice.  The limit on the
# looping is general bit.  Note that this file is now called "sam1."
# This is a bit confusing, because there was a file sam1 which I had
# previously renamed to "sam".

# Joseph Rosevear 100113 I removed this line:
#
#    echo "This, the old process, will be frozen until you exit from the new one."
#
# I think it was confusing.  The process doesn't have to stay frozen,
# anyway.  The user can invoke with & or use bg.

# Joseph Rosevear 100114 I added "&" to the end of a line making this:
#
#    exec xterm -ls -sb -fn 7x14 -geometry 80x35+80+5 -title \
#    "SAM-${host} on $LOGNAME" -e $sam_pre_go/sam $host $* &
#
# Now we don't get a hung frozen window after doing "sudo ~/bree" or
# "~/breekit".  The "sudo ~/bree" wasn't so bad, because cntrl-z would
# fix it, but "~/breekit" made two frozen windows which would lock up
# and need killing if you tried to use cntrl-z.  That problem is fixed
# now.

# Joseph Rosevear 100114 I changed the "all are in use" message.

# Joseph Rosevear 110116 I replaced $LOGNAME with $sam_temp_base. 
# $LOGNAME wasn't working correctly with the new version of bree that
# doesn't use sudo.  $sam_temp_base gives useful information to the
# user, anyway.

# Joseph Rosevear 110410 I changed "-geometry 80x35+80+5" to "-geometry
# 80x35+80+35".  I didn't like where the window was opening.

# Joseph Rosevear 111231 I replaced #!/bin/sh with #!/bin/bash.

# Joseph Rosevear 120808 I changed the prgama back to #!/bin/sh,
# because due to changes in SAM the sh shell will work fine.

# Joseph Rosevear 191102 I made this (isam1) from sam1.  In "isam1" the
# prefixed "i" is for "iconic".  This script is the same as sam1,
# except it makes an iconic xterm.

# Joseph Rosevear 191102 I changed the -title specification for xterm. 
# Now the title shows the value environment variable "task".

# Joseph Rosevear 191103 I added use of variables sam_task and
# sam_iconic.  Note that it is convenient to define these optional
# variables on the command line like this:

#    sam_task=Bob sam_iconic=true /mnt/150605aa2/begin

# But it is for a different use that I added them.  I made a script

#    /mnt/joresorc/iftask

# That means "Iconic FUMC task".  That script, in turn, invokes a task
# in "pdata; fumcsd".  It does it like this:

#    sam_task=$* sam_iconic=true $sam_root/begin "pdata; fumcsd; tasks; $*"

# What I did is handy, because it adds the ability to make a specially
# labeled xterm for a specific task which opens inconified.  Yet it is
# optional; if you omit the variable definitions, then you get a
# behavior only trivially different from before (the format of the
# window label changed, but has the same information).

# I was motivated to do this by three things:  1.  I had long wished
# for a way to have specially labeled xterms.  The default labeling was
# confusing.  2.  For aesthetic and social reasons I want to keep the
# screen clean and uncluttered.  Thus the inconified windows.  But
# iconified windows need to be made unique; I can no longer depend on
# the placement of the icon to identify them.  Thus the special labels. 
# 3.  I want to work more efficiently.  These changes I think will alow
# that.

# Joseph Rosevear 230605 I got rid of variable sam_iconic and replaced
# it with xterm_option.  Variable xterm_option is not set or changed by
# this script, and I moved it to the end of the xterm invocation where
# it can be used to override the options which I coded in the script. 
# This it can be used in other ways besides just:

#    xterm_opt=-iconic

# Joseph Rosevear 240109 I changed this:

#    if [ "$DISPLAY" = "" ]; then

# to:

#    if [[ ("$DISPLAY" = "") || ("$sam_flat" = "flat") ]]; then

# Joseph Rosevear 240806 I added code to make the xterm open similarly
# to what /usr/bin/uxterm does.  To do this I added this near the
# beginning of the invocation:

#    -class UXTerm

# and this near the end:

#    -u8

# Joseph Rosevear 240806 I removed:

#    -fn 7x14

# and replaced it with:

#    -fa 'Monospace' -fs 12

# Joseph Rosevear 240806.  OK I changed it to:

#    -fa 'DejaVu Sans Mono' -fs 9

# Joseph Rosevear 240806.  I changed the tail end of the invocation
# from:

#    -e $sam_pre_go/sam $host $* &

# to:

#    -e $sam_pre_go/sam $host $*&

# Joseph Rosevear 240808 I replaced pieces of the hard coded xterm
# invocation with uses of variables scroll, font and geometry.

# Joseph Rosevear 240812 I replaced:

#    scroll
#    font
#    geometry

# with: 

#    gui_scroll
#    gui_font
#    gui_geometry



###############################################################################
### set sam_pre_go to location of the SAM Go files.  That is very likely the  #
### directory this file is in, but not necessarily.  There is no need to      #
### export this variable.                                                     #
###                                                                           #
sam_pre_go=$sam_root/go       # The location of the SAM Go files              #
###############################################################################

done=false
host=0
limit=$1
shift

if [ "$limit" = "" ]; then

   echo "sam1-n: \$limit is undefined"
   exit
fi

#loop over $limit hosts
while [ "$host" -lt "$limit" ]; do

   host=`expr $host + 1`
   if [ \( "$done" = "false" \) -a \
        \( "`$sam_pre_go/sam $host | grep \
        \"Temporary directory is available\"`" != "" \) ]; then 

      if [[ ("$DISPLAY" = "") || ("$sam_flat" = "flat") ]]; then

         $sam_pre_go/sam $host $*

      else

         # Define sam_task.
         if [ "$sam_task" = "" ]; then
         
            sam_task=SAM
         fi
         
         cat << DONE

Starting a new process in a new window, "SAM-${host} on $sam_temp_base"...
DONE

         #exec xterm -class UXTerm -u8 -ls -sb \
         exec xterm $gui_scroll \
         $gui_font \
         $gui_geometry \
         -title "$sam_task on ${sam_temp_base}:${host}/${limit}" \
         $xterm_opt -e $sam_pre_go/sam $host $*&
      fi 

      done=true
   fi
done
   
if [ "$done" = "false" ]; then
   echo
   echo "All SAM hosts (1 thru $limit) are unavailable."
   echo "Either they are all in use or there is a problem with"
   echo "\$sam_temp_base ($sam_temp_base)."
fi
