#!/bin/bash

PRODDIR=/opt/products/dstar

if [ $EUID != 0 ] ; then
        echo "installer must be run as root"
        exit
fi

if [ ! -d $PRODDIR ] ; then
	echo "$PRODDIR not found"
	exit
fi

DSTARGW=/opt/products/dstar/dstar_gw
DSIPSVD=$DSTARGW/dsipsvd/dsipsvd.conf

if [ ! -r $DSIPSVD ]; then
    echo "$DSIPSVD not found"
    exit
fi

GW_CALL=`egrep -e '^ZR_CALLSIGN' $DSIPSVD | sed -e 's/ //g' | awk -F"=" '{print $2}' |sed -e 's/\r//'`

cd /tmp

if [ -d $PRODDIR/apache ] ; then
#G2 install
curl -O http://www.opendstar.org/.admins/.certs/dsgw-cert-install.tgz

cd $PRODDIR/apache

if [ ! -f conf/dstarssl.conf.prekey ] ; then
	echo "backing up files"
	cp conf/dstarssl.conf conf/dstarssl.conf.prekey
fi

tar xzf /tmp/dsgw-cert-install.tgz

#sed -e s/CALLSIGN/$GW_CALL/ <securesite/index.html.template >securesite/index.html

service dstar_gw stop
sleep 5
service dstar_gw start
fi

echo ""
echo "completed certificate installation for $GW_CALL"
echo "try http://$GW_CALL.dstargateway.org/"

exit 0
