#!/bin/sh
#
# @package      hubzero-mw-service
# @file         postinst
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2015 HUBzero Foundation, LLC.
#
# 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 HUBzero Foundation, LLC.
#

# postinst script for hubzero-mw-service
#
# 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)
		if [ ! -e /var/log/mw-service ]
		then
			mkdir /var/log/mw-service
			chown root:adm /var/log/mw-service
			chmod 0750 /var/log/mw-service
		fi
		if [ ! -e /var/log/mw-service/open-sessions ]
		then
			mkdir /var/log/mw-service/open-sessions
			chown root:adm /var/log/mw-service/open-sessions
			chmod 0750 /var/log/mw-service/open-sessions
		fi
		PEMFILE="/etc/mw-service/xvnc.pem"
		# Generate new certs if needed
		if [ ! -e $PEMFILE ]; then
			cd /etc/mw-service
			PATH=$PATH:/usr/bin/ssl
			FQDN=`hostname -f`
MAILNAME=`cat /etc/mailname 2> /dev/null || hostname -f`
  (openssl req -new -x509 -days 365 -nodes -out $PEMFILE.cert -keyout $PEMFILE.key  > /dev/null 2>&1 <<+
.
.
.
HUBzero Middleware
$FQDN
$FQDN
root@$MAILNAME
+
  ) || echo "Warning : Bad SSL config, can't generate certificate."
  			cat $PEMFILE.cert $PEMFILE.key > $PEMFILE
  			rm $PEMFILE.cert $PEMFILE.key
		fi
		if [ -f /etc/hubzero/quota.conf ] 
		then
			if [ ! -f /etc/mw-service/quota.conf ]
			then
				mv /etc/hubzero/quota.conf /etc/mw-service/quota.conf
			else
				rm /etc/hubzero/quota.conf
			fi
		fi
		if [ -f /etc/hubzero/xvnc.pem ] 
		then
			if [ ! -f /etc/mw-service/xvnc.pem ]
			then
				mv /etc/hubzero/xvnc.pem /etc/mw-service/xvnc.pem
			else
				rm /etc/hubzero/xvnc.pem
			fi
		fi
		if [ -f /etc/hubzero/notify.key ] 
		then
			if [ ! -f /etc/mw-service/notify.key ]
			then
				mv /etc/hubzero/notify.key /etc/mw-service/notify.key
			else
				rm /etc/hubzero/notify.key
			fi
		fi
		if [ -f /var/log/hubzero/service.log ] 
		then
			if [ ! -f /var/log/mw-service/service.log ]
			then
				mv /var/log/hubzero/service.log /var/log/mw-service/service.log
			else
				log=`mktemp --tmpdir=/var/log/mw-service service.log.XXX`
				mv /var/log/hubzero/service.log $log
			fi
		fi
		if [ -d /var/lib/hubzero/ ] 
		then
			rm -f /var/lib/hubzero/xstartup
			find /var/lib/hubzero -maxdepth 1 -name "*.out" -type f -exec mv {} /var/log/mw-service/open-sessions \;
			find /var/lib/hubzero -maxdepth 1 -name "*.err" -type f -exec mv {} /var/log/mw-service/open-sessions \;
			if [ ! -d /var/run/mw-service ]
           	then
               	mkdir /var/run/mw-service
               	if [ -d /var/run/mw-service ]
               	then
                   	chown root:root /var/run/mw-service
                   	chmod 0775 /var/run/mw-service
               	fi
           	fi
			if [ -d /var/run/mw-service ]
			then
				find /var/lib/hubzero -maxdepth 1 -name "pass.*" -type f -exec mv {} /var/run/mw-service \;
				find /var/lib/hubzero -maxdepth 1 -name "passwd" -type f -exec mv {} /var/run/mw-service \;
				find /var/lib -maxdepth 1 -name "pid.*" -type f -exec mv {} /var/run/mw-service \;
			fi
			rmdir /var/lib/hubzero
		fi
		if [ -f /etc/mw/quota.conf ] 
		then
			if [ ! -f /etc/mw-service/quota.conf ]
			then
				mv /etc/mw/quota.conf /etc/mw-service/quota.conf
			else
				rm /etc/mw/quota.conf
			fi
		fi
		if [ -f /etc/mw/xvnc.pem ] 
		then
			if [ ! -f /etc/mw-service/xvnc.pem ]
			then
				mv /etc/mw/xvnc.pem /etc/mw-service/xvnc.pem
			else
				rm /etc/mw/xvnc.pem
			fi
		fi
		if [ -f /etc/mw/notify.key ] 
		then
			if [ ! -f /etc/mw-service/notify.key ]
			then
				mv /etc/mw/notify.key /etc/mw-service/notify.key
			else
				rm /etc/mw/notify.key
			fi
		fi
		if [ -f /etc/mw-client/notify.key ] 
		then
			if [ ! -f /etc/mw-service/notify.key ]
			then
				cp /etc/mw-client/notify.key /etc/mw-service/notify.key
			fi
		fi
		if [ -f /var/log/mw/service.log ] 
		then
			if [ ! -f /var/log/mw-service/service.log ]
			then
				mv /var/log/mw/service.log /var/log/mw-service/service.log
			else
				log=`mktemp --tmpdir=/var/log/mw-service service.log.XXX`
				mv /var/log/mw/service.log $log
			fi
		fi
		if [ -d /var/lib/mw/ ] 
		then
			rm -f /var/lib/mw/xstartup
			find /var/lib/mw -maxdepth 1 -name "*.out" -type f -exec mv {} /var/log/mw-service/open-sessions \;
			find /var/lib/mw -maxdepth 1 -name "*.err" -type f -exec mv {} /var/log/mw-service/open-sessions \;
			if [ ! -d /var/run/mw-service ]
           	then
               	mkdir /var/run/mw-service
               	if [ -d /var/run/mw-service ]
               	then
                   	chown root:root /var/run/mw-service
                   	chmod 0775 /var/run/mw-service
               	fi
           	fi
			if [ -d /var/run/mw-service ]
			then
				find /var/lib/mw -maxdepth 1 -name "pass.*" -type f -exec mv {} /var/run/mw-service \;
				find /var/lib/mw -maxdepth 1 -name "passwd" -type f -exec mv {} /var/run/mw-service \;
				find /var/lib -maxdepth 1 -name "pid.*" -type f -exec mv {} /var/run/mw-service \;
			fi
			rmdir /var/lib/mw
		fi
		if [ -f /etc/mw-service/mw-service.conf ]
		then
			chown root:root /etc/mw-service/mw-service.conf
			chmod 0600 /etc/mw-service/mw-service.conf
		fi
		if [ -f /etc/mw-service/quota.conf ]
		then
			chown root:root /etc/mw-service/quota.conf
			chmod 0600 /etc/mw-service/quota.conf
		fi
		if [ -f /etc/mw-service/notify.key ]
		then
			chown root:root /etc/mw-service/notify.key
			chmod 0400 /etc/mw-service/notify.key
		fi
		if [ -f /etc/mw-service/xvnc.pem ]
		then
			chown root:root /etc/mw-service/xvnc.pem
			chmod 0600 /etc/mw-service/xvnc.pem
		fi
		ucf /usr/share/hubzero-mw-service/quota.conf /etc/mw-service/quota.conf
  		if [ -e $PEMFILE ]; then
    		ucfr hubzero-mw-service $PEMFILE
  		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
