#!/bin/bash

format_date() {
  date -u +"%Y-%m-%d %H:%M:%S"
}

log_line() {
  echo "$(format_date) pre-start --> $1"
}

# logfile cleanup in case of runaway logs
SIZE_LIMIT=$((100 * 1024 * 1024)) # 100 MB
MAIN_LOG_FILE_PATH="/var/log/unifi-talk/unifi-talk.log"
MAIN_LOG_FILE_SIZE=$(stat -c%s "$MAIN_LOG_FILE_PATH")

# check if logs exceed limit, truncate
if [[ -f "$MAIN_LOG_FILE_PATH" && $MAIN_LOG_FILE_SIZE -gt $SIZE_LIMIT ]]; then
  truncate -s 0 "$MAIN_LOG_FILE_PATH"
  log_line "truncated '$MAIN_LOG_FILE_PATH' (size: $((MAIN_LOG_FILE_SIZE / 1024 / 1024)) MB)"
fi

log_line "starting pre-start script"

USER="unifi-talk"
GROUP="unifi-talk"
FREESWITCH_USER="freeswitch"
FREESWITCH_GROUP="freeswitch"
DB_NAME="unifi-talk"
DB_USER="unifi-talk"
PKG="unifi-talk"
LOGROTATE="/etc/logrotate.d/$PKG"
mkdir -p "/usr/share/$PKG/app/runtime"
mkdir -p "/var/log/$PKG"
chown -R "$USER:$GROUP" "/var/log/$PKG" "/usr/share/$PKG/app/runtime" "/etc/freeswitch/" "/var/lib/freeswitch" "/usr/share/freeswitch"
usermod -a -G ${FREESWITCH_GROUP} $USER || true
usermod -a -G $GROUP ${FREESWITCH_USER} || true
chmod -R 770 "/usr/share/freeswitch/sounds"
chmod -R 770 "/var/lib/freeswitch"
chmod -R 770 "/var/log/freeswitch"
chmod -R 770 "/etc/freeswitch/"

log_line "cleaning up legacy service logs"
rm /var/log/unifi-talk-service.log
rm /var/log/unifi-base-service.log

while [ "$(systemctl is-active freeswitch)" = "activating" ]
do
  log_line "Waiting for Freeswitch to activate..."
  sleep 5
done

dependency=$(systemctl is-active freeswitch)
log_line "Freeswitch service status '${dependency}'"
if [ "${dependency}" != "active" ]; then
  log_line "Freeswitch status '${dependency}' is not 'active' -> exiting"
  exit 1
fi

log_line "creating db user"
psql -U postgres <<-EOSQL
  CREATE USER "${DB_USER}";
  CREATE DATABASE "${DB_NAME}";
EOSQL

# Datbase used to be owned by postgres, reassign databse owner if needed.
if [ "$( psql -U postgres -tAc "SELECT pg_catalog.pg_get_userbyid(d.datdba) FROM pg_catalog.pg_database d WHERE d.datname = '$DB_NAME'" )" = $DB_NAME ]
then
  log_line "Database is already owned by $DB_USER"
else
  psql -U postgres -c "ALTER DATABASE \"$DB_NAME\" OWNER TO \"$DB_USER\""
  psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE \"$DB_NAME\" to \"$DB_USER\""
  psql -U postgres -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO \"$DB_USER\"" $DB_NAME
  psql -U postgres -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO \"$DB_USER\"" $DB_NAME
  for tbl in `psql -U postgres -qAt -c "select tablename from pg_tables where schemaname = 'public';" $DB_NAME` ; do  psql -U postgres -c "alter table \"$tbl\" owner to \"$DB_USER\"" $DB_NAME; done
  for tbl in `psql -U postgres -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" $DB_NAME` ; do  psql -U postgres -c "alter table \"$tbl\" owner to \"$DB_USER\"" $DB_NAME; done
  for tbl in `psql -U postgres -qAt -c "select table_name from information_schema.views where table_schema = 'public';" $DB_NAME` ; do  psql -U postgres -c "alter table \"$tbl\" owner to \"$DB_USER\"" $DB_NAME; done
fi

# UBC legacy db
UBC_DB_NAME="unifi-talk-base"
UBC_DB_USER="unifi-base"

# Legacy UBC database manipulations
# 1. rename db to "unifi-talk-base" so it's not lost on purge
# 2. rename table "device" to "device_discovery"
# 3. rename device_pkey index to device_discovery_pkey as device_pkey already exists
# 4. use pg_dump to copy the table over to unifi-talk database
# 5. set correct owner on the copied table

