# dofun

# dofun
# 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 110818 I replaced "source" with "." for improved
# POSIX compliance.

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

# Joseph Rosevear 140819 I replaced "unset" with "unset -f".  I did
# this with the intention of allowing use of SAM functions that begin
# with numerals (e.g., 140819aa.sam) which I like to do.  It seems
# unset unsets variables first if they exist, and if not then it looks
# for a function to unset.  This may perhaps have been a problem
# waiting to happen, but that is not why I'm making this change.  I
# found if I use "unset -f" then functions that start with a letter
# will be unset without complaining.  Before this change, they were
# still unset, but there was complaining.

# Ah.  No good.  ~/bree fails with a complaint when invoked from a menu
# that has a function that starts with a numeral.  But I'll keep the
# change, anyway for the other reason I mentioned.

# For the time being I'll just live with the restriction that sam
# functions (end with .sam and .f) cannot start with a numeral.  I'll
# probably just use a letter or other character prefix making nine
# characters.  E.g., _140819aa.sam.

# Joseph Rosevear 230513 I removed the stanza for funfun.

# Joseph Rosevear 240706 I added added declarations and definitions for
# local variables string and elements.

# The definition of string is taken from the previous version of this
# function definition, but the definition of elements is new and uses
# reverse.

# Joseph Rosevear 240707 I removed the use of reverse by making some
# code changes.

# I added new and blank lines for clarity.

# I added debug output.



dofun() {

   local LS_OPTIONS
   local string
   local elements

   string="$1"

   elements=`echo $string | tr ":" "\n" | tac`

   for sam_dofun_string in $elements; do

      for sam_dofun_file in `ls $sam_dofun_string/*.sam 2> /dev/null`; do

         if [ "`head $sam_dofun_file | grep 'OK SAM'`" != "" ]; then

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

               echo "in dofun: sourcing $sam_dofun_file"
            fi

            . $sam_dofun_file
         fi

         sam_function=`basename $sam_dofun_file | sed 's/\.sam//g' -`
         export -f $sam_function
         echo "unset -f $sam_function" >> $sam_temp_dir/unfun
      done
   done

   return
}
