#!/bin/csh -f
# findsnd : script to find soundfiles within a user's snd directories
# whose names include one or more character string arguments, and,
#  optionally, to play these soundfiles
# A.S. 4/10/97  revised 1/98 9, 1998 after Irix 6.5 broke and 7/99 for Linux

if ($#argv == 0) then
   echo 'findsnd syntax:'
   echo  '  findsnd [flag]  charstring  [charstring2]  [charstringN]'
   echo  '  where "charstring" is an ascii character string '
   echo  'Flag options:'
   echo  ' -play or -p : the soundfile matches  will be played.'
   echo  ' -all or -a : findsnd will list ALL files in your'
   echo  "   snd directories, including analysis files, rather"
   echo  "   than soundfiles only."
   echo  ' -ns : findsnd will list ONLY files that are NOT soundfiles'
   echo  ' -d : findsnd will list your snd disk directories'
   echo  ' -s : soundfile matches will be sorted by pitch abbreviation'
   echo  ' -sp or -ps: soundfile matches will be sorted by pitch abbreviation'
   echo  '   and then played (combines -s and -p flags)'
   echo  ' -as or -sa: all files (not just soundfiles) in your snd directories'
   echo  '  will be searched for matches, then sorted by pitch abbreviation'

   echo  'Examples: '
   echo  '    findsnd  vln '
   echo  " will display a list of all soundfiles within all of your"
   echo  ' /snd directories whose names include the character string >> vln <<'
   echo  '    findsnd  -d a1 water > listing '
   echo  " will list your snd directories, then all of your soundfiles whose"
   echo  " names include the character strings >> a1 << or >> water << and will"
   echo  " write all of this output into a file named >> listing << "
   echo  "    findsnd  -p  mix"
   echo  " will display a list of all of the user's soundfiles whose names"
   echo  " include the character string >> mix << and then will play"
   echo  " each of these soundfiles."
   echo  "    findsnd  -sp  flute contratenor"
   echo  ' will display a list of all of your "flute" and "contratenor"'
   echo  ' soundfiles, both sorted by pitch abbreviation, then play all'
   echo  ' of these soundfiles.'

   echo  "For more details, type :   man  findsnd "
   echo '    - - - - - - - '
endif

set PWD = `pwd`
@ ARG = ( 2 )  # command line argument counter
   set PLAYLIST = ''
   set LISTDIRS = ''
   set PLAY = "NO"
   set ALL = "NO"
   set SORT = "NO"
   set NONSOUND = "NO"

switch ($1)
case -play:
   set PLAY = "YES"
breaksw
case -p:
   set PLAY = "YES"
breaksw
case -all:
   set ALL = "YES"
#   set NONSOUND = "YES"
breaksw
case -a:
   set ALL = "YES"
#   set NONSOUND = "YES"
breaksw
case -ns:
   set NONSOUND = "YES"
breaksw

case -s:
   set SORT = "YES"
breaksw
case -sort:
   set SORT = "YES"
breaksw
case -sp:
   set SORT = "YES"
   set PLAY = "YES"
breaksw
case -ps:
   set SORT = "YES"
   set PLAY = "YES"
breaksw
case -as:
   set SORT = "YES"
   set ALL = "YES"
breaksw
case -sa:
   set SORT = "YES"
   set ALL = "YES"
breaksw
case -d:
   set LISTDIRS = "YES"
breaksw


default:
@ ARG = ( 1 )  # command line argument counter
breaksw
endsw

/bin/rm -f ~/.tmpfindsnd* >& /dev/null

# Create list of user snd directories:
   # Line below for SGI only
  #set SFDIRS = `ls -d $HOME_SFDIR ; ls  -R $HOME_SFDIR | grep \/snd | sed -e 's/://g'  | sed -e 's/No\ match//g' `
   # Line below for Linux only
  set SFDIRS = `ls  -R $HOME_SFDIR | grep \/snd | sed -e 's/://g'  | sed -e 's/No\ match//g' `
   if ($LISTDIRS == YES) then
      echo " COMMENT  Your /snd disk directories are:"
      ls -d $SFDIRS
   endif

set SOUNDFILES = ''
set OTHERFILES = ''
set OTHERFILETYPES = ''
cd $HOME  # This cd is really stupid; otherwise $NUMBER below includes ~
while ($ARG <= $#argv)
set i = ( $argv[$ARG] )

   foreach DIR (`echo $SFDIRS`)
     ls -d $DIR/*$i* >>& .tmpfindsnd2
   end

   cat .tmpfindsnd2 | grep -v No\ match > .tmpfindsnd
  /bin/rm -f .tmpfindsnd2

  set SIZE = `cat .tmpfindsnd | sed -e 's/No\ match//g' | wc -w`
  if ($SIZE == 0) then
      echo " COMMENT No files found matching character string  >> $i << "
   else

   foreach FILE (`cat .tmpfindsnd`)
      # 2 lines below, using "filetype" command, for SGI only
       #set FILETYPE = `filetype $FILE`
       #set SFTEST = `echo $FILETYPE | grep SoundFile | wc -w`
      # 2 lines below, using "file" command, for Linux only
       set FILETYPE = `file $FILE`
       set SFTEST = `echo $FILETYPE | grep audio | wc -w`
       if ( $SFTEST  != 0) then
         set SOUNDFILES = `echo $SOUNDFILES $FILE`
         echo $FILE >> .tmpfindsnd2  # list of soundfiles
       else
         set OTHERFILETYPES = `echo $OTHERFILETYPES $FILETYPE`
         set OTHERFILES = `echo $OTHERFILES $FILE`
         echo $FILE >> .tmpfindsnd3
         echo $FILETYPE >> .tmpfindsnd4
       endif
   end

   if ($NONSOUND == YES) then
      if (-e .tmpfindsnd3) then
        set NUM2 = ` wc -w  .tmpfindsnd3 |  sed -e 's/.tmpfindsnd3//g' `
        echo "    COMMENT $NUM2 non-soundfile match(es) found for character string >> $i << :"
      else
        echo "    COMMENT No non-soundfile matches found for character string >> $i << :"
      endif
      if (-e .tmpfindsnd4) then
        cat .tmpfindsnd4
      endif
#   endif
   else
      if (-e .tmpfindsnd2) then
       set NUM1 = `wc -w  .tmpfindsnd2 | sed -e 's/.tmpfindsnd2//g'`
          echo "    COMMENT $NUM1 soundfile(s) matching character string >> $i << :"
################$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         if ($SORT == YES) then
            /usr/local/bin/sortsflist .tmpfindsnd2  > .tmpfindsnd5
            mv .tmpfindsnd5 .tmpfindsnd2
          endif
        cat .tmpfindsnd2
        else
           echo "    COMMENT  NO soundfiles found matching character string >> $i << :"
       endif
     endif

     if ($ALL == YES) then
      if (-e .tmpfindsnd3) then
        set NUM2 = ` wc -w  .tmpfindsnd3 |  sed -e 's/.tmpfindsnd3//g' `
        echo "    COMMENT $NUM2 non-soundfile match(es) found for character string >> $i << :"
      else
        echo "    COMMENT No non-soundfile matches found for character string >> $i << :"
      endif
      if (-e .tmpfindsnd4) then
         if ($SORT == YES) then
    #    #    #    #    # sort non-soundfile files
   /bin/rm -r ~/.tmpsortsflist* >& /dev/null
   cat .tmpfindsnd4 | grep -v COMMENT > ~/.tmpsortsflist2

   foreach NOTE (`cat /usr/local/lib/notes | grep -v COMMENT`)
      grep $NOTE ~/.tmpsortsflist2 >> ~/.tmpsortsflist
   end

   foreach PITCHED (`cat ~/.tmpsortsflist`)
      cat ~/.tmpsortsflist2 | grep -v $PITCHED  > ~/.tmpsortsflist2
   end
   set PITCHEDNUM =  `cat ~/.tmpsortsflist | wc -w`
   @ PITCH = ( $PITCHEDNUM / 2 )
   set UNPITCHEDNUM =  `cat ~/.tmpsortsflist2 | wc -w`
   @ NOPITCH = ( $UNPITCHEDNUM / 2 )

   echo "COMMENT $PITCH files sorted by pitch:"
   cat ~/.tmpsortsflist
   if (-z ~/.tmpsortsflist2) then
     else
     echo "COMMENT $NOPITCH file(s) with no pitch abbreviation:"
     cat ~/.tmpsortsflist2
   endif
   /bin/rm -r ~/.tmpsortsflist* >& /dev/null
    #    #    #    #    # sort non-soundfile files
          else
             cat .tmpfindsnd4
          endif
      endif
 endif

   if ($PLAY == YES) then
       set PLAYLIST = `echo $PLAYLIST; cat  .tmpfindsnd2 | grep -v COMMENT`
   endif

/bin/rm -f ~/.tmpfindsnd* >& /dev/null
endif
@ ARG = ($ARG + 1)
echo ""
#     echo " COMMENT  -  -  -  -"
end
## if -play or -p flag included, play these soundfiles
if ($PLAY == YES) then   
  echo "Playing these soundfiles:"
    foreach SOUNDFILE (`echo $PLAYLIST`)
            # set DUR = `/usr/local/bin/sndinfo $SOUNDFILE | grep length: | sed -e 's/length:/duration:/g'`
            set DUR = `sfdur $SOUNDFILE | awk '{print $1}'`
     echo " Playing soundfile >> $SOUNDFILE <<  $DUR seconds"

      $DEFAULT_PLAYER  $SOUNDFILE
      sleep 1
    end
endif



