#!/bin/bash

# SpringBoot profile: dev or production
PROFILE="default" # debug = h2 db
#PROFILE="prod"   # production = postgresql

JAVA_OPTS="-Xms128m -Xmx512m -Dspring.profiles.active=$PROFILE"

APP_CMD="java $JAVA_OPTS -jar doi-csv-trunk.jar --v=3"


PROJECT=AMMA-CATCH

CONF=../conf/$PROJECT
INPUT=$CONF/inputs/
TMP=../tmp/$PROJECT

RES=../resources
XSL=$RES/xsl

rm -rf $TMP
mkdir -p $TMP


# 0. GetRecords from bd.amma-catch.org:

# Note: if amma-catch is DOWN (09/2016)
#cp /home/bourgesl/dc/osug-doi/src/test/resources/getRecords.csv $TMP/

wget -O $TMP/getRecords.xml "http://bd.amma-catch.org/amma-catchWS2/WS/csw/default?service=CSW&request=GetRecords&version=2.0.2&typenames=csw:Record&resulttype=results&maxrecords=100&elementsetname=summary&outputschema=http://www.isotc211.org/2005/gmd"

xsltproc -o $INPUT/getRecords.csv --stringparam DOI_SUFFIX_START "$PROJECT" $XSL/csw2txt.xsl $TMP/getRecords.xml

if [ $? -ne 0 ]; then
     echo "step 0: xsltproc failed"
     exit 1
fi


# 1. Process inputs (datasets and partial) CSV to generate DOI documents and landing pages:
$APP_CMD --action=process --project=$PROJECT

if [ $? -ne 0 ]; then
  echo "step 1: java failed"
  exit 1
fi


echo "done."