#!/bin/sh

# isam1
# Copyright (C) 2004, 2005, 2006, 2007, 2010, 2011, 2012, 2019 Joseph
# Rosevear

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



###############################################################################
### 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_distro/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" = "" ]; then
         $sam_pre_go/sam $host $*
      else
         # Define sam_task.
         if [ "$sam_task" = "" ]; then
         
            sam_task="SAM-${host}"
         fi
         
         # Define sam_iconic.
         if [ "$sam_iconic" != "" ]; then
         
            sam_iconic="-iconic"
         fi
         
         echo
         echo "Starting a new process in a new window, \"SAM-${host} on $sam_temp_base\"..."
         exec xterm $sam_iconic -ls -sb -fn 7x14 -geometry 80x35+80+35 -title \
         "$sam_task on ${sam_temp_base}:${host}/${limit}" -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
