#OK SAM

# ment
# Copyright (C) 2003, 2004, 2005, 2006, 2007, 2011, 2019, 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 110821 I replaced the use of set with type, because
# set doesn't show functions in POSIX bash.

# Joseph Rosevear 191022 I changed the code to put "\$level" in the
# script that is made instead of "$level".

# Joseph Rosevear 230522 I removed the code that put "con_menu" in the
# generated file.

# Joseph Rosevear 230526 I added an invocation of superstring, then
# after this I replaced the use of "$1" with ${superstring}.

# Joseph Rosevear 230526 I changed this function definition to a
# script.

# I replaced the test for availability with a single use of the type
# command.



problem=false

if [ -a "$level/$1" ]; then
   
   echo ""
   echo "file $level/$1 already exists"
   problem=true

fi

if [ -f "$level/data" ]; then

   echo ""
   echo "$level/data already exists, and it is a file."
   problem=true

fi

if [ "$problem" = "false" ]; then

   hello

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

   cat << DONE

Running:

   type $1

to see if $1 is already a command.
DONE

      type $1

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

      read answer

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

         echo '#!/bin/sh'                              >  $level/$1

         ###
cat << done >> $level/$1

if [ "\$1" != "-" ]; then

   sam_edit \$level/data/$1.dat\$1 \$2 

else

   sam_edit \$level/data/$1.dat \$2 
   
fi
done
         ###
         
         chmod 755 $level/$1

         if ! [ -e "$level/data" ]; then

            echo ""
            echo "Dir $level/data does not exist."
            echo "Making it..."
            mkdir $level/data

         fi
   
         # Compute superstring.
         mkstring "$1"

         echo ""
         echo "Adding $1 to the menu..."
         echo -e "\n${superstring}***made by ment--please edit***" >> $level/menu.dat
  
      else

         echo ""
         echo "Exiting..."

      fi

   else

      echo ""
      echo "Usage: ment <new menu entry>"

   fi

   bye
fi
