#!/bin/bash

source debian/vars.sh

set -x

mkdir -p $DEB_INSTALL_ROOT/home/cpanelsolr/
cp -a bin CHANGES.txt dist docs example licenses LICENSE.txt LUCENE_CHANGES.txt NOTICE.txt README.txt server $DEB_INSTALL_ROOT/home/cpanelsolr/

mkdir -p $DEB_INSTALL_ROOT/home/cpanelsolr/server/solr/
tar xzvf debian/SOURCES_FROM_SPEC/dovecot.tar.gz -C debian/tmp/home/cpanelsolr/server/solr/

chmod 0750 "$DEB_INSTALL_ROOT/$workdir"

mkdir -m 0755 $DEB_INSTALL_ROOT/$workdir/server/logs

install -d $DEB_SOURCE_ROOT/usr/local/cpanel/3rdparty/scripts
install -d $DEB_SOURCE_ROOT/usr/local/cpanel/scripts
install -d $DEB_SOURCE_ROOT/etc/dovecot
install -d $DEB_SOURCE_ROOT/etc/chkserv.d
install -d $DEB_SOURCE_ROOT/etc/cron.d

install -D -m0644 $DEB_SOURCE_ROOT/Cpanel_dovecot_solr.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/ServiceManager/Services/Cpanel_dovecot_solr.pm
install -D -m0644 $DEB_SOURCE_ROOT/Utils.pm $DEB_INSTALL_ROOT/var/cpanel/perl/Cpanel/Dovecot/Solr/Utils.pm
install -D -m0600 $DEB_SOURCE_ROOT/solr.sysconfig.in $DEB_INSTALL_ROOT/etc/sysconfig/cpanel-dovecot-solr
sed -i 's,@@HOME@@,/home/cpanelsolr/server/solr,g' $DEB_INSTALL_ROOT/etc/sysconfig/cpanel-dovecot-solr
install -D -m0600 $DEB_SOURCE_ROOT/cpanel-dovecot-solr.chkserv.d $DEB_INSTALL_ROOT/etc/chkserv.d/cpanel-dovecot-solr
install -D -m0600 $DEB_SOURCE_ROOT/fts.conf $DEB_INSTALL_ROOT/etc/dovecot/fts.conf
install -D -m0600 $DEB_SOURCE_ROOT/cpanel-dovecot-solr.cron $DEB_INSTALL_ROOT/etc/cron.d/cpanel-dovecot-solr
install -D -m0700 $DEB_SOURCE_ROOT/setup_cpanel_dovecot_fts $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/setup_cpanel_dovecot_fts
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_service_restart $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_service_restart
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_commit $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_commit
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_maintenance $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_maintenance
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_firewall $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_firewall
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_isonline $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_isonline
install -D -m0700 $DEB_SOURCE_ROOT/cpanel_dovecot_solr_rebuild_index $DEB_INSTALL_ROOT/usr/local/cpanel/3rdparty/scripts/cpanel_dovecot_solr_rebuild_index
install -D -m0600 $DEB_SOURCE_ROOT/solr.in.sh.in $DEB_INSTALL_ROOT/home/cpanelsolr/bin/solr.in.sh
install -D -m0600 $DEB_SOURCE_ROOT/realm.properties $DEB_INSTALL_ROOT/home/cpanelsolr/server/etc/realm.properties

install -D -m0644 $DEB_SOURCE_ROOT/solr.service.in $DEB_INSTALL_ROOT/etc/systemd/system/cpanel-dovecot-solr.service
sed -i 's,@@PKG_ROOT@@,/home/cpanelsolr/,g' $DEB_INSTALL_ROOT/etc/systemd/system/cpanel-dovecot-solr.service

mkdir -m 0755 $DEB_INSTALL_ROOT/usr/local/cpanel/scripts/
ln -s -f /usr/local/cpanel/scripts/restartsrv_base $DEB_INSTALL_ROOT/usr/local/cpanel/scripts/restartsrv_cpanel_dovecot_solr

rm -rf $DEB_INSTALL_ROOT/home/cpanelsolr/example
