#!/bin/sh

set -eu

copy_js_font_files()
{
  # Rspamd web interface doesn't support symlinks and directory traversal
  # copy javascript files from original Debian packages
  RSPAMD_CSS_DIR=/usr/share/rspamd/www/css
  RSPAMD_JS_LIB_DIR=/usr/share/rspamd/www/js/lib
  cp --remove-destination /usr/share/javascript/bootstrap5/js/bootstrap.bundle.min.js $RSPAMD_JS_LIB_DIR/bootstrap.bundle.min.js
  cp --remove-destination /usr/share/javascript/bootstrap5/css/bootstrap.min.css $RSPAMD_CSS_DIR/bootstrap.min.css
  cp --remove-destination /usr/share/javascript/jquery/jquery.min.js $RSPAMD_JS_LIB_DIR/jquery.min.js
  cp --remove-destination /usr/share/javascript/requirejs/require.js $RSPAMD_JS_LIB_DIR/require.min.js

  # remove path of previous versions, if existent
  if [ -f $RSPAMD_JS_LIB_DIR/jquery-3.3.1.min.js ]; then
    rm $RSPAMD_JS_LIB_DIR/jquery-3.3.1.min.js
  fi
  if [ -f $RSPAMD_JS_LIB_DIR/bootstrap.min.js ]; then
    rm $RSPAMD_JS_LIB_DIR/bootstrap.min.js
  fi

  # copy font files from original Debian package
  RSPAMD_FONT_DIR=/usr/share/rspamd/www/fonts
  cp --remove-destination /usr/share/fonts-glyphicons/glyphicons-halflings-regular.ttf $RSPAMD_FONT_DIR/glyphicons-halflings-regular.ttf
  cp --remove-destination /usr/share/fonts-glyphicons/glyphicons-halflings-regular.woff $RSPAMD_FONT_DIR/glyphicons-halflings-regular.woff
  cp --remove-destination /usr/share/fonts-glyphicons/glyphicons-halflings-regular.woff2 $RSPAMD_FONT_DIR/glyphicons-halflings-regular.woff2
}

case "$1" in
    configure)
      SERVER_HOME=/var/lib/rspamd
      SERVER_LOG=/var/log/rspamd
      SERVER_USER=_rspamd

      adduser --quiet \
              --system \
              --group \
              --home $SERVER_HOME \
              --no-create-home \
              --disabled-login \
              --gecos "rspamd spam filtering system" \
              --force-badname \
              $SERVER_USER

      chown $SERVER_USER: $SERVER_HOME $SERVER_LOG

      copy_js_font_files
    ;;

    triggered)
      copy_js_font_files
    ;;

    abort-*)
    ;;

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#
