#!/bin/bash
###################################################################
#                               THEMA
# Programmed by: David Huron         Date: April, 1996
# Copyright (c) 1996 David Huron
#
# Modifications:
# Date:         Programmer:       Description:
# ==================================================================
# June 1998     David Huron       * Implemented -a option
# 20 July 1999  Craig Stuart Sapp * Soft-wired database location.
#                                 * Changed last line to echo "$THEMES"
#                                   instead of echo $THEMES to preserve
#                                   newline characters.
# 11 Mar 2000   Craig Stuart Sapp * Adjusted -I option so M1 won't match M10
# 20 Mar 2001   Craig Stuart Sapp * Removed s/-/x/ replacement for -I option
#                                 * Removed s/+/X/ replacement for -I option
# 14 Nov 2001   Craig Stuart Sapp * Added escape characters for \{ and \}
#                                   in search pattern for egrep 
#                                   (grep with extended syntax).
#
#    This shell program is used to search thematic databases.
#

OPTIONS=""
HELP=""
TONIC=""
MODE=""
A_OPTION=""
C_INCIPIT=""
c_INCIPIT=""
d_INCIPIT=""
i_INCIPIT=""
I_INCIPIT=""
P_INCIPIT=""
p_incipit=""
R_INCIPIT=""
r_INCIPIT=""
S_OPTION=""
METER=""
TONIC=""
USAGE1="USAGE: thema -h                (Help Screen)"
USAGE2="       thema [-m|M] [-asz] [-C incipit] [-c incipit] [-d incipit]"
USAGE3="             [-i incipit] [-I incipit] [-P incipit] [-p incipit]"
USAGE4="             [-R incipit] [-T meter] [-t tonic]"
DUALCASE=1
DATABASE="NONE"


if [ "$#" -eq 0 ]
then
   echo "thema: ERROR: Option required for the \"thema\" command." 1>&2
   echo "" 1>&2
   echo "${USAGE1}" 1>&2
   echo "${USAGE2}" 1>&2
   echo "${USAGE3}" 1>&2
   echo "${USAGE4}" 1>&2
   exit
fi


#
# Process command-line arguments
#


while [ "$#" -ne 0 ]
do
   case "$1" in
   --)   shift
      break ;;
   -)   break ;;
   -*h*)
      HELP="ON"
      shift
      break ;;
   -[cCdiIpPrRTt])
      if [ x"$2" = x ]
      then
         echo "thema: ERROR: $1 option requires argument." 1>&2
         echo "" 1>&2
         echo "${USAGE1}" 1>&2
         echo "${USAGE2}" 1>&2
         echo "${USAGE3}" 1>&2
         echo "${USAGE4}" 1>&2
         exit
      else
         OPTIONS="${OPTIONS}$1"
         case "$1" in
            -c) c_incipit="$2" ;;
            -C) C_INCIPIT="$2" ;;
            -d) d_incipit="$2" ;;
            -i) i_incipit="$2" ;;
            -I) I_INCIPIT="$2" ;;
            -p) p_incipit="$2 " ;;
            -P) P_INCIPIT=`echo $2 | sed 's/ //g'` ;;
            -r) r_incipit="|[<>=]*$2" ;;
            -R) R_INCIPIT="$2" ;;
            -T) METER=`echo $2 | awk '{
                        gsub(" ","",$0); tolower($0)
                        if ($0 ~ /simpleduple/) gsub("simpleduple","duplesimple",$0)
                        if ($0 ~ /simpletriple/) gsub("simpletriple","triplesimple",$0)
                        if ($0 ~ /simplequadruple/) gsub("simplequadruple","quadruplesimple",$0)
                        if ($0 ~ /compoundduple/) gsub("compoundduple","duplecompound",$0)
                        if ($0 ~ /compoundtriple/) gsub("compoundtriple","triplecompound",$0)
                        if ($0 ~ /compoundquadruple/) gsub("compoundquadruple","quadruplecompound",$0)
                        print $0
                        }'` ;;
            -t) TONIC=`echo "$2=" | awk '{print toupper($0)}'` ;;
         esac
         shift 2
      fi ;;
   -*[cCdiIprRt]*)
      echo "thema: ERROR: Options expecting arguments must be specified separately." 1>&2
      echo "" 1>&2
      echo "${USAGE1}" 1>&2
      echo "${USAGE2}" 1>&2
      echo "${USAGE3}" 1>&2
      echo "${USAGE4}" 1>&2
      exit ;;
   -*)   
      OPTIONS="${OPTIONS}$1"
      case "$1" in
         -a) A_OPTION="ANCHOR" ;;
         -m) MODE="z" ;;
         -M) MODE="Z" ;;
         -s) S_OPTION="$1" ;;
         -z) DUPLICATE="NONE" ;;
      esac
      shift ;;
   *)   
      DATABASE=$1
      break ;;
   esac
