#!/bin/sh

LOG=/usr/local/psa/var/log/amazon
PROFTPD_CONF=/etc/proftpd.conf
PROFTPD_CONF_BAK=$PROFTPD_CONF.bak
PROFTPD_CONF_TMP=$PROFTPD_CONF.tmp


IP=$1
IPchk=`echo $IP | sed -e "s/[0-9]\{1,3\}\\\\.[0-9]\{1,3\}\\\\.[0-9]\{1,3\}\\\\.[0-9]\{1,3\}//g"`


if [ -z "$1" -o "$1" = "--help" -o ! -z "$IPchk" ]; then
	echo $0 \<external_ip\>
	exit 1;
fi

date >> $LOG
echo $0 $* >> $LOG


######################################################################
# ProFTPd
######################################################################

# Make a backup
cp $PROFTPD_CONF $PROFTPD_CONF_BAK
ERR=$?

# If backup fails, there is a problem
if [ $ERR -ne 0 ]; then
	echo ERROR making a backup copy of $PROFTPD_CONF to $PROFTPD_CONF_BAK
	exit 2;
fi


# Remove uncommented MasqueradeAddress
grep -v -E ^[^#]\ *M?asqueradeAddress $PROFTPD_CONF > $PROFTPD_CONF_TMP

# Add MasqueradeAddress with specified IP
echo MasqueradeAddress $IP >> $PROFTPD_CONF_TMP

# Check if there is PassivePorts
grep -E ^[^#]\ *P?assivePorts $PROFTPD_CONF_TMP > /dev/null
ERR=$?

# Add PassivePorts if absent
if [ $ERR -ne 0 ]; then
	echo PassivePorts 60000 65535 >> $PROFTPD_CONF_TMP;
fi

# Copy config from the working copy
mv $PROFTPD_CONF_TMP $PROFTPD_CONF



######################################################################
# DNS Templates
######################################################################

#QUERY="UPDATE dns_recs_t SET displayVal='$IP', val='$IP' WHERE type='A';
#UPDATE dns_recs_t SET displayHost='$IP', host='$IP' WHERE type='PTR' AND opt='24';"
#
#echo $QUERY | mysql --user=admin --password=`cat /etc/psa/.psa.shadow` psa


