#!/usr/bin/env bash

MODULE_FTP_SERVER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODULE_FTP_SERVER_PROJECT_ROOT="$(cd "$MODULE_FTP_SERVER_DIR/../../.." && pwd)"

# shellcheck source=lib/package.sh
source "$MODULE_FTP_SERVER_PROJECT_ROOT/lib/package.sh"
# shellcheck source=modules/network/ftp-server/config.sh
source "$MODULE_FTP_SERVER_DIR/config.sh"
# shellcheck source=modules/network/ftp-server/metadata.conf
source "$MODULE_FTP_SERVER_DIR/metadata.conf"

module_ftp_server_metadata() {
  printf '%s|%s|%s\n' "$MODULE_ID" "$MODULE_NAME" "$MODULE_DESCRIPTION"
}

module_ftp_server_config_path() {
  printf '%s/%s\n' "$MODULE_FTP_SERVER_PROJECT_ROOT" "$POSTINSTALL_FTP_SETTINGS_FILE"
}

module_ftp_server_settings() {
  local config_path=""
  local listen_port="21"
  local anonymous_enable="no"
  local local_enable="yes"
  local write_enable="no"
  local local_root="/home/gilles"

  config_path="$(module_ftp_server_config_path)"
  if [[ -f "$config_path" ]]; then
    while IFS='=' read -r key value; do
      case "$key" in
        listen_port) listen_port="$value" ;;
        anonymous_enable) anonymous_enable="$value" ;;
        local_enable) local_enable="$value" ;;
        write_enable) write_enable="$value" ;;
        local_root) local_root="$value" ;;
      esac
    done < <(
      awk '
        /^[[:space:]]*listen_port:/ { print "listen_port=" $2 }
        /^[[:space:]]*anonymous_enable:/ { print "anonymous_enable=" $2 }
        /^[[:space:]]*local_enable:/ { print "local_enable=" $2 }
        /^[[:space:]]*write_enable:/ { print "write_enable=" $2 }
        /^[[:space:]]*local_root:/ { print "local_root=" $2 }
      ' "$config_path"
    )
  fi

  printf '%s|%s|%s|%s|%s\n' "$listen_port" "$anonymous_enable" "$local_enable" "$write_enable" "$local_root"
}

module_ftp_server_write_config() {
  local listen_port="$1"
  local anonymous_enable="$2"
  local local_enable="$3"
  local write_enable="$4"
  local local_root="$5"

  cat > "$POSTINSTALL_FTP_CONFIG_FILE" <<EOF
listen=YES
listen_port=$listen_port
anonymous_enable=$anonymous_enable
local_enable=$local_enable
write_enable=$write_enable
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
ssl_enable=NO
local_root=$local_root
allow_writeable_chroot=YES
EOF
}

module_ftp_server_install() {
  local settings=""
  local listen_port=""
  local anonymous_enable=""
  local local_enable=""
  local write_enable=""
  local local_root=""

  settings="$(module_ftp_server_settings)"
  IFS='|' read -r listen_port anonymous_enable local_enable write_enable local_root <<< "$settings"

  package_refresh_indexes
  package_install vsftpd
  mkdir -p "$local_root"
  module_ftp_server_write_config "$listen_port" "$anonymous_enable" "$local_enable" "$write_enable" "$local_root"

  systemctl enable --now vsftpd
  systemctl restart vsftpd

  log_info "Serveur FTP configure sur le port $listen_port"
  ui_success "Serveur FTP configure sur le port $listen_port"
}

module_ftp_server_test() {
  package_is_installed vsftpd || return 1
  test -f "$POSTINSTALL_FTP_CONFIG_FILE" || return 1
  test -f "$(module_ftp_server_config_path)" || return 1
  command -v ftp >/dev/null 2>&1 || true
}
