# dofun
# Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2014 Joseph
# Rosevear

# This file is part of The SAM Kernel.

# The SAM Kernel 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.

# The SAM Kernel 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/>.



# JHR 110818 I replaced "source" with "." for improved POSIX compliance.

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

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



dofun() {

   local LS_OPTIONS

   for sam_dofun_string in `echo $1 | sed 's/:/ /g' -`; do

      for sam_dofun_file in `ls $sam_dofun_string/*.f 2> /dev/null`; do
         if [ "`head $sam_dofun_file | grep 'OK SAM'`" != "" ]; then . $sam_dofun_file; fi
         sam_function=`basename $sam_dofun_file | sed 's/\.f//g' -`
         export -f $sam_function
         echo "unset -f $sam_function" >> $sam_temp_dir/funfun
      done

      for sam_dofun_file in `ls $sam_dofun_string/*.sam 2> /dev/null`; do
         if [ "`head $sam_dofun_file | grep 'OK SAM'`" != "" ]; then . $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
}
