#!/bin/sh
# @package      hubzero-expire-sessions
# @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-expire-sessions
#
# 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)
        chown www-data:adm /var/log/expire-sessions
        chmod 0750 /var/log/expire-sessions
        if [ -d /var/run/expire-sessions -a -f /var/run/expire-sessions/expire-sessions.pid ]
        then
            if [ ! -f /var/run/expire-sessions.pid ]
            then
                mv /var/run/expire-sessions/expire-sessions.pid /var/run/expire-sessions.pid
                if [ -d  /var/run/expire-sessions -a ! -h /var/run/expire-sessions ]
                then
                    rmdir --ignore-fail-on-non-empty /var/run/expire-sessions
                fi
            fi
        fi

        if [ -d /var/log/hubzero ]
        then
             find /var/log/hubzero -maxdepth 1 -name "expire.log*" -type f -exec mv --backup=t {} /var/log/expire-sessions \;
             if [ ! -e /var/log/expire-sessions/expire-sessions.log -a -e /var/log/expire-sessions/expire.log ]
             then
                 mv --backup=t /var/log/expire-sessions/expire.log /var/log/expire-sessions/expire-sessions.log
             fi
             if [ -d  /var/log/hubzero -a ! -h /var/log/hubzero ]
             then
                 rmdir --ignore-fail-on-non-empty /var/log/hubzero
             fi
        fi

        if [ -d /var/log/mw ]
        then
             find /var/log/mw -maxdepth 1 -name "expire.log*" -type f -exec mv --backup=t {} /var/log/expire-sessions \;
             if [ ! -e /var/log/expire-sessions/expire-sessions.log -a -e /var/log/expire-sessions/expire.log ]
             then
                 mv --backup=t /var/log/expire-sessions/expire.log /var/log/expire-sessions/expire-sessions.log
             fi
             if [ -d  /var/log/mw -a ! -h /var/log/mw ]
             then
                 rmdir --ignore-fail-on-non-empty /var/log/mw
             fi
        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
