-#!/bin/bash
-if [ "$JDBC_DRIVER" == "" ]
-then
-JDBC_DRIVER=/usr/share/java/mysql-connector-java.jar
-#echo "JDBC_DRIVER environment variable not set. Assumed path: $JDBC_DRIVER"
-fi
-if [ "$GIGI_EXEC" == "" ]
-then
-GIGI_EXEC=/usr/share/java/gigi.jar
-#echo "GIGI_EXEC environment variable not set. Assumed path: $GIGI_EXEC"
-fi
-
-if [ "$1" == "start" ]
-then
- java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.Launcher
-elif [ "$1" == "signer" ]
-then
- java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.SimpleSigner
-elif [ "$1" == "reset-database" ]
-then
- java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.DatabaseManager
-elif [ "$1" == "fetch-locales" ]
-then
- java -cp $JDBC_DRIVER:$GIGI_EXEC org.cacert.gigi.util.FetchLocales
-else
- echo "Usage: gigi <option>"
-echo "start - starts gigi"
-echo "signer - starts the simple signer"
-echo "reset-database - resets the database"
-echo "fetch-locales - (re)fetch the localisation"
-fi
+#!/bin/sh
+
+: ${GIGI_CONF_DIR:=/etc/wpia/gigi}
+
+[ -r "$GIGI_CONF_DIR/launcher.conf" ] && . "$GIGI_CONF_DIR/launcher.conf"
+
+# assign default values
+: \
+ ${JDBC_DRIVER:=/usr/share/java/postgresql-jdbc4.jar} \
+ ${DNSJAVA:=/usr/share/java/dnsjava.jar} \
+ ${GIGI_EXEC:=/usr/share/java/gigi.jar} \
+ ${GIGI_DATA_DIR:=/var/lib/wpia-gigi}
+
+die() {
+ if [ $# -ge 1 ]; then
+ printf >&2 'Could not %s.\n' "$1"
+ fi
+ exit 1
+}
+
+cd "$GIGI_DATA_DIR" || die 'move to data directory'
+
+runGigiClass() {
+ class=$1
+ shift
+ if [ $# -ge 1 ]; then
+ java_opts="$1"
+ shift
+ fi
+ exec java $java_opts -cp "$JDBC_DRIVER:$DNSJAVA:$GIGI_EXEC" club.wpia.gigi.$class "$@"
+}
+
+command="$1"
+shift
+case "$command" in
+ start) runGigiClass Launcher ;;
+ debug) runGigiClass Launcher "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000" ;;
+ start-daemon)
+ exec 3</etc/gigi/conf.tar || die 'open config'
+ exec 4>>/var/log/wpia-gigi.log || die 'open log'
+ runGigiClass Launcher <&3 >&4 2>&1 &
+ echo $! > /var/run/wpia-gigi.pid
+ ;;
+ signer) runGigiClass util.SimpleSigner ;;
+ reset-database) runGigiClass util.DatabaseManager ;;
+ fetch-alexa) runGigiClass util.HighFinancialValueFetcher "" "$@" ;;
+ fetch-locales) runGigiClass util.FetchLocales "" "$@" ;;
+ signer-conf)
+ mkdir -p "$GIGI_DATA_DIR/config" || die 'create config directory'
+ cd "$GIGI_DATA_DIR/config" || die 'move to config directory'
+ tar x gigi.properties
+ ;;
+ *)
+ cat >&2 << 'EOF'
+Usage: gigi COMMAND [ARGUMENT...]
+
+Commands:
+ start - start gigi
+ debug - start gigi in debug mode (on port 8000, with config from stdin)
+ start-daemon - start gigi in background (using config from /etc/gig/config.tar)
+ signer - start the simple signer
+ signer-conf - extract config for simple signer (and reset-database) from the tar from stdin
+ reset-database - reset the database
+ fetch-alexa - fetch domains with high financial value from Alexa top 1 million list; required argument: target file, optional argument: number of domains to save
+ fetch-locales - fetch the localisation; optional argument: URL pattern to indicate download location
+EOF
+esac