#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2012 x23
# Copyright (C) 2009-2013 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2012-2015 ultraman
# Copyright (C) 2021-present Team LibreELEC (https://libreelec.tv)

. /etc/profile

mkdir -p "/storage/.kodi/userdata/addon_data/service.proftpd"
oe_setup_addon service.proftpd

chmod a+x $ADDON_DIR/bin/*

if [ ! -f "$ADDON_HOME/proftpd.conf" ]; then
  cp $ADDON_DIR/etc/proftpd.conf $ADDON_HOME
fi

if [ ! -f "$ADDON_HOME/openssl.cnf" ]; then
  cp $ADDON_DIR/etc/openssl.cnf $ADDON_HOME
fi

[ -z "$Debuglevel" ] && Debuglevel=0

if [ ! -f "$ADDON_HOME/proftpd.cert.pem" -o ! -f "$ADDON_HOME/proftpd.key.pem" ]; then
  openssl req -newkey rsa:${CertBitStrength} -config $ADDON_HOME/openssl.cnf -new -x509 -days "$CertTTL" -nodes -out "$ADDON_HOME/proftpd.cert.pem" -keyout "$ADDON_HOME/proftpd.key.pem"
fi

# Edit proftpd.conf with user settings
if [ "$TLSEngine" = "false" ]; then
  sed -i 's/TLSEngine.*/TLSEngine off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/TLSEngine.*/TLSEngine on/g' $ADDON_HOME/proftpd.conf
fi

if [ "$AllowOverwrite" = "false" ]; then
  sed -i 's/AllowOverwrite.*/AllowOverwrite off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/AllowOverwrite.*/AllowOverwrite on/g' $ADDON_HOME/proftpd.conf
fi

sed -i 's/^Port.*/Port '$Port'/g' $ADDON_HOME/proftpd.conf
sed -i 's/MaxClients .*/MaxClients '$MaxClients'/g' $ADDON_HOME/proftpd.conf
sed -i 's/MaxClientsPerHost.*/MaxClientsPerHost '$MaxClientsPerHost'/g' $ADDON_HOME/proftpd.conf
sed -i 's/MaxHostsPerUser.*/MaxHostsPerUser '$MaxHostsPerUser'/g' $ADDON_HOME/proftpd.conf
sed -i 's/MaxInstances.*/MaxInstances '$MaxInstances'/g' $ADDON_HOME/proftpd.conf
sed -i 's/Umask.*/Umask '$Umask'/g' $ADDON_HOME/proftpd.conf
sed -i 's/ServerName.*/ServerName "'"$ServerName"'"/g' $ADDON_HOME/proftpd.conf
sed -i 's/PassivePorts.*/PassivePorts '$PassivePorts' '$PassivePorts2'/g' $ADDON_HOME/proftpd.conf

sed -i '/MasqueradeAddress.*/d' $ADDON_HOME/proftpd.conf
if [ "$MasqueradeAddress" = "true" ]; then
  sed -i '1i\MasqueradeAddress '$MasqueradeAddress2'' $ADDON_HOME/proftpd.conf
fi

if [ "$ServerIdent" = "false" ]; then
  sed -i 's/ServerIdent.*/ServerIdent off "'"$ServerIdentText"'"/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/ServerIdent.*/ServerIdent on "'"$ServerIdentText"'"/g' $ADDON_HOME/proftpd.conf
fi

if [ "$AllowForeignAddress" = "false" ]; then
  sed -i 's/AllowForeignAddress.*/AllowForeignAddress off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/AllowForeignAddress.*/AllowForeignAddress on/g' $ADDON_HOME/proftpd.conf
fi

if [ "$AllowStoreRestart" = "false" ]; then
  sed -i 's/AllowStoreRestart.*/AllowStoreRestart off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/AllowStoreRestart.*/AllowStoreRestart on/g' $ADDON_HOME/proftpd.conf
fi

if [ "$AllowRetrieveRestart" = "false" ]; then
  sed -i 's/AllowRetrieveRestart.*/AllowRetrieveRestart off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/AllowRetrieveRestart.*/AllowRetrieveRestart on/g' $ADDON_HOME/proftpd.conf
fi

if [ "$RootLogin" = "false" ]; then
  sed -i 's/RootLogin.*/RootLogin off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/RootLogin.*/RootLogin on/g' $ADDON_HOME/proftpd.conf
fi

if [ "$TLSRequired" = "false" ]; then
  sed -i 's/TLSRequired.*/TLSRequired off/g' $ADDON_HOME/proftpd.conf
else
  sed -i 's/TLSRequired.*/TLSRequired on/g' $ADDON_HOME/proftpd.conf
fi

cd "$ADDON_DIR/bin"

mkdir -p /var/config/
rm -f /var/config/proftpd.passwd
touch /var/config/proftpd.passwd
chmod 700 /var/config/proftpd.passwd

for NUM in $(seq 1 100); do
  USERNAME=$(eval echo \$Username${NUM})
  USERPASS=$(eval echo \$Userpass${NUM})
  USERPATH=$(eval echo \$Userpath${NUM})

  case "$CryptoHash" in
    "0") password_entry=$(openssl passwd -6 "${USERPASS}");;
    "1") password_entry=$(openssl passwd -5 "${USERPASS}");;
    "2") password_entry=$(openssl passwd -1 "${USERPASS}");;
  esac

  if [ -n "$USERNAME" ]; then
    echo $USERNAME:$password_entry:10:10::$USERPATH:/bin/false >> /var/config/proftpd.passwd
    # user writes with different user/group
    mkdir -p "$USERPATH"
    chmod 777 "$USERPATH"
  fi
done

exec proftpd -n -d $Debuglevel -c $ADDON_HOME/proftpd.conf
