#!/bin/bash

# ===========================================================
# Fetch and install ARRIBA.
#
#	http://www.arriba-hausarzt.de
#	http://www.arriba-hausarzt.de/material/software.html
#
# ===========================================================

JAR_VER="2.4.1"

# ===========================================================
DL_SOURCE_URL="http://www.arriba-hausarzt.de/downloads/arriba.jar"
DL_JAR_NAME="arriba.jar"
DL_TARGET_DIR="/tmp"
APP_NAME="arriba"
VERSIONED_JAR_NAME="${APP_NAME}-${JAR_VER}.jar"
INSTALL_BASE="/usr/local/bin"
WRAPPER_NAME=${DL_TARGET_DIR}/${APP_NAME}


# get jar
wget -c ${DL_SOURCE_URL} -P ${DL_TARGET_DIR}/
mv ${DL_TARGET_DIR}/${DL_JAR_NAME} ${DL_TARGET_DIR}/${VERSIONED_JAR_NAME}


# create wrapper
echo "#!/bin/sh"										> ${WRAPPER_NAME}
echo ""													>> ${WRAPPER_NAME}
echo "java -jar  ${INSTALL_BASE}/${APP_NAME}.jar \$@"	>> ${WRAPPER_NAME}
echo ""													>> ${WRAPPER_NAME}
echo "# ======================================="		>> ${WRAPPER_NAME}
chmod +x ${WRAPPER_NAME}


# install jar
echo "Sie müssen jetzt das Passwort für root eingeben (außer Sie SIND root):"
su -c "mv -vf ${WRAPPER_NAME} ${INSTALL_BASE}/${APP_NAME} && mv -vf ${DL_TARGET_DIR}/${VERSIONED_JAR_NAME} ${INSTALL_BASE}/${VERSIONED_JAR_NAME} && ln -f -s -v ${INSTALL_BASE}/${VERSIONED_JAR_NAME} ${INSTALL_BASE}/${APP_NAME}.jar"


# remove old jars ?
echo ""
echo -n "Alte ARRIBA-Versionen entfernen ? [j/N]: "
read REPLY
if test "${REPLY}" = "j" ; then

	OLD_JARS="/usr/bin/arriba* /usr/local/bin/arriba-2.0.3.jar /usr/local/bin/arriba-2.0.4.jar /usr/local/bin/arriba-2.2.2.jar"

	echo "Sie müssen jetzt das Passwort für root eingeben (außer Sie SIND root):"
	su -c "rm -v -f ${OLD_JARS}"
fi


exit 0
