#!/usr/bin/env sh
confd='/etc/opt/simplex-xftp'
logd='/var/opt/simplex-xftp'

# Check if server has been initialized
if [ ! -f "${confd}/file-server.ini" ]; then
  # If not, determine ip or domain
  case "${ADDR}" in
    '') printf 'Please specify $ADDR environment variable.\n'; exit 1 ;;
    *[a-zA-Z]*)
      case "${ADDR}" in
        *:*) set -- --ip "${ADDR}" ;;
        *) set -- -n "${ADDR}" ;;
      esac
      ;;
    *) set -- --ip "${ADDR}" ;;
  esac

  # Set quota
  case "${QUOTA}" in
    '') printf 'Please specify $QUOTA environment variable.\n'; exit 1 ;;
    *GB) QUOTA="$(printf ${QUOTA} | tr '[:upper:]' '[:lower:]')"; set -- "$@" --quota "${QUOTA}" ;;
    *gb) set -- "$@" --quota "${QUOTA}" ;;
    *) printf 'Wrong format. Format should be: 1gb, 10gb, 100gb.\n'; exit 1 ;;
  esac

  # Init the certificates and configs
  xftp-server init -l -p /srv/xftp "$@"
fi

# Backup store log just in case
#
# Uses the UTC (universal) time zone and this
# format: YYYY-mm-dd'T'HH:MM:SS
# year, month, day, letter T, hour, minute, second
#
# This is the ISO 8601 format without the time zone at the end.
#
_file="${logd}/file-server-store.log"
if [ -f "${_file}" ]; then
  _backup_extension="$(date -u '+%Y-%m-%dT%H:%M:%S')"
  cp -v -p "${_file}" "${_file}.${_backup_extension:-date-failed}"
  unset -v _backup_extension
fi
unset -v _file

# Finally, run xftp-sever. Notice that "exec" here is important:
# smp-server replaces our helper script, so that it can catch INT signal
exec xftp-server start +RTS -N -RTS

