#!/bin/bash

source debian/vars.sh
set -x

# Create dirs
mkdir -p $DEB_INSTALL_ROOT/usr/local/cpanel-ccs/bin
mkdir -p $DEB_INSTALL_ROOT/usr/local/cpanel/scripts
mkdir -p $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/ccs
mkdir -p $DEB_INSTALL_ROOT/etc/chkserv.d
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/ServiceManager/Services/
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/TaskProcessors/
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/DAV/Config
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/Pkgacct/Components
mkdir -p $DEB_INSTALL_ROOT/usr/local/cpanel/Cpanel/API
mkdir -p $DEB_INSTALL_ROOT/usr/local/cpanel/Cpanel/Admin/Modules/Cpanel
mkdir -p $DEB_INSTALL_ROOT/var/cpanel/perl5/lib

# Move over main files
mv ./HACKING.md $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-twisted-osx.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./CalendarServer.egg-info $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./txweb2 $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./README.rst $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./LICENSE.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./twisted $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-cs.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-osx.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-default.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
#mv ./data $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./conf $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./.project $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./setup.py $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./txdav $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./subprojects $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./locales $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./PULL_REQUEST_TEMPLATE.md $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./bin $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./contrib $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./doc $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-dev.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./lib-patches $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./support $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-twisted-default.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./CONTRIBUTING.md $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./.develop $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./requirements-ignore-installed.txt $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./README.md $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./twistedcaldav $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./simplugin $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./HACKING.rst $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./calendarserver $DEB_INSTALL_ROOT/opt/cpanel-ccs/
mv ./.pydevproject $DEB_INSTALL_ROOT/opt/cpanel-ccs/

# Other files
mv horde_calendars2ics.php $DEB_INSTALL_ROOT/usr/local/cpanel-ccs/bin/horde_calendars2ics.php
mv horde_contacts2vcf.php $DEB_INSTALL_ROOT/usr/local/cpanel-ccs/bin/horde_contacts2vcf.php
mv CCSHooks.pm $DEB_INSTALL_ROOT/var/cpanel/perl5/lib/CCSHooks.pm
mv CCS_AdminBin.pm $DEB_INSTALL_ROOT/usr/local/cpanel/Cpanel/Admin/Modules/Cpanel/ccs.pm
mv CCS_UAPI.pm $DEB_INSTALL_ROOT/usr/local/cpanel/Cpanel/API/CCS.pm
mv CCSPkgAcct.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/Pkgacct/Components
mv CCS.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/DAV/Config/CCS.pm
mv CCSTasks.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/TaskProcessors/CCSTasks.pm
mv Cpanel_CCS.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/ServiceManager/Services/Cpanel_ccs.pm
mv CCS_Utils.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/Utils.pm
mv CCS_DBUtils.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/DBUtils.pm
mv CCS_Userdata.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/Userdata.pm
mv CCS_Constants.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/Constants.pm
mv CCS_DomainTLS.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/DomainTLS.pm
mv CCS_Delegates.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/CCS/Delegates.pm
mv chkservd_config $DEB_INSTALL_ROOT/etc/chkserv.d/cpanel-ccs

# Install the service file, restart scripts
install -D -m0600 cpanel-ccs.service.in $DEB_INSTALL_ROOT/etc/systemd/system/cpanel-ccs.service
cd $DEB_INSTALL_ROOT/usr/local/cpanel/scripts
ln -s restartsrv_base restartsrv_cpanel_ccs

# Now remove some of the "test" configs
rm -f $DEB_INSTALL_ROOT/opt/cpanel-ccs/conf/caldavd-apple.plist
rm -f $DEB_INSTALL_ROOT/opt/cpanel-ccs/conf/caldavd-stdconfig.plist
rm -f $DEB_INSTALL_ROOT/opt/cpanel-ccs/conf/caldavd.plist
find $DEB_INSTALL_ROOT/opt/cpanel-ccs/conf/ -name *test* | xargs rm -rf
