#!/bin/bash

# **************************************************************************
# *                                                                        *
# *  FidoNMS by Alexey Khromov AKA zxalexis AKA 2:5030/723                 *
# *                                                                        *
# **************************************************************************
# *                                                                        *
# * postfaq - post file from internet to localechoes                                  *
# *                                                                        *
# *                                                                        *
# **************************************************************************

NMSBASE=""
FIDOBASE=""

# Read parameters
while [ -n "$1" ]
do
        case "$1" in
                -f)     cfgfile="$2"
                        shift;;
                *)      cmdusage="1";;
        esac
        shift
done

if [[ "${cmdusage}" = "1" ]]; then
        echo -ne "Usage: \n\
postfaq {-f <cfgfile>}\n"
        exit -2
fi

pwdsource=${BASH_SOURCE[0]}
while [ -L "$pwdsource" ]; do
        pwddir=$( cd -P "$( dirname "$pwdsource" )" > /dev/null 2>&1 && pwd )
        pwdsource=$( readlink "$pwdsource" )
        [[ $pwdsource != /* ]] && pwdsource=$pwddir/$pwdsource
done
pwddir=$( cd -P "$( dirname "$pwdsource" )" > /dev/null 2>&1 && pwd )

if [ -f /etc/fidonms ]; then
    source /etc/fidonms
fi
if [ -z $NMSBASE ]; then
    #Try to guess
    NMSBASE=/opt/fidonms
fi
if [ -z $FIDOBASE ]; then
    FIDOBASE=/var/spool/ftn
fi

if [[ -z ${cfgfile} ]]; then
        if [[ -f "${NMSBASE}/etc/echofaq.tbl" ]]; then
                cfgfile="${NMSBASE}/etc/echofaq.tbl"
        elif [[ -f "/etc/echofaq.tbl" ]]; then
                cfgfile="/etc/echofaq.tbl"
        elif [[ -f "$pwddir/etc/echofaq.tbl" ]]; then
                cfgfile="$pwddir/etc/echofaq.tbl"
        else
                echo "Config file not found. Please specify one"
                exit -2
        fi
fi

echo "Applying cfgfile: ${cfgfile}"

# --------------------------------------------------------------------------
# Here comes a script
# --------------------------------------------------------------------------

if [ -f ${FIDOCONFIG} ];
then
    AKA=$(grep -ie ^Address ${FIDOCONFIG}| head -n1 | awk -F'[ @]' '{ print $2 }')
    echo "Post echorules for ${AKA}"
else
    echo "No FIDOCONFIG var found..."
    break;
fi

BIN_TXT2PKT=$(which txt2pkt)
LOCALINB=$FIDOBASE/inbound-local

#Declaring arrays
declare -a E_NAME=()
declare -a E_CHS=()
declare -a E_FILE=()

while read -r eline;
	do
#		echo ${eline}
		EARG1=$(echo -e ${eline} | awk '{ printf $1 }')
		EARG2=$(echo -e ${eline} | awk '{ printf $2 }')
		EARG3=$(echo -e ${eline} | awk '{ printf $3 }')
		E_NAME+=(${EARG1})
		E_CHS+=(${EARG2})
		E_FILE+=(${EARG3})
	done < ${cfgfile}

for eidx in ${!E_NAME[@]}; do
	echo "PostRULES: idx: ${eidx}, E_NAME: ${E_NAME[$eidx]}, E_FILE: ${E_FILE[$eidx]}"
	curl ${E_FILE[$eidx]} | iconv -f ${E_CHS[$eidx]} -t cp866 > ${FIDOBASE}/tmp/${E_NAME[$eidx]}.faq
	$BIN_TXT2PKT -nf "Vyborg Station robot" -xf "${AKA}".998  -xt "${AKA}"  -t "Powered by FidoNMS(Node Management System)" -o "fido.zxalexis.ru" -s "${E_NAME[$eidx]} FAQ" -e "${E_NAME[$eidx]}" -d "$LOCALINB" ${FIDOBASE}/tmp/${E_NAME[$eidx]}.faq
	rm -f ${FIDOBASE}/tmp/${E_NAME[$eidx]}.faq
	done

touch ${FIDOBASE}/flags/toss