#!/bin/sh
# @package      hubzero-mw2-client
# @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-mw-client
#
# 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


case "$1" in
    configure)
		chmod 0700 /etc/mw-client
		chown www-data:www-data /etc/mw-client

		chmod 0640 /etc/mw-client/mw2-client.conf
		chown www-data:www-data /etc/mw-client/mw2-client.conf

		if [ ! -f /etc/mw-client/maxwell.key.pub ] 
		then 
			printf "y\n" | ssh-keygen -q -t rsa -b 3072 -f /etc/mw-client/maxwell.key -N '' -C "created `date +'%Y-%m-%d'` with 3072 bits for www-data`hostname`"
			chmod 0400 /etc/mw-client/maxwell.key.pub
			chown www-data:www-data /etc/mw-client/maxwell.key.pub
			ucfr hubzero-mw-client /etc/mw-client/maxwell.key.pub
			chmod 0400 /etc/mw-client/maxwell.key
			chown www-data:www-data /etc/mw-client/maxwell.key
			ucfr hubzero-mw-client /etc/mw-client/maxwell.key
 		fi

		if [ ! -f /etc/mw-client/notify.key.pub ]
 		then 
 			printf "y\n" | ssh-keygen -q -t rsa -b 3072 -f /etc/mw-client/notify.key -N '' -C "created `date +'%Y-%m-%d'` with 3072 bits for www-data@`hostname`"
			chmod 0750 /etc/mw-client/notify.key.pub
			chown www-data:www-data /etc/mw-client/notify.key.pub
			ucfr hubzero-mw-client /etc/mw-client/notify.key.pub
			chmod 0750 /etc/mw-client/notify.key
			chown www-data:www-data /etc/mw-client/notify.key
			ucfr hubzero-mw-client /etc/mw-client/notify.key
 		fi

		chown www-data:www-data /var/log/mw-client
		chmod 0750 /var/log/mw-client

		chown www-data:www-data /var/log/mw-client/sessions
		chmod 0750 /var/log/mw-client/sessions

		# move log files from very old middleware versions to current location

	        if [ -d /var/log/hubzero/sessions ]
        	then
			chmod -R 0640 /var/log/hubzero/sessions
			find /var/log/hubzero/sessions -maxdepth 1 -name "*.out" -type f -exec mv --backup=t {} /var/log/mw-client/sessions \;
			find /var/log/hubzero/sessions -maxdepth 1 -name "*.err" -type f -exec mv --backup=t {} /var/log/mw-client/sessions \;
			rmdir --ignore-fail-on-non-empty /var/log/hubzero/sessions
		fi

		if [ -d /var/log/hubzero ]
		then
			find /var/log/hubzero -maxdepth 1 -name "vnc-*" -type f -exec mv --backup=t {} /var/log/mw-client \;
			find /var/log/hubzero -maxdepth 1 -name "master*" -type f -exec mv --backup=t {} /var/log/mw-client \;
			rmdir --ignore-fail-on-non-empty /var/log/hubzero
		fi

		if [ -d /var/log/mw/sessions ]
		then
			chmod -R 0640 /var/log/mw/sessions
			find /var/log/mw/sessions -maxdepth 1 -name "*.out" -type f -exec mv --backup=t {} /var/log/mw-client/sessions \;
			find /var/log/mw/sessions -maxdepth 1 -name "*.err" -type f -exec mv --backup=t {} /var/log/mw-client/sessions \;
			rmdir --ignore-fail-on-non-empty /var/log/mw/sessions
		fi

		if [ -d /var/log/mw ]
		then
			chmod -R 0640 /var/log/mw
			find /var/log/mw -maxdepth 1 -name "vnc-*" -type f -exec mv --backup=t {} /var/log/mw-client \;
			find /var/log/mw -maxdepth 1 -name "master*" -type f -exec mv --backup=t {} /var/log/mw-client \;
			rmdir --ignore-fail-on-non-empty /var/log/mw
		fi

		if [ -f /etc/mw-client/notify.key -a -d /etc/mw-service ]
		then 
			cp /etc/mw-client/notify.key /etc/mw-service/notify.key
			chown root:root /etc/mw-service/notify.key
			chmod 0400 /etc/mw-service/notify.key
		fi

		if [ ! -d ~www-data/.ssh ]
		then
			mkdir -p ~www-data/.ssh
			chown www-data:www-data ~www-data/.ssh
			chmod 0700 ~www-data/.ssh
	    	fi

		if [ ! -f ~www-data/.ssh/authorized_keys ]
		then 
	 		touch  ~www-data/.ssh/authorized_keys
			chmod 0640 ~www-data/.ssh/authorized_keys
			chown www-data:www-data ~www-data/.ssh/authorized_keys
		fi
	
		grep -q -f /etc/mw-client/notify.key.pub ~www-data/.ssh/authorized_keys || (echo -n "COMMAND=\"/usr/bin/maxwell notify\" " >> ~www-data/.ssh/authorized_keys; cat /etc/mw-client/notify.key.pub >> ~www-data/.ssh/authorized_keys)

		chown www-data:www-data ~www-data/.ssh
		chmod 0700 ~www-data/.ssh
		chown www-data:www-data ~www-data/.ssh/authorized_keys
		chmod 0400 ~www-data/.ssh/authorized_keys

    ;;

    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
