#!/bin/sh

# msub
# 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 230526 I changed to code to use only "type" to check
# for availability.

# I made some formatting changes.

# I replaced direct use of "$1" with local variable "name".

# I removed an initial "hello" and final "bye".

# I removed an invocation of "love" and the corresponding "bye".

# I added optional variable "place" and used it in one place instead of
# "level" to reference a location:

#     echo "bound $place/$name"  >> $Place/${name}.sam

# I added variable "Place" and used it in all the other (see above)
# instances instead of "level" to reference locations.  Place is
# defined from $place using eval.

# I added variables string and superstring and used with with the pad
# command to format the entry for menu.dat 

# Joseph Rosevear 230526 I removed the portion of the code used to
# compute superstring and replaced it with a call to function
# superstring.

# I made this function definition into a script.

# Joseph Rosevear 230530 I added code so this:

#    mkdir $Place/$name

# is performed only when $2 is "".

# and I added prompts to explain.

# Joseph Rosevear 230531 I replaced part of the code with a call to
# mcore.

# Joseph Rosevear 240515 I added use of libo as a flag when it is $4.

# I added the option of $2 and $3 being place holders when "-".

# Joseph Rosevear 240706 I made some sweeping changes. The code is now
# more consistent and more flexible.



# Terms.
# I need to output different lines of code depending on the task:

#    task is menu or "":

#       bound $1 $2 $3

#    task is library:

#       bound [\$level|$2] $1 $3

# Definitions.
Name="$1"
final="$3"
place="\$level"
eval Place="$place"

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

   name=`basename $Name`
fi

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

   task=menu
fi

case $task in

   menu)

#      target="${place}/${name}";
      target="$Name";
      other="$2";;
      
   library)
   
      other="$Name";

      case $2 in
      
         ""|-)
         
            target="$place";;
            
         *)

            target="$2";;
      esac;
      
esac

string="bound $target $other $final"



# Program starts here.
if [ "$name" != "" ]; then

   cat << DONE

Running:

   bound $Place
   type $name

to see if "$name" is already a command.

DONE

   bound $Place
   type $name

   echo ""
   echo "Does it appear to be available? y/n"

   read answer

   if [ "$answer" = "y" ]; then

      if ! [ -e "${Place}/${name}.sam" ]; then
 
         if ! [ -f "${Place}/${name}" ]; then

            echo ""
            echo "Making file ${Place}/${name}.sam"

            echo "#OK SAM"              > ${Place}/${name}.sam
            echo "${name}() {"         >> ${Place}/${name}.sam
            echo                       >> ${Place}/${name}.sam
            echo "   $string"          >> ${Place}/${name}.sam
            echo                       >> ${Place}/${name}.sam
            echo "   return"           >> ${Place}/${name}.sam
            echo "}"                   >> ${Place}/${name}.sam
 
            chmod 644 ${Place}/${name}.sam

            echo ""
            echo "Running upfun to activate new function ${name}."
            upfun

            if [[ ! (-d "${Place}/${name}") && \
                  ! ("$task" = "library") ]]; then

               cat << DONE
               
Dir ${Place}/${name} does not exist and task is not "library".

Making ${Place}/${name}.
DONE

               #mkdir ${Place}/${name}
               eval mkdir $Name

               echo ""
               echo "Using mzit to put a starter menu in it."
               $name; mzit; bye
            fi
   
            echo ""
            echo "Adding $name to the menu..."
            mcore "$name" >> ${Place}/menu.dat
  
         else

            echo ""
            echo "${Place}/${name} already exists, and it is a file."
            echo "Cannot make dir ${Place}/${name}."
         fi

      else

         echo ""
         echo "${Place}/${name}.sam already exists"
      fi

   else

      echo ""
      echo "Usage: msub <dir>"
   fi

   bye
fi
