#!/bin/sh

# msub
# Copyright (C) 2003, 2004, 2005, 2006, 2007, 2023 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 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.



# Definitions.
name="$1"
place="$level"

if [ "$2" != "" ]; then

   place="$2"
fi

target="$place/$name"

if [ "$3" != "" ]; then

   target="$3"
fi 

eval Place="$place"



# 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 "   bound $target"    >> $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") && ("$2" = "") ]]; then

               echo ""
               echo "Dir $Place/${name} does not exist and \$2 is \"\"."
               echo "Making $Place/${name}."
               mkdir $Place/$name
#               eval echo doing: mkdir $target
#               eval mkdir $target

               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
