#!/bin/sh
#
# @package      hubzero-submit-monitors
# @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-monitors
#
# 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
MONITORTUNNEL=monitorTunnel.py
MONITORPROBE=monitorProbe.py
HUBHOSTNAME=`hostname --fqdn`
MANAGEIDENTITIES=manageIdentities.py

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
        # adduser --quiet gridman tunneler || true
        if [ "$IUID" = "NONE" ]; then
            adduser --system --disabled-password  \
                    --home /var/gridman           \
                    --force-badname --group --shell /bin/bash --gecos Gridman gridman
        fi
        # install-monitor-job
        chown gridman ${SUBMITDIR}/monitorJob.py
        chown gridman ${SUBMITDIR}/monitorJobSite.py
        chown gridman ${SUBMITDIR}/monitorJobSQL.py
        chown gridman ${SUBMITDIR}/monitorJobSiteSQL.py
        chown gridman ${SUBMITDIR}/jobCacheSimTools.py
        chown gridman ${SUBMITDIR}/jobExecuter.py
        chown gridman ${SUBMITDIR}/jobMetrics.py
        chown gridman ${SUBMITDIR}/jobProcessor.py
        chown gridman ${SUBMITDIR}/jobStageInput.py

        chgrp gridman ${SUBMITDIR}/monitorJobA.py
        chgrp gridman ${SUBMITDIR}/monitorJobP.py
        chgrp gridman ${SUBMITDIR}/monitorJobQ.py
        chgrp gridman ${SUBMITDIR}/monitorJobR.py
        chgrp gridman ${SUBMITDIR}/monitorJobS.py
        chgrp gridman ${SUBMITDIR}/monitorJobT.py
        chgrp gridman ${SUBMITDIR}/monitorJobU.py
        chgrp gridman ${SUBMITDIR}/monitorProbeD.py
        chgrp gridman ${SUBMITDIR}/monitorProbeQ.py
        chgrp gridman ${SUBMITDIR}/monitorProbeR.py
        chgrp gridman ${SUBMITDIR}/monitorProbeU.py
        chgrp gridman ${SUBMITDIR}/monitorTunnelA.py
        chgrp gridman ${SUBMITDIR}/monitorTunnelD.py
        chgrp gridman ${SUBMITDIR}/monitorTunnelI.py
        chgrp gridman ${SUBMITDIR}/monitorTunnelR.py
        chgrp gridman ${SUBMITDIR}/monitorTunnelT.py

	chown gridman:gridman /etc/submit/jobmonitor.conf

        # install-monitor-tunnel
        chown gridman ${SUBMITDIR}/${MONITORTUNNEL}
        # install-monitor-tunnel
        chown gridman ${SUBMITDIR}/${MONITORPROBE}
	# install-manage-identities
	chown gridman ${SUBMITDIR}/${MANAGEIDENTITIES}
        # initialize-monitor-job:
	mkdir -p ${SUBMITDIR}/DB
	chown gridman:root ${SUBMITDIR}/DB
	cd ${SUBMITDIR}/DB
	rm -f ${SUBMITDIR}/DB/monitorJob.db
        python ${SUBMITDIR}/monitorJobInitialize.py
	chown gridman:root ${SUBMITDIR}/DB/monitorJob.db
	chmod 0640 ${SUBMITDIR}/DB/monitorJob.db
	rm -f ${SUBMITDIR}/DB/monitorJob.sql
	touch ${SUBMITDIR}/DB/monitorJob.sql
	chown gridman:root ${SUBMITDIR}/DB/monitorJob.sql
	chmod 0640 ${SUBMITDIR}/DB/monitorJob.sql

	mkdir -p ${VARLOG}/submit
	chmod 0755 ${VARLOG}/submit
	mkdir -p ${VARLOG}/submit/monitors
	chmod 0755 ${VARLOG}/submit/monitors
	chown gridman ${VARLOG}/submit/monitors

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

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

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

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

	if [ ! -f /var/log/submit/monitors/jobCacheSimTools.log ]
	then
		touch /var/log/submit/monitors/jobCacheSimTools.log
	fi

	if [ ! -f /var/log/submit/monitors/jobExecuter.log ]
	then
        	touch /var/log/submit/monitors/jobExecuter.log
	fi

	if [ ! -f /var/log/submit/monitors/jobMetrics.log ]
	then
        	touch /var/log/submit/monitors/jobMetrics.log
	fi

	if [ ! -f /var/log/submit/monitors/jobProcessor.log ]
	then
        	touch /var/log/submit/monitors/jobProcessor.log
	fi

	if [ ! -f /var/log/submit/monitors/jobStageInput.log ]
	then
       		touch /var/log/submit/monitors/jobStageInput.log
	fi

        chmod 0644 ${VARLOG}/submit/monitors/monitorJob.log
        chown gridman:gridman ${VARLOG}/submit/monitors/monitorJob.log

        chmod 0644 ${VARLOG}/submit/monitors/monitorJobSite.log
        chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSite.log

        chmod 0644 ${VARLOG}/submit/monitors/monitorJobSQL.log
        chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSQL.log

        chmod 0644 ${VARLOG}/submit/monitors/monitorJobSiteSQL.log
        chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSiteSQL.log

	chmod 0644 /var/log/submit/monitors/jobCacheSimTools.log
	chown gridman:gridman /var/log/submit/monitors/jobCacheSimTools.log

	chmod 0644 /var/log/submit/monitors/jobExecuter.log
	chown gridman:gridman /var/log/submit/monitors/jobExecuter.log

	chmod 0644 /var/log/submit/monitors/jobMetrics.log
	chown gridman:gridman /var/log/submit/monitors/jobMetrics.log

	chmod 0644 /var/log/submit/monitors/jobProcessor.log
	chown gridman:gridman /var/log/submit/monitors/jobProcessor.log

	chmod 0644 /var/log/submit/monitors/jobStageInput.log
	chown gridman:gridman /var/log/submit/monitors/jobStageInput.log

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

        chmod 0644 ${VARLOG}/submit/monitors/monitorTunnel.log
        chown gridman:gridman ${VARLOG}/submit/monitors/monitorTunnel.log
    ;;

    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
