# sam_core

# sam_core
# 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 100114 I changed/added some message.  Before a
# message made reference to a script in the application.  I don't want
# this code to be tied to an application in that way.

# I added a message that says the "Syntax is not correct (no arguments)."
# This message appears independantly of whether the temporary directory
# is available.

# I added a message that tells (if true) that the temporary directory
# is available.  This message is used by an application script that I
# wrote so now I can't change the message.  See note below.

# Joseph Rosevear 100114 I added code to make $sam_temp_dir if it
# doesn't exist.  Consequently I moved the "echo in sam_core" line up.

# Joseph Rosevear 110108 I fixed what I think was a bug regarding
# sam_message.  The code now checks for the existence of "$sam_message"
# and if it is executable it is invoked.  Before it checked it lacked
# the quotes and the "$".  I don't see how that could have been right.

# Joseph Rosevear 110818 I replaced "source" with "." for improved
# POSIX compliance.

# Joseph Rosevear 110818 I replaced "-a" with "-e" for improved POSIX
# compliance.

# Joseph Rosevear 110819 I replaced "declare -fx" with "export -f" for
# improved POSIX compliance.

# Joseph Rosevear 111220 I replaced the use of "bash" with "/bin/bash". 
# This is not POSIX conformant, but it works with --rcfile which avoids
# a problem I had in X.  That problem was that in X I sometimes could
# not open five SAM hosts.  Perhaps it as a memory problem, since
# without --rcfile I had to use double shells.

# Joseph Rosevear 190704 I replaced

#    echo in sam_core

# with

#    echo "in $BASH_SOURCE"

# Joseph Rosevear 230430 I changed this:

#    echo "bound $*" >> $sam_temp_dir/elusive

# to:

#    echo "$*" >> $sam_temp_dir/elusive

# Joseph Rosevear 230430 I changed it back.

# Joseph Rosevear 230509 I changed:

#    dofun $sam
#    dofun $sam_tool

# to:

#    dofun $sam_add

# Joseph Rosevear 230513 I removed two lines that initialize and modify
# file funfun.

# Joseph Rosevear 230925 I changed the winking SAM.



#echo in sam_core
echo "in $BASH_SOURCE"

# Make $sam_temp_dir if it doesn't already exist
if ! [ -d $sam_temp_dir ]; then

   echo "sam_core: Making ${sam_temp_dir}..."
   mkdir --parents $sam_temp_dir
fi

#Here we lock sam_core.lck in the applicable $sam_temp_dir
lockfile $sam_temp_dir/sam_core.lck
#Below we remove it being careful to remove it only once.  I think
#bad things could happen if it is removed more than once.  Timing...
#Another session could be started just before the second remove...

error_syntax=false

### Temporary directory $sam_temp_dir is in use
if [ -e $sam_temp_dir/in_use ]; then

   echo "sam_core: Temporary directory $sam_temp_dir already in use."

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

      error_syntax=true
   fi

   rm -f $sam_temp_dir/sam_core.lck

else

   # Joseph Rosevear 100114 Note I am using the reply below in my
   # application.  In particular, I'm using it in file sam1.  It looks
   # for "Temporary directory is available".  So do not change that.
   echo "sam_core: Temporary directory is available ($sam_temp_dir)."

   ### $1 is NOT ""
   if [ "$1" != "" ]; then

      echo "host" > $sam_temp_dir/in_use
      chmod 644 $sam_temp_dir/in_use
      rm -f $sam_temp_dir/sam_core.lck

      echo "#" > $sam_temp_dir/unfun
      chmod 644 $sam_temp_dir/unfun

      . $sam/dofun
      export -f dofun

      dofun $sam_add

      echo "#" > $sam_temp_dir/elusive
      chmod 644 $sam_temp_dir/elusive

      if [ "$sam_debug" != "" ]; then
         echo "echo in elusive" >> $sam_temp_dir/elusive
      fi

      if [ -r $HOME/.bashrc ]; then
         echo ". $HOME/.bashrc" >> $sam_temp_dir/elusive
      fi

#     echo "$*" >> $sam_temp_dir/elusive
      echo "bound $*" >> $sam_temp_dir/elusive


      if [ "$sam_debug" != "" ]; then
         echo before bash
      fi       

      # When sam_batch is set to "batch", then -c is used instead of
      # --rcfile.  Using -c is new.  I put it here so SAM would work
      # inside cron.  Joseph Rosevear 070221
      if [ "$sam_batch" = "batch" ]; then

         /bin/bash -c ". $sam_temp_dir/elusive"

      else

#        Joseph Rosevear 111223 This version below may later become an
#        option that can be selected by use of an environment variable. 
#        Currently it gives me a problem.  That problem is that I
#        cannot always open five hosts when running in X.  Perhaps that
#        is because this uses a double shell.  (The variable should
#        probably code for use of /bin/sh above also.)

#         /bin/sh -c ". $sam_temp_dir/elusive; /bin/sh"

         /bin/bash --rcfile $sam_temp_dir/elusive

      fi

      if [ "$sam_debug" != "" ]; then
         echo before rm
      fi

      rm $sam_temp_dir/in_use
   
      ### $sam_message DOES exist
      #if [ -x "$sam_message" ]; then

      #   $sam_message

      ### $sam_message does NOT exist
      #else

         echo "*** Thank you for using SAM ;~) ***"
      #fi

   ### $1 IS ""
   else

      error_syntax=true
      rm -f $sam_temp_dir/sam_core.lck
   fi 
fi

if [ "$error_syntax" = "true" ]; then

   echo "sam_core: Syntax is not correct (no arguments)."
fi
