#!/usr/bin/env bash
#
# The `ftp` machine action: whether the kiosk exposes an anonymous FTP server
# on all network interfaces for troubleshooting.
#
#   on    the FTP server is started, allowing clients on LAN or Hotspot
#         to browse and manage /srv/karaoke without passwords
#   off   the FTP server is stopped and ports are closed
#
# The setting is the unit: archstar-ftp.service enabled and started, or
# disabled and stopped. The launcher /usr/bin/archstar-ftp handles vsftpd
# execution and enforces a 15-minute inactivity timeout that automatically
# puts the service to sleep if unused.
#

set -euo pipefail

ARCHSTAR_ACTIONS_LIB="${ARCHSTAR_ACTIONS_LIB:-/usr/lib/archstar-actions/lib/common.sh}"
# shellcheck source=/dev/null
source "${ARCHSTAR_ACTIONS_LIB}"

UNIT='archstar-ftp.service'

value="${1:-}"
require_one_of "${value}" on off

if [[ "${value}" == 'on' ]]; then
    systemctl enable "${UNIT}"
    if [[ "${ARCHSTAR_ACTION_RESTART}" == '1' ]]; then
        systemctl start "${UNIT}"
    fi
else
    systemctl disable "${UNIT}"
    if [[ "${ARCHSTAR_ACTION_RESTART}" == '1' ]]; then
        systemctl stop "${UNIT}"
    fi
fi

remember_setting ftp "${value}"

if [[ "${value}" == 'on' ]]; then
    printf 'the troubleshooting FTP server is on (port 21); auto-sleep after 15 min of inactivity\n'
else
    printf 'the FTP server is off\n'
fi
