#!/bin/sh

# sam1
# Copyright (C) 2004, 2005, 2006, 2007, 2010, 2011, 2012 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.)



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

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

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

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

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

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

# JHR 111231 I replaced #!/bin/sh with #!/bin/bash.

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



###############################################################################
### 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`

   echo
   echo host is $host
   echo done is $done
   echo \$sam_pre_go is $sam_pre_go
   echo running \$sam_pre_go/sam $host ...
   echo
   $sam_pre_go/sam $host

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

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