#!/bin/sh
#
# @package      hubzero-submit-distributor
# @file         postinst
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

# postinst script for hubzero-submit-distributor
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

SUBMITDIR=/opt/submit
VARLOG=/var/log
HUBHOSTNAME=`hostname --fqdn`
DISTRIBUTORMAINS="distributor.py distributor.sh"
PEGASUSLOCALCONFIGS="pegasus_local.rc sites_local.xml"
DISTRIBUTORDFTFILES="aggregators appaccess environmentwhitelist identities managers monitors sites tools tunnels"

case "$1" in
    configure)
        # install-user-gridman
        if id gridman > /dev/null 2>&1; then
            IUID=`id --user gridman`
            IGID=`id --group gridman`
        else
            IUID="NONE"
            IGID="NONE"
        fi
        if [ "$IUID" = "NONE" ]; then
            adduser --system --disabled-password  \
                    --home /var/gridman           \
                    --force-badname --group --shell /bin/bash --gecos Gridman gridman
        fi
        # install-distrubtor-dat
        for dftFile in ${DISTRIBUTORDFTFILES} ; do \
                [ -e ${SUBMITDIR}/${dftFile}.dat ] || install --mode 0644 -D ${SUBMITDIR}/${dftFile}.dft ${SUBMITDIR}/${dftFile}.dat; \
        done
        sed -e "s/@HUBHOSTNAME@/${HUBHOSTNAME}/" --in-place ${SUBMITDIR}/monitors.dat
	if [ -f /etc/submit/daemons.conf ] ; then sed -e "s/@HOSTNAME@/${HUBHOSTNAME}/" --in-place /etc/submit/daemons.conf; fi
        # install-distributor-bin
        ${SUBMITDIR}/bin/update-known-hosts ${HUBHOSTNAME}
        # install-distributor-keys
	if [ -f ${SUBMITDIR}/.ssh/submit_rsa ] ; then \
		echo "submit RSA key already exists in ${SUBMITDIR}/.ssh/submit_rsa"; \
	else \
		printf "y\n\n" | ssh-keygen -q -t rsa -f ${SUBMITDIR}/.ssh/submit_rsa -N '' -C "submit@${HUBHOSTNAME}"; \
	fi
        chown gridman ${SUBMITDIR}/.ssh/submit_rsa ${SUBMITDIR}/.ssh/submit_rsa.pub
        rm -f ${SUBMITDIR}/etc/submit_rsa
        install -o gridman -g gridman --mode 0700 -d /var/gridman/.ssh
        cat ${SUBMITDIR}/.ssh/submit_rsa.pub >> /var/gridman/.ssh/authorized_keys
        chown gridman:gridman /var/gridman/.ssh/authorized_keys
        chmod 0600 /var/gridman/.ssh/authorized_keys
        # debian specifics
        ucfr hubzero-submit-distributor /opt/submit/.ssh/submit_rsa
        ucfr hubzero-submit-distributor /opt/submit/.ssh/submit_rsa.pub

        if [ ! -d ${VARLOG}/submit ]
        then
                mkdir --mode=0775 ${VARLOG}/submit
        fi

        if [ ! -d ${VARLOG}/submit/distributor ]
        then
                mkdir --mode=0750 ${VARLOG}/submit/distributor
        fi

        if [ ! -f ${VARLOG}/submit/distributor/distributor.log ]
        then
            touch ${VARLOG}/submit/distributor/distributor.log
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
