#!/bin/ksh
#

#
# Options to pass to shell script xval.bp:
# If there are options you want to pass to xval.bp everytime
# it is invoked, define them here; otherwise set options=""
#
options="-t -o -tweak"

#
# More options to pass to shell script xval.bp:
# Replace the line(s) beginning "set -A" with a similar line
# defining the epochs, hiddenNodes, momenta, rates, seeds, or
# weights that you wish to use.
#
set -A epochs 20000
let numberOfEpochs=${#epochs[*]}
let iEpochs=0

set -A hiddenNodes 16
let numberOfHiddenNodes=${#hiddenNodes[*]}
let iHiddenNode=0

set -A momentum 95
let numberOfMomenta=${#momentum[*]}
let iMomentum=0

set -A rate 5
let numberOfRates=${#rate[*]}
let iRate=0

set -A seed 997
let numberOfSeeds=${#seed[*]}
let iSeed=0

set -A weight 1
let numberOfWeights=${#weight[*]}
let iWeight=0

function showUseage {
   cat << EOF

   Useage is

	${0##*/} -c <YES|NO> -f <file> -s <sets>

   where

      -c <YES|NO> is an optional parameter indicating whether or
      not .result, .test, and .train  files are to be removed

      -f <file> is a required parameter which specifies the file
      stem

      -s <sets> is a required parameter which specifies the number
      of cross-correlations to be done

EOF
}

function nextWeight {
   if (( numberOfWeights > 0 ))
   then
      if (( iWeight < numberOfWeights ))
      then
         optWeight="-w 0.${weight[$iWeight]}"
         let iWeight=iWeight+1
	 return 0
      else
         return 1
      fi
   elif (( iWeight == 0 ))
   then
      let iWeight=iWeight+1
      return 0
   else
      return 1
   fi
}

function nextSeed {
   if (( numberOfSeeds > 0 ))
   then
      if (( iSeed < numberOfSeeds ))
      then
         optSeed="-s ${seed[$iSeed]}"
         let iSeed=iSeed+1
	 return 0
      else
         return 1
      fi
   elif (( iSeed == 0 ))
   then
      let iSeed=iSeed+1
      return 0
   else
      return 1
   fi
}

function nextRate {
   if (( numberOfRates > 0 ))
   then
      if (( iRate < numberOfRates ))
      then
         optRate="-r 0.${rate[$iRate]}"
         let iRate=iRate+1
	 return 0
      else
         return 1
      fi
   elif (( iRate == 0 ))
   then
      let iRate=iRate+1
      return 0
   else
      return 1
   fi
}

function nextMomentum {
   if (( numberOfMomenta > 0 ))
   then
      if (( iMomentum < numberOfMomenta ))
      then
         optMomentum="-m 0.${momentum[$iMomentum]}"
         let iMomentum=iMomentum+1
	 return 0
      else
         return 1
      fi
   elif (( iMomentum == 0 ))
   then
      let iMomentum=iMomentum+1
      return 0
   else
      return 1
   fi
}

function nextHiddenNode {
   if (( numberOfHiddenNodes > 0 ))
   then
      if (( iHiddenNode < numberOfHiddenNodes ))
      then
         optHiddenNodes="-h ${hiddenNodes[$iHiddenNode]}"
         let iHiddenNode=iHiddenNode+1
	 return 0
      else
         return 1
      fi
   elif (( iHiddenNode == 0 ))
   then
      let iHiddenNode=iHiddenNode+1
      return 0
   else
      return 1
   fi
}

function nextEpoch {
   if (( numberOfEpochs > 0 ))
   then
      if (( iEpochs < numberOfEpochs ))
      then
         optEpochs="-e ${epochs[$iEpochs]}"
         let iEpochs=iEpochs+1
	 return 0
      else
         return 1
      fi
   elif (( iEpochs == 0 ))
   then
      let iEpochs=iEpochs+1
      return 0
   else
      return 1
   fi
}

if (( $# < 1 ))
then
   showUseage
else
   clean=NO
   file=
   sets=
   while getopts ":c:f:s:" option
   do
      case $option in
         c )
            clean=$OPTARG
            ;;
         f )
            file=$OPTARG
            ;;
         s )
            sets=$OPTARG
            ;;
         \? )
            showUseage
            exit 1
      esac
   done

   if [[ -z "$file" ]] || [[ -z "$sets" ]]
   then
      showUseage
      exit 1
   else
      options="$options -f $file -c $sets"
   fi

   echo "xlate -f $file -a bp"
   xlate -f $file -a bp

   let iWeight=0
   while nextWeight
   do
      let iSeed=0
      while nextSeed
      do
         let iRate=0
         while nextRate
         do
            let iMomentum=0
            while nextMomentum
            do
               let iHiddenNode=0
               while nextHiddenNode
               do
                  let iEpochs=0
                  while nextEpoch
                  do
		     echo "xval.bp $options $optEpochs $optHiddenNodes $optMomentum $optRate $optSeed $optWeight"
		     xval.bp $options $optEpochs $optHiddenNodes $optMomentum $optRate $optSeed $optWeight
		     if [ $clean = YES ]
                     then
                        rm -f $file.*.result.bp $file.*.test.bp $file.*.train.bp
                     fi
                  done
               done
            done
         done
      done
   done
fi
