echo "KeyHelp Components Updater"
echo "BEWARE! We do NOT garantee an full functional KeyHelp after executing of this Installer"
echo "Use it with caution!!!"
sleep 1
echo "By Justman10000"
sleep 2.5

reset

echo "Installing dependencies"
sleep 1.5
bash <(curl -s https://scripts.justman10000.de/install.sh)

reset

echo "Updating RoundCube (Webmail)"
sleep 1.5
cd /home/keyhelp/www/roundcube
mv config/config.inc.php /tmp
rm -r .* *
git init
git pull https://github.com/roundcube/roundcubemail
mv /tmp/config.inc.php config
COMPOSER_ALLOW_SUPERUSER=1 composer install
if command -v bun > /dev/null 2>&1; then
  bun i --no-save
else
  yarn
fi
cd skins/elastic
make css

reset

echo "Updating Dovecot (Mail Delivery Agent (MDA))"
sleep 1.5
mkdir /home/dovecot
cd /home/dovecot
git init
git pull https://github.com/dovecot/core.git
bash autogen.sh
bash configure
make install

echo "Updating all IonCubes"
echo "Production (PHP 5.6-8.4)"
wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.zip
unzip ioncube_loaders_lin_x86-64.zip
mv ioncube/ioncube_loader_lin_*.so /usr/local/ioncube
echo "Beta (PHP 8.5)"
wget https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64_beta.zip
unzip ioncube_loaders_lin_x86-64_beta.zip
mv ioncube/ioncube_loader_lin_*.so /usr/local/ioncube

for pid in $(pgrep -f 'keyhelp-php-fpm|php-fpm.*|php-fpm.*' --nslist pid); do
  kill -USR2 $pid
done