#!/bin/bash
# postinst script for cpanel-ccs-calendarserver
#
# see: dh_installdeb(1)

set -e

# Begin vars.sh
#!/bin/bash
# DO NOT EDIT
# This file is regenerated each run.
# If you want to add or change a variable, edit debify/settings.json and the vars hashref

source /etc/os-release
export OBS_REPO="x${NAME}_${VERSION_ID}"
if ls debian/*.install &> /dev/null; then
    perl -pi -e 's/\${env:OBS_REPO}/$ENV{OBS_REPO}/' debian/*.install
fi

export DEB_INSTALL_ROOT="/usr/src/packages/BUILD/debian/tmp"
export DEB_SOURCE_ROOT="/usr/src/packages/BUILD/debian/SOURCES_FROM_SPEC"
export RPM_SOURCE_DIR="/usr/src/packages/BUILD/debian/SOURCES_FROM_SPEC"
export __isa_bits="64"
export __isa_name="x86"
export __sourcedir=""
export _bindir="/opt/cpanel//root/usr/bin"
export _datadir="/opt/cpanel//root/usr/share"
export _default_patch_fuzz="0"
export _defaultdocdir="/opt/cpanel//root/usr/share/doc"
export _docdir="/opt/cpanel//root/usr/share/doc"
export _exec_prefix="/opt/cpanel//root/usr"
export _includedir="/opt/cpanel//root/usr/include"
export _infodir="/opt/cpanel//root/usr/share/info"
export _isa=""
export _lib="lib64"
export _libdir="/opt/cpanel//root/usr/lib64"
export _libexecdir="/opt/cpanel//root/usr/libexec"
export _localstatedir="/opt/cpanel//root/var"
export _mandir="/opt/cpanel//root/usr/share/man"
export _prefix="/opt/cpanel//root/usr"
export _root_bindir="/opt/cpanel//root/usr/bin"
export _root_datadir="/opt/cpanel//root/usr/share"
export _root_exec_prefix="/opt/cpanel//root/usr"
export _root_includedir="/opt/cpanel//root/usr/include"
export _root_infodir="/opt/cpanel//root/usr/share/info"
export _root_initddir="/opt/cpanel//root/etc/rc.d/init.d"
export _root_libdir="/opt/cpanel//root/usr/lib64"
export _root_libexecdir="/opt/cpanel//root/usr/libexec"
export _root_localstatedir="/opt/cpanel//root/var"
export _root_mandir="/opt/cpanel//root/usr/share/man"
export _root_prefix="/opt/cpanel//root/usr"
export _root_sbindir="/opt/cpanel//root/usr/sbin"
export _root_sharedstatedir="/opt/cpanel//root/usr/com"
export _root_sysconfdir="/opt/cpanel//root/etc"
export _sbindir="/opt/cpanel//root/usr/sbin"
export _scl_prefix="/opt/cpanel"
export _scl_root="/opt/cpanel//root"
export _sysconfdir="/opt/cpanel//root/etc"
export _systemd_dir="/etc/systemd/system"
export _unitdir="/usr/lib/systemd/system"
export base_name="cpanel-ccs"
export buildroot="/usr/src/packages/BUILD"
export cpanel_name="cpanel-ccs-calendarserver"
export debug_package="0"
export full_package_name="cpanel-ccs-calendarserver-9.3"
export git_commit="fbd0e11675cc0f64a425581b5c8398cc1e09cb6a"
export ix86="i386"
export link_file="/etc/init.d/cpanel_ccs"
export link_name="cpanel-ccs.service"
export name="cpanel-ccs-calendarserver"
export nil="0"
export ns_name="ea"
export pkg=""
export pkg_name=""
export pkgname=""
export previous_version="9.3-25.cpanel"
export release="26"
export release_prefix="26"
export scl=""
export scl_name=""
export scl_name_base=""
export scl_name_version=""
export scl_prefix=""
export service_file="/etc/systemd/system/cpanel-ccs"
export service_name="cpanel-ccs"
export start_script_path="/etc/init.d/cpanel-ccs"
export upstream_name="ccs-calendarserver"
export version="9.3"
export version_with_debian_revision="9.3-26"
export SOURCE1="$DEB_SOURCE_ROOT/caldavd.plist"
export SOURCE2="$DEB_SOURCE_ROOT/cpanel-ccs.init.in"
export SOURCE3="$DEB_SOURCE_ROOT/cpanel-ccs.service.in"
export SOURCE4="$DEB_SOURCE_ROOT/cpanel.py"
export SOURCE5="$DEB_SOURCE_ROOT/populate_mailuser_directory_for_ccs"
export SOURCE6="$DEB_SOURCE_ROOT/CCSHooks.pm"
export SOURCE7="$DEB_SOURCE_ROOT/CCS_AdminBin.pm"
export SOURCE8="$DEB_SOURCE_ROOT/CCS_UAPI.pm"
export SOURCE9="$DEB_SOURCE_ROOT/CCSPkgAcct.pm"
export SOURCE10="$DEB_SOURCE_ROOT/test_add_ccs_cal_event"
export SOURCE11="$DEB_SOURCE_ROOT/test_get_ccs_cal_events"
export SOURCE12="$DEB_SOURCE_ROOT/CCS.pm"
export SOURCE13="$DEB_SOURCE_ROOT/CCSTasks.pm"
export SOURCE14="$DEB_SOURCE_ROOT/caldavd.tmpl"
export SOURCE15="$DEB_SOURCE_ROOT/rebuildccsconf"
export SOURCE16="$DEB_SOURCE_ROOT/safe_configure_postgres_for_cpanel"
export SOURCE17="$DEB_SOURCE_ROOT/Cpanel_CCS.pm"
export SOURCE18="$DEB_SOURCE_ROOT/CCS_Utils.pm"
export SOURCE19="$DEB_SOURCE_ROOT/migrate_horde2ccs"
export SOURCE20="$DEB_SOURCE_ROOT/horde_calendars2ics.php"
export SOURCE21="$DEB_SOURCE_ROOT/horde_contacts2vcf.php"
export SOURCE22="$DEB_SOURCE_ROOT/chkservd_config"
export SOURCE23="$DEB_SOURCE_ROOT/CCS_DBUtils.pm"
export SOURCE24="$DEB_SOURCE_ROOT/CCS_Userdata.pm"
export SOURCE25="$DEB_SOURCE_ROOT/CCS_Constants.pm"
export SOURCE26="$DEB_SOURCE_ROOT/CCS_DomainTLS.pm"
export SOURCE27="$DEB_SOURCE_ROOT/CCS_Delegates.pm"
export SOURCE28="$DEB_SOURCE_ROOT/pkg.cpanel-ccs-calendarserver.preinst"
export SOURCE29="$DEB_SOURCE_ROOT/pkg.cpanel-ccs-calendarserver.prerm"
export SOURCE30="$DEB_SOURCE_ROOT/pkg.cpanel-ccs-calendarserver.postinst"
export SOURCE31="$DEB_SOURCE_ROOT/pkg.cpanel-ccs-calendarserver.postrm"
# End vars.sh

# 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 https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# Contents of SOURCES/pkg.cpanel-ccs-calendarserver.postinst
#!/bin/bash

# This will break on ubuntu upgrades if we do not create the id here too
# https://www.debian.org/doc/debian-policy/ap-flowcharts.html
id -u cpanel-ccs >/dev/null 2>&1 || useradd --create-home --home-dir /opt/cpanel-ccs --system --shell /bin/false --user-group --comment "Calendar and Contacts Server Daemon User" cpanel-ccs

chown -hR cpanel-ccs:cpanel-ccs /opt/cpanel-ccs ||:

# Correct shebangs
sed -i 's:/home/abuild/rpmbuild/BUILD/cpanel-ccs[^/]*/:/opt/cpanel-ccs/:g' /opt/cpanel-ccs/bin/calendarserver_* ||:

