#!/usr/bin/env bash
#
# Cloudesta Roundcube skin — installer (run as root on a cPanel server)
#
# Copies the skin into cPanel's bundled Roundcube, compiles its CSS,
# and prints the one config line to make it the default for all clients.
#
set -euo pipefail

# cPanel's bundled Roundcube location
RC_DIR="/usr/local/cpanel/base/3rdparty/roundcube"
HERE="$(cd "$(dirname "$0")" && pwd)"
SKIN_SRC="$HERE/cloudesta"
SKIN_DST="$RC_DIR/skins/cloudesta"

if [ ! -d "$RC_DIR" ]; then
  echo "ERROR: Roundcube not found at $RC_DIR"
  echo "       Check the path (cPanel version differences) and edit RC_DIR."
  exit 1
fi

echo ">> Copying skin to $SKIN_DST"
rm -rf "$SKIN_DST"
cp -a "$SKIN_SRC" "$RC_DIR/skins/"

echo ">> Compiling LESS -> CSS"
cd "$RC_DIR"
if [ -x "bin/updatecss.sh" ]; then
  bin/updatecss.sh --dir skins/cloudesta
else
  echo "   WARNING: bin/updatecss.sh not found/executable."
  echo "   Compile manually with lessc, or see README (Node.js prerequisite)."
fi

echo
echo ">> Done. To make Cloudesta the default skin for everyone, add to:"
echo "   $RC_DIR/config/config.inc.php"
echo
echo "   \$config['skin'] = 'cloudesta';"
echo "   \$config['skins_allowed'] = ['cloudesta', 'elastic'];"
echo "   \$config['skin_logo'] = ["
echo "       'cloudesta:*'     => 'skins/cloudesta/images/logo.svg',"
echo "       'cloudesta:small' => 'skins/cloudesta/images/logo.svg',"
echo "       'cloudesta:login' => 'skins/cloudesta/images/logo.svg',"
echo "   ];"
echo
echo "   (cPanel may reset config/skin on a Roundcube update — re-run reapply.sh after updates.)"
