#!/bin/sh

################################################################################
# Run after installing the main package files. This script lays down any
# required folders, install notes and links that may be needed by the software.
#
# Code42 Software Inc, Minneapolis, MN
# http://www.code42.com/
# 
# Copyright (c) 2008 Code42 Software, All Rights Reserved
################################################################################

mkdirFailed() {
  echo "ERROR: Failed to create required folder ${1}. Must exit."
  exit 1
}

APP_BASENAME=CrashPlan
TARGETDIR=${PKG_INSTALL_ROOT}/opt/sfw/crashplan
ENGINE_SCRIPT=${TARGETDIR}/bin/CrashPlanEngine
DESKTOP_SCRIPT=${TARGETDIR}/bin/CrashPlanEngine
SERVICE_XML=${TARGETDIR}/bin/crashplan.xml
LOG_DIR=${TARGETDIR}/log
MANIFESTDIR=${PKG_INSTALL_ROOT}/opt/sfw/var/crashplan
BINSDIR=${PKG_INSTALL_ROOT}/opt/sfw/bin
JAVACOMMON=`which java`
NOW=`date +%Y%m%d`

echo "log dir: $LOG_DIR"
mkdir $LOG_DIR
chmod 777 $LOG_DIR

echo ""

# The asset .defaults will be converted to a base install.vars file.  
# Append various run-time values to it here.
echo "TARGETDIR=${TARGETDIR}" >> ${TARGETDIR}/install.vars
echo "BINSDIR=${BINSDIR}" >> ${TARGETDIR}/install.vars
echo "MANIFESTDIR=${MANIFESTDIR}" >> ${TARGETDIR}/install.vars
echo "JAVACOMMON=${JAVACOMMON}" >> ${TARGETDIR}/install.vars
echo "INSTALLDATE=$NOW" >> ${TARGETDIR}/install.vars
echo "APP_BASENAME=$APP_BASENAME" >> ${TARGETDIR}/install.vars


# and the service xml paths
echo "adjusting service xml paths"
sed "s|.*/opt/sfw/crashplan/bin.*start.*|                exec=\'${ENGINE_SCRIPT} start\'|" ${SERVICE_XML} > ${SERVICE_XML}.new && mv ${SERVICE_XML}.new ${SERVICE_XML}
sed "s|.*/opt/sfw/crashplan/bin.*stop.*|                exec=\'${ENGINE_SCRIPT} stop\'|" ${SERVICE_XML} > ${SERVICE_XML}.new && mv ${SERVICE_XML}.new ${SERVICE_XML}

# update the configs for file storage
echo "adjusting manifest path"
mkdir -p ${MANIFESTDIR}
sed "s|<manifestPath>manifest</manifestPath>|<manifestPath>${MANIFESTDIR}</manifestPath>|g" ${TARGETDIR}/conf/default.service.xml > ${TARGETDIR}/conf/default.service.xml.new && mv ${TARGETDIR}/conf/default.service.xml.new ${TARGETDIR}/conf/default.service.xml

echo "creating identity path"
mkdir -p /var/lib/crashplan

##
## Installation is complete. Below this line post final messages to the user.
##


# call out the "service has been started" by creating a pause
echo ""
echo "${APP_BASENAME} has been installed."
#echo ""
#echo -n "Press Enter to complete installation. "
#read ENTER

echo ""
echo "Important directories:"
echo "  Installation:"
echo "    ${TARGETDIR}"
echo "  Logs:"
echo "    ${TARGETDIR}/log"
echo "  Default archive location:"
echo "    ${MANIFESTDIR}"

echo ""
echo "Start Script (run as root):"
echo "  ${TARGETDIR}/bin/CrashPlanEngine start|stop"
#echo "  ${GUISCRIPT}"

echo ""
echo "Start Desktop UI:"
echo "  ${TARGETDIR}/bin/CrashPlanDesktop"

echo ""
echo "You may connect a remote Desktop UI to this Service via port-forwarding"
echo "and manage it remotely. Instructions for remote management are in the"
echo "readme files placed in your installation directory:"
echo "  ${TARGETDIR}/doc"
echo ""
echo "To enable ${APP_BASENAME} as a service (as root):"
echo "  /usr/sbin/svccfg import ${TARGETDIR}/bin/crashplan.xml"
echo "  /usr/sbin/svcadm enable crashplan"

echo ""
echo "Thank you for installing ${APP_BASENAME} for Solaris."
echo ""

