#!/bin/bash
############################################
# Initialization script for setting up the installers.
# Usage: ./init-installer
#
# Changelog:
# rpm77 - moving from /var/tmp/dtb into /var/pgsql/dtb_config/
############################################

# Import definitions
CMD=$(basename $0)
if [ -f /opt/dtb/bin/_common_definitions ]; then
    . /opt/dtb/bin/_common_definitions
else
	logger -t $CMD -p ERROR -s "The /opt/dtb/bin/_common_definitions file does not exist, exiting the script..."
	exit 1
fi

plog "DTB STARTUP: rpm version: $(cat $DTB_CFGDIR/rpmversion)"

# Check, if the installers are already unpacked (having both install directories should be a good indicator)
if [ -d "$DTB_INSTALLDIR/DTBMaster" ] && [ -d "$DTB_INSTALLDIR/DTBServer" ]; then
    plog "DTB installers are already unpacked; Nothing to do."
    exit 0
fi

# Unpack the DTB Master installer
plog "Unpacking the DTB Master installer from /opt/dtb/DTBMaster/install into /var/opt/dtb/install."
rm -rf $DTB_INSTALLDIR/DTBMaster
mkdir -p $DTB_INSTALLDIR/DTBMaster
unzip $DTBMASTER_INSTALLSRC/$DTBMASTER_INSTALLPKG.zip -d $DTB_INSTALLDIR/DTBMaster
# Dirty hack for getting it running with defaults
cp $DTBMASTER_INSTALLSRC/*.iss $DTB_INSTALLDIR/DTBMaster/$DTBMASTER_INSTALLPKG
plog "Unpacking the DTB Master complete."

# Unpack the DTB Server installer
plog "Unpacking the DTB Server installer from /opt/dtb/DTBServer/install into /var/opt/dtb/install."
rm -rf $DTB_INSTALLDIR/DTBServer
mkdir -p $DTB_INSTALLDIR/DTBServer
unzip $DTBSERVER_INSTALLSRC/$DTBSERVER_INSTALLPKG.zip -d $DTB_INSTALLDIR/DTBServer
# Dirty hack for getting it running with defaults
cp $DTBSERVER_INSTALLSRC/*.iss $DTB_INSTALLDIR/DTBServer/$DTBSERVER_INSTALLPKG
plog "Unpacking the DTB Server complete."

# Set user/group
chown -R $DTB_USER:$DTB_GROUP $DTB_INSTALLDIR
