#!/bin/sh
#
# @package      hubzero-solr
# @file         postinst
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2016 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2016 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-solr
#
# 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

SOLR_ARCHIVE=solr-6.1.0.tgz
SOLR_DIR=solr-6.1.0
SOLR_EXTRACT_DIR=/usr/share/hubzero-solr
SOLR_INSTALL_DIR="${SOLR_EXTRACT_DIR}/${SOLR_DIR}"
SOLR_SERVICE=hubzero-solr
SOLR_BASENAME=solr
SOLR_USER=hubzero-solr
SOLR_VAR_DIR=/srv/hubzero-solr
SOLR_PORT=8445

case "$1" in
    configure)
        adduser --system --shell /bin/bash --group --disabled-password --home "$SOLR_VAR_DIR" "$SOLR_USER"
	chmod 0750 "${SOLR_VAR_DIR}"
        chown "${SOLR_USER}:" "${SOLR_VAR_DIR}"

        mkdir -p "${SOLR_VAR_DIR}/data"
        chmod 0750 "${SOLR_VAR_DIR}/data"
        chown "${SOLR_USER}:" "${SOLR_VAR_DIR}/data"

        mkdir -p "${SOLR_VAR_DIR}/logs"
        chmod 0750 "${SOLR_VAR_DIR}/logs"
        chown "${SOLR_USER}:" "${SOLR_VAR_DIR}/logs"

        cp "${SOLR_INSTALL_DIR}/server/solr/solr.xml" "${SOLR_VAR_DIR}/data/solr.xml"
	chmod 0640  "${SOLR_VAR_DIR}/data/solr.xml"
        chown "${SOLR_USER}:" "${SOLR_VAR_DIR}/data/solr.xml"
	
        cp "${SOLR_INSTALL_DIR}/server/resources/log4j.properties" "${SOLR_VAR_DIR}/log4j.properties"
        chmod 0640 "${SOLR_VAR_DIR}/log4j.properties"
        chown "${SOLR_USER}:" "${SOLR_VAR_DIR}/log4j.properties"

        cp -r "${SOLR_INSTALL_DIR}/hubzero-solr-core" "${SOLR_VAR_DIR}/data"
        chown -R "${SOLR_USER}:" "${SOLR_VAR_DIR}/data/hubzero-solr-core"
	chmod o-rwx "${SOLR_VAR_DIR}/data/hubzero-solr-core"

    ;;

    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
