#!/bin/sh
#
# @package      hubzero-hubgraph
# @file         postinst
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2010-2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

# postinst script for hubzero-hubgraph
#
# 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 id hubgraph > /dev/null 2>&1; then
            IUID=`id --user hubgraph`
            IGID=`id --group hubgraph`
        else
            IUID="NONE"
            IGID="NONE"
        fi
        if [ "$IUID" = "NONE" ]; then
            adduser --system --disabled-password  \
                    --home /var/lib/hubgraph           \
                    --force-badname --group --shell /bin/bash --gecos HUBgraph hubgraph
        fi

        mkdir -p /var/lib/hubgraph/lib
        mkdir -p /var/lib/hubgraph/lib/resources
        mkdir -p /var/lib/hubgraph/lib/views
        mkdir -p /var/log/hubgraph
        mkdir -p /var/log/hubgraph/daily

        chown hubgraph:hubgraph /etc/hubgraph.conf
        chown hubgraph:hubgraph /var/lib/hubgraph/lib
        chown hubgraph:hubgraph /var/lib/hubgraph/lib/resources
        chown hubgraph:hubgraph /var/lib/hubgraph/lib/views
        chown hubgraph:hubgraph /var/log/hubgraph
        chown hubgraph:hubgraph /var/log/hubgraph/daily

	   for f in /usr/lib/hubzero-hubgraph/node_modules/hubgraph-hubzero/lib/views/*;
	   do
		  basename=`basename $f`
		  if [ ! -e /var/lib/hubgraph/lib/views/$basename -a ! -h /var/lib/hubgraph/lib/views/$basename ]; then
		      ln -s /usr/lib/hubzero-hubgraph/node_modules/hubgraph-hubzero/lib/views/$basename /var/lib/hubgraph/lib/views/$basename
              chown -h hubgraph:hubgraph /var/lib/hubgraph/lib/views/$basename
		  fi
	   done

	   for f in /usr/lib/hubzero-hubgraph/node_modules/hubgraph-hubzero/lib/resources/*;
	   do
		  basename=`basename $f`
		  if [ ! -e /var/lib/hubgraph/lib/resources/$basename -a ! -h /var/lib/hubgraph/lib/resources/$basename ]; then
		      ln -s /usr/lib/hubzero-hubgraph/node_modules/hubgraph-hubzero/lib/resources/$basename /var/lib/hubgraph/lib/resources/$basename
              chown -h hubgraph:hubgraph /var/lib/hubgraph/lib/resources/$basename
		  fi
	   done

    ;;

    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
