#!/bin/sh

# vet
# 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 230426 I wrote this script.



# Here is how to use this script:


# First, initialize:

#    /mnt/SAM_root/vet init


# Then test as needed.  The results of the testing will be stored for use by task finish.  Testing can be done
# manually:

#    test_x:

#       find -L [dir] -executable -maxdepth 1 -type b,c,p,f,l,s | (while read string; do /mnt/SAM_root/vet test_x $string; done)


#    test_f:

#       find -L [dir] -readable -maxdepth 1 -type b,c,p,f,l,s -name "*.f" | (while read string; do /mnt/SAM_root/vet test_f $string; done)


#    test_sam:

#       find -L [dir] -readable -maxdepth 1 -type b,c,p,f,l,s -name "*.sam" | (while read string; do /mnt/SAM_root/vet test_sam $string; done)


# Or it can be done with tasks that do the above for you:

#    dir_x:

#       /mnt/SAM_root/vet dir_x [path to dir] 


#    dir_f:

#       /mnt/SAM_root/vet dir_f [path to dir] 


#    dir_sam:

#       /mnt/SAM_root/vet dir_sam [path to dir] 


# Finally, view the results.  This can also be done between tests:

#    /mnt/SAM_root/vet report



# Definitions.
vet_log=/tmp/vet_log.txt
place=`dirname "$0"`
self=`basename "$0"`

task="$1"
subject="$2"
sub=`basename "$subject"`

case "$task" in

   test_f)
   sub=`basename "$sub" .f`;;
   
   test_sam)
   sub=`basename "$sub" .sam`;;
esac


# Take appropriate action.
case $task in

   init)
   rm "$vet_log";;
   
   dir_x)
   find -L $subject -executable -maxdepth 1 -type b,c,p,f,l,s | \
   (while read string; do
   
      $place/vet test_x $string;
   done);;
   
   dir_f)
   find -L $subject -readable -maxdepth 1 -type b,c,p,f,l,s -name "*.f" | \
   (while read string; do
   
      $place/vet test_f $string;
   done);;
   
   dir_sam)
   find -L $subject -readable -maxdepth 1 -type b,c,p,f,l,s -name "*.sam" | \
   (while read string; do
   
      $place/vet test_sam $string;
   done);;
   
   report)
   cat $vet_log;;

   test_x|test_f|test_sam)  
   type "$sub" >> "$vet_log";
   chmod ugo+rw "$vet_log";;
   
   finish)
   if [ -a "$vet_log" ]; then
   
      if [ -s "$vet_log" ]; then

         cat "$vet_log";
         exit 1;
      fi;
   fi;;

   *)
   echo "$self: Bad \$1 ($1).";;

esac