# also ensure that queueprocd is kicked so that it reads in the custom task
# processor that we wrote for it
/usr/local/cpanel/scripts/restartsrv queueprocd ||:
echo "CCS" > /var/cpanel/calendarserver

# Remove "bad" hooks that were left around by a bad previous version of the RPM
# Ignore failures in case you are on a version of cPanel too old for feature
/usr/local/cpanel/bin/manage_hooks prune ||:
/usr/local/cpanel/bin/manage_hooks add module CCSHooks ||:

# $2 will be null on ubuntu if this is a new install
# otherwise, it will be set to the previous version of the package
# This is the only way I could find to differentiate the two in the documentation
# for postinst
if [[ -z $2 || $1 -eq 1 ]]; then
    /opt/cpanel-ccs/bin/safe_configure_postgres_for_cpanel ||:
fi

# Check things on update -- if we've already imported/genned users,
# Then this opt will cause it to be noop
/opt/cpanel-ccs/bin/populate_mailuser_directory_for_ccs --import-accounts --import-tls --only-if-missing ||:

# Regen the configuration to match the host details
/opt/cpanel-ccs/bin/rebuildccsconfig ||:

if [[ -z $2 || $1 -eq 1 ]]; then
    /usr/local/cpanel/scripts/restartsrv_cpdavd --hard ||:
fi

echo 'caldavcarddav:Calendars and Contacts' > /usr/local/cpanel/whostmgr/addonfeatures/caldavcarddav
echo 'manage_calendar_access:Calendar Delegation' > /usr/local/cpanel/whostmgr/addonfeatures/manage_calendar_access

# Enable monitoring in chkservd.
echo "cpanel-ccs:1" >> /etc/chkserv.d/chkservd.conf
echo "+" > /var/run/chkservd/cpanel-ccs

systemctl enable cpanel-ccs ||:
systemctl daemon-reload ||:
systemctl --no-pager start cpanel-ccs ||:

# Slurp in horde calendar if horde is installed
if [[ -z $2 || $1 -eq 1 ]]; then
    /usr/local/cpanel/bin/servers_queue schedule 60 migrate_horde2ccs ||:

    # Ensure cPanel knows CCS is installed or not.
    # This binary stupidly exits 141 on success, so good luck getting a
    # meaningful exit code from this.
    /usr/local/cpanel/bin/build_global_cache ||:
fi
# End SOURCES/pkg.cpanel-ccs-calendarserver.postinst


exit 0
