#!/bin/bash

# findsflib : script to find sflib soundfiles 
# whose names include one or more character string arguments, and,
#  optionally, to play these soundfiles
# A.S. 4/10/97, revised 9/21/01 for Linux WAVE sflib; revised 7/03 to add 96k sflib

if  ( [ $# == 0 ] ) ; then  # print usage summary
  echo 'findsflib: finds format sflib soundfiles whose names include input
string argument(s)'
   echo 'findsflib syntax:'
   echo  ' findsflib [-flag option(s)]  charstring  [charstring2]  [charstringN]'
   echo  '  where "charstring" is a character string '
   echo  ' Flag options:'
   echo  ' -44 : only the 44.1k sflib directories will be searched'
   echo  ' -96 : only the 96k sflib directories will be searched'
   echo  ' (By default findsflib searches BOTH the 44.1k and 96k directories)'
   echo  '  -p : the soundfiles will be played'
   echo  '  -a : ALL sflib files, including non-playable lpc, phase vocoder'
   echo  '  and sms analysis files will be included in the search'
   echo  '  -s : the soundfiles will be sorted by pitch, from lowest'
   echo  '   to highest pitch, followed by any soundfiles'
   echo '  whose names do not include a pitch abbreviation'
   echo 'Multiple flag arguments must be concatenated (in any order) into a single argument'
   echo 'preceded by a single - sign.'
   echo  'Examples: '
   echo  '    findsflib  drum '
   echo  " will display a list of all 96k and 44.1k sflib soundfiles whose"
   echo  ' names include the character string >> drum <<'
   echo  "    findsflib  -44p  tama  tawa    or   findsflib  -p44  tama  tawa"
   echo  " will display a list of all 44.1k sflib soundfiles whose names"
   echo  ' include the character strings "tama" or "tawa", and'
   echo  " then will play each of these soundfiles."
   echo  '    findsflib  -a .d3 .d4 > listing '
   echo  " will create a list of all sflib soundfiles and analysis files whose"
   echo  " names include the character strings >> .d3 << or >> .d4 <<, and will"
   echo  " write this list into a file named >> listing << "
   echo ""
   echo  "For full usage details, type :   man  findsflib "
   echo '    - - - - - - - '
   exit 0
fi

# set default values:
# the environment variables SFLIB and SFLIB96 are defined in the turnkey aliases file
    # NO 44SFLIB='/sflib'
    # NO 96SFLIB='/sflib96'
   # default flags:
    SF96=YES
    SF44=YES
    PLAYIT=NO
    SORT=NO
    ALL=NO
   # tmp files
   TMPFILE=~/.tmpfindsflib
   TMPFILE2=~/.tmpfindsflib2
   gp=~/tmpfindsflib3
   PLAYER="play"

# (1) separate flag arguments (which can be concatenated or given separately) from soundfile arguments
ARGS=$*
FLAGARGS=""
SFARGS=`echo $ARGS`
for i in $ARGS ; do  # user arguments
  if [ `echo $i | grep -e "\-"` ] ; then
    FLAGARGS=`echo $FLAGARGS $i`
  fi
done

for i in $FLAGARGS ; do
   SFARGS=`echo $SFARGS | sed -e s/$i//g`
done

# Check for bogus flag arguments:
FLAGCHECK=`echo $FLAGARGS | sed -e 's/-//g' | sed -e 's/44//g' | sed -e 's/96//g' | sed -e 's/p//g' | sed -e 's/a//g' | sed -e 's/s//g' | sed -e 's/\ //g'`
 if [ "${FLAGCHECK}" != "" ] ; then
  echo "ERROR: Incorrect flag argument >> $FLAGCHECK << "
  echo "  findsflib syntax:"
  echo  'findsflib [-flag option(s)]  charstring  [charstring2] [charstringN]'
  echo 'valid flag options are: -[44 96 p s a]'
  echo 'Example:  findsflib -44pa    (sets the 44, p and a flags)'
  exit 1
fi

# =================================================
# (2) Set flag options
for FLAGS in `echo $FLAGARGS` ; do
  if ( ( [ `echo $FLAGARGS | grep -e 44` ] ) && ( [ `echo $FLAGARGS | grep -e 96` ] ) ) ; then  # both 96 & 24 arguments given
     SF44=YES
     SF96=YES
  elif [ `echo $FLAGARGS | grep -e 44` ] ; then  # search only the 44.1k sflib directories
     SF96=NO
  elif [ `echo $FLAGARGS | grep -e 96` ] ; then  # search only the 96k sflib directories
     SF44=NO
  else
     SF44=YES
     SF96=YES
  fi # ---------------------
  if [ `echo $FLAGARGS | grep -e p` ] ; then
     PLAYIT=YES
     PLAYLIST=""
  else
     PLAYIT=NO
  fi  # ----------------------
  if [ `echo $FLAGARGS | grep -e s` ] ; then
      SORT=YES
   else
      SORT=NO
  fi  # ----------------------
  if [ `echo $FLAGARGS | grep -e a` ] ; then
      ALL=YES
   else
      ALL=NO
  fi  # ----------------------
done
   #echo SORT is $SORT  ALL is $ALL FLAGARGS is $FLAGARGS 
   #echo SF96 is $SF96 SF44 is $SF44FLAGCHECK is $FLAGCHECK 
# Create list of soundfile matches
rm -f $TMPFILE $TMPFILE2 

#cd ~  # This cd is dumb but necessary, else $NUMBER includes ~  NO LONGEWR NEEDED
for ARG in $SFARGS ; do
# ----------  96k 24 bit  sflib96 soundfiles: -----------------
  if [ $SF96 == "YES" ] ; then  # 44.1 k sflib directories:
    SFLIB96DIRS=`ls -R $SFLIB96 | grep sflib | sed -e 's/://g' | sed -e 's/\/snd//g'`
    for DIR in $SFLIB96DIRS ; do
       find $DIR/*$ARG* >> $TMPFILE 2> /dev/null
    done
          # for some reason duplicates show up in 96k directories
          # uniq removes duplicate lines
   sort $TMPFILE | uniq > $TMPFILE2 
   mv $TMPFILE2 $TMPFILE
 # if [ $ALL == "YES" ] ; then  # non-playable 96 k analysis files
 # Currently there are NO analysis files in the 96k directories
 # fi

   NUM=`wc -w $TMPFILE | awk '{print $1}'`
   if ( ! [ $NUM == 0 ] ) ; then
   #if [ $NUM == 0 ] ; then
      # echo "   COMMENT No 96k sflib files found matching character string  >> $ARG << "
   #else
       echo " COMMENT $NUM  96k sflib files matching character string >> $ARG << :"

      if [ $SORT == "YES" ] ; then
            sortsflist $TMPFILE  > $TMPFILE2
            mv $TMPFILE2 $TMPFILE
      fi
   fi
  if [ $PLAYIT == "YES" ] ; then
    PLAYLIST=`echo $PLAYLIST; cat $TMPFILE | grep -v COMMENT`
  fi
cat $TMPFILE
rm -f $TMPFILE # $TMPFILE2
  fi
# ----------  44.1k sflib soundfiles: --------------------------
  if [ $SF44 == "YES" ] ; then  # 44.1 k sflib directories:
    SFLIB44DIRS=`ls /sflib | grep -v anal | sed -e 's/No\ match//g'`
    for DIR in $SFLIB44DIRS ; do
       find /sflib/$DIR/*$ARG* >> $TMPFILE 2> /dev/null
    done
  if [ $ALL == "YES" ] ; then  # non-playable 44.1 k analysis files
      #find /snd/sflib/anal -name *$ARG* | sed -s 's^/snd/sflib/^/sflib/^g' #  >> $TMPFILE 2> /dev/null
      find /snd/sflib/anal -name *$ARG* | sed -s 's^/snd/sflib/^/sflib/^g' >> $TMPFILE 2> /dev/null
  fi

   NUM44=`wc -w $TMPFILE | awk '{print $1}'`
   if ( ! [ $NUM44 == 0 ] ) ; then
      #  echo "   COMMENT No 44.1k sflib files found matching character string  >> $ARG << "
   # else
       echo " COMMENT $NUM44  44.1k sflib files matching character string >> $ARG << :"
 #   fi
      if [ $SORT == "YES" ] ; then
            sortsflist $TMPFILE  > $TMPFILE2
            mv $TMPFILE2 $TMPFILE
      fi
   fi
  if [ $PLAYIT == "YES" ] ; then
    PLAYLIST=`echo $PLAYLIST; cat $TMPFILE | grep -v COMMENT`
  fi
cat $TMPFILE
rm -f $TMPFILE $TMPFILE2
  fi
# echo "COMMENT    ---------------------------"
done

  if [ $PLAYIT == "YES" ] ; then
      echo "Playing these soundfiles:"
      for SOUNDFILE in `echo $PLAYLIST` ; do
        $PLAYER $SOUNDFILE
      done
  fi