if [ "$( psql -U postgres -tAc "SELECT 1 FROM pg_catalog.pg_database WHERE datname='unifi-base'" )" = '1' ]
then
  log_line "renaming unifi-base db to $UBC_DB_NAME"
  psql -U postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'unifi-base' AND pid <> pg_backend_pid()"
  psql -U postgres -c "ALTER DATABASE \"unifi-base\" RENAME TO \"$UBC_DB_NAME\""
  psql -U postgres -d $UBC_DB_NAME -c "ALTER TABLE IF EXISTS device RENAME TO device_discovery"
  psql -U postgres -d $UBC_DB_NAME -c "ALTER INDEX IF EXISTS device_pkey RENAME TO device_discovery_pkey"

  # only do this is device_discovery doesn't already exist in Talk DB
  if [ "$( psql -U postgres -d unifi-talk -tAc "select 1 from pg_tables where schemaname = 'public' and tablename = 'device_discovery'" )" = '1' ]
  then
    log_line "device_discovery table already exists in Talk DB, skipping import"
  else
    log_line "copying device_discovery table to unifi-talk DB"
    pg_dump -U postgres -t device_discovery $UBC_DB_NAME | psql -U postgres $DB_NAME
    psql -U postgres -d $DB_NAME -c "ALTER TABLE public.device_discovery OWNER TO \"$DB_USER\""
  fi

  # rename device_discovery in UBC DB to device_discovery_backup, this ensures even further that repeat startups of Talk won't break the process
  psql -U postgres -d $UBC_DB_NAME -c "ALTER TABLE IF EXISTS device_discovery RENAME TO device_discovery_backup"
else
  log_line "unifi-base db does not exist, proceeding"
fi

log_line "setting logrotate config"
if [ ! -f ${LOGROTATE} ]; then
cat << EOF > ${LOGROTATE}
/var/log/unifi-talk/*.log {
         maxsize 10M
         rotate 5
         copytruncate
         compress
         notifempty
         missingok
         su root root
}
EOF
fi

log_line "creating internal directories"
for dir in "$UFT_INTERNAL_DIR" "$UFT_INTERNAL_DIR/recordings" "$UFT_INTERNAL_DIR/sounds" "$UFT_INTERNAL_DIR/ivr" "$UFT_INTERNAL_DIR/call_center" "$UFT_INTERNAL_DIR/contacts" "$UFT_INTERNAL_DIR/voicemail" "$UFT_INTERNAL_DIR/voicemail/greetings" "$UFT_INTERNAL_DIR/custom_hold_music" "$UFT_INTERNAL_DIR/call_record_announcements" "$UFT_INTERNAL_DIR/config" "$UFT_INTERNAL_DIR/device_wallpapers" "$UFT_INTERNAL_DIR/device_wallpapers/landscape" "$UFT_INTERNAL_DIR/device_wallpapers/portrait" "$UFT_INTERNAL_DIR/ringtones/custom" "$UFT_INTERNAL_DIR/ringtones/standard"
do
    mkdir -p ${dir} || true
done

is_link() {
  [ -L "$1" ] && [ -e "$1" ] && return
  false
}

log_line "creating external directories"
if mountpoint -q /srv || is_link /srv; then
  for dir in "$UFT_EXTERNAL_DIR/recordings" "$UFT_EXTERNAL_DIR/contacts" "$UFT_EXTERNAL_DIR/voicemail" "$UFT_EXTERNAL_DIR/voicemail/greetings"
  do
      mkdir -p ${dir} || true
  done
  chown -R "$USER:${FREESWITCH_GROUP}" "$UFT_EXTERNAL_DIR"  || true
  chmod -R 770 "$UFT_EXTERNAL_DIR"  || true
fi

log_line "creating other directories"
for dir in "$UFT_EMMC_TMP_DIR"
do
    mkdir -p ${dir} || true
done

chown -R "$USER:${FREESWITCH_GROUP}" "$UFT_INTERNAL_DIR" "$UFT_EMMC_TMP_DIR"  || true
chmod -R 770 "$UFT_INTERNAL_DIR"  || true

ln -sf /usr/share/$PKG/app/node_modules/@babel/cli/bin/babel.js /usr/share/$PKG/app/node_modules/.bin/babel

log_line "pre-start script done"
