#!/usr/bin/env bash
#####
#
#
#

. ./common-variables
PORT=8983

. ./solr-functions

ME=$(basename $0)

show_usage() { # display help massage
  cat <<EOF
usage:
 ${ME} [-d|--db] [-p|--file-path] [-m|--file-mask] [-w|--no-delete]
       [-s|--solrFieldType] [-x|--marcxml] [-a|--alephseq] [-t|--trimId]
       [-r|--defaultRecordType] [-v|--marcVersion]
       [-h|--help]

 -d, --db <name>        name of the database
 -p, --file-path <path> the directory of the input files
 -m, --file-mask <mask> file mask (e.g. *.mrc)
 -w, --no-delete        do not delete the old index
 -s, --solrFieldType <field type>: How Solr field should be named.
                        Possible values: 'marc-tags', 'human-readable', or 'mixed'
 -l, --limit <limit>    index only a limited number of records
 -x, --marcxml          the source is in MARCXML format
 -a, --alephseq         the source is in Alephseq format
 -t, --trimId           trim record identifiers
 -r, --defaultRecordType <record type> the default record type if the record's type is undetectable.
                        Possible values: BOOKS (default), CONTINUING_RESOURCES, MUSIC, MAPS,
                        VISUAL_MATERIALS, COMPUTER_FILES, MIXED_MATERIALS
 -v, --marcVersion <version> MARC version.
                        Possible values: MARC21 (default), OCLC, DNB, GENT, SZTE, FENNICA, UNIMARC
 -i, --ignorableRecords <condition> ignore records from the analysis 
 -h, --help             this help
EOF
  exit 1
}

if [ $# -eq 0 ]; then
  show_usage
fi

GETOPT=$(getopt -o d:p:m:ws::xatr:hv:l:i: \
  --long db:,file-path:,file-mask:,no-delete,solrFieldType:,marcxml,alephseq,trimId,defaultRecordType,help,marcVersion:,limit:,ignorableRecords: \
  -n ${ME} -- "$@")
eval set -- "$GETOPT"

DB=""
solrFieldType=mixed
defaultRecordType=BOOKS
marcVersion=MARC21
limit=""
DELETE=1
while true ; do
  case "$1" in
    -d|--db) DB=$2 ; shift 2;;
    -p|--file-path) FILE_PATH=$2 ; shift 2;;
    -m|--file-mask) FILE_MASK=$2 ; shift 2;;
    -w|--no-delete) DELETE=0 ; shift;;
    -s|--solrFieldType) solrFieldType=$2 ; shift 2;;
    -r|--defaultRecordType) defaultRecordType=$2 ; shift 2;;
    -v|--marcVersion) marcVersion=$2 ; shift 2;;
    -l|--limit) limit="--limit $2"; shift 2;;
    -i|--ignorableRecords) ignorableRecords="--ignorableRecords $2"; shift 2;;
    -x|--marcxml) marcxml="--marcxml" ; shift;;
    -a|--alephseq) alephseq="--alephseq" ; shift;;
    -t|--trimId) trimId="--trimId" ; shift;;
    -h|--help) show_usage ; shift ;;
    --) shift ; break ;;
    *) echo "Internal error!: $1" ; exit 1 ;;
  esac
done

echo "limit: $limit"

CORE=${DB}_dev

export SOLR=http://localhost:8983/solr/${CORE}

if [ "${DELETE}" == "1" ]; then
  echo "Delete records in ${CORE}"
  curl $SOLR/update -H "Content-type: text/xml" --data-binary '<delete><query>*:*</query></delete>'
fi

echo "Prepare schema"
prepare_schema $CORE

echo "Start indexing"
curl $SOLR/update -H "Content-type: text/xml" --data-binary '<commit/>'

cat <<EOT
running the command
---BEGIN
/usr/bin/java -cp $JAR de.gwdg.metadataqa.marc.cli.MarcToSolr \
  --solrUrl ${SOLR} \
  --solrFieldType $solrFieldType \
  --defaultRecordType $defaultRecordType \
  --marcVersion $marcVersion \
  $limit \
  $trimId \
  $marcxml \
  $alephseq \
  $ignorableRecords \
  ${FILE_PATH}/${FILE_MASK}
---END
EOT

/usr/bin/java -cp $JAR de.gwdg.metadataqa.marc.cli.MarcToSolr \
  --solrUrl ${SOLR} --solrFieldType $solrFieldType \
  --defaultRecordType $defaultRecordType \
  --marcVersion $marcVersion $limit $trimId $marcxml $alephseq $ignorableRecords \
  ${FILE_PATH}/${FILE_MASK}

echo "Start optimizing"
curl "$SOLR/update?optimize=true" -H 'Content-type: text/xml' --data-binary '<commit/>'

# dev -> production
echo "Swap ${CORE} to ${DB}"
swap_cores ${CORE} ${DB}

echo "indexing DONE"