done


# If no options were specified, print a help screen
if [ -n "$HELP" ]
then
   if [ -f ${HUMDRUM}/bin/helpscrn/thema.hlp ]
   then
      cat ${HUMDRUM}/bin/helpscrn/thema.hlp
   else
      echo "No help available on this command."
   fi
   exit
fi

# If -z option specified, remove duplicate "same" (s) from contour incipits,
# and/or remove duplicate values from diatonic incipits.

if [ "X$DUPLICATE" = XNONE ]
then
   c_incipit=`echo $c_incipit | awk '{gsub("ss*","s*",$0)
                  print "#[UDuds]*" $0}'`
   d_incipit=`echo $d_incipit | awk '{gsub("11*","1",$0)
                  gsub("22*","2",$0)
                  gsub("33*","3",$0)
                  gsub("44*","4",$0)
                  gsub("55*","5",$0)
                  gsub("66*","6",$0)
                  gsub("77*","7",$0)
                  print $0}'`
else
   c_incipit="$c_incipit"
   d_incipit="$d_incipit"
fi

if [ "X$S_OPTION" = X ]
then
   # Check to ensure that there is a thematic database available.

   if [ ! -f $DATABASE ]
   then
      echo "thema: ERROR: Thematic database not found: $1" 1>&2
      exit
   fi

   i_incipit=`echo $i_incipit | sed 's/-/m/g; s/+/p/g; s/ 0/p0/g; s/ //g'`
#   I_INCIPIT=`echo $I_INCIPIT | sed 's/-/x/g; s/+/X/g; s/D/d/g; s/a/A/g; s/p/P/g; s/ //g'`
   I_INCIPIT=`echo $I_INCIPIT | sed 's/D/d/g; s/a/A/g; s/p/P/g; s/ //g'`
# Changed 11 March 2000 To prevent matches for M1:
#    I_INCIPIT=`echo $I_INCIPIT | sed 's/-/x/g; s/+/X/g; s/D/d/g; s/a/A/g; s/p/P/g; s/ //g; s/$/[^0-9\\t]/'`
       
   #i_incipit=`echo $i_incipit | sed 's/+/p/g'`
   C_INCIPIT=`echo $C_INCIPIT | awk '{print toupper($0)}'`

   # Assemble the aggregate search pattern depending on whether the
   # search is to be anchored to the beginning of the theme.
   if [ "X$A_OPTION" = X ]
   then
      PATTERN="$MODE$TONIC.*\{.*$i_incipit.*#.*$c_incipit.*:.*$C_INCIPIT.*%.*$d_incipit.*\}.*$I_INCIPIT.*j.*$P_INCIPIT.*J.*$p_incipit.*M.*$METER"
   else
      PATTERN="$MODE$TONIC.*\{$i_incipit.*#$c_incipit.*:$C_INCIPIT.*%$d_incipit.*\}$I_INCIPIT.*j$P_INCIPIT.*J$p_incipit.*M.*$METER"
   fi

   #echo $PATTERN
   #exit
   # Avoid searching for the null pattern.
   #if [ "X$PATTERN" = "
   #echo "PATTERN: $PATTERN"
   THEMES=`egrep "$PATTERN" $DATABASE | sed 's/   .*//'`
else
   # Check to ensure that there is a structural-tones database available.

   if [ ! -f "$HUMDRUM/themes/classics.str" ]
   then
      echo "thema: ERROR: Structural-tones thematic database not found: $1" 1>&2
      exit
   fi
fi
echo "$THEMES"


