#OK SAM

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

# Joseph Rosevear 240101 I replaced the part of this script that writes
# the contents to $1 with a single line that makes a symlink to
# $lib/tools/view_it with the name $1.

# Joseph Rosevear 240101.  I fixed a bug.

# Joseph Rosevear 240624 I replaced the lines which make a symlink with
# lines that make an executable wrapper.



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

#ln -s $lib/tool/view_it $level/$1

# Make a wrapper at $level/$1.
cat << DONE > $level/$1
#!/bin/sh

# This script was made by ment of Rosevear Software's SAM.tool library.



target="$1" view_it "\$@"
DONE

         # Give $level/$1 execute permission.
         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
