#!/bin/bash

source /usr/local/etc/library.sh

# INIT NCP CONFIG (first run)
[[ "$1" == "start" ]] && [[ -d /data/ncp ]] && {
  echo "Install default configurations for new ncp apps"
  rsync --ignore-existing -a --out-format="%n" /usr/local/etc/ncp-config.d/*.cfg /data/ncp/ | awk '{ print " > " $0 }'
}
persistent_cfg /usr/local/etc/ncp-config.d /data/ncp
persistent_cfg /etc/letsencrypt                    # persist SSL certificates
persistent_cfg /etc/shadow                         # persist ncp-web password
persistent_cfg /etc/cron.d
persistent_cfg /etc/cron.daily
persistent_cfg /etc/cron.hourly
persistent_cfg /etc/cron.weekly

# reset bin if we pull a more recent container
[[ -d /data/bin ]] && {
  date_img=$(date "+%s" -r /.docker-image)
  date_per=$(date "+%s" -r /data/bin)
  [[ $date_img -gt $date_per ]] && {
    rm -r /data/bin
  }
}
persistent_cfg /usr/local/bin /data/bin

[[ "$1" == "start" ]] && {
  install_template "php/opcache.ini.sh" "/etc/php/${PHPVER}/mods-available/opcache.ini"
  install_template "php/90-ncp.ini.sh" "/etc/php/${PHPVER}/fpm/conf.d/90-ncp.ini"
  install_template "php/pool.d.www.conf.sh" "/etc/php/${PHPVER}/fpm/pool.d/www.conf"
  #install_template "mysql/91-ncp.cnf.sh" "/etc/mysql/mariadb.conf.d/91-ncp.cnf"
  install_template "ncp-metrics.cfg.sh" "/usr/local/etc/ncp-metrics.cfg"
}

touch /var/log/ncp.log
tail -f -n0 "/var/log/ncp.log" &

exit 0
