#!/bin/bash

# **************************************************************************
# *                                                                        *
# *  Pointtools v.0.3 by Alexey Khromov AKA zxalexis AKA 2:5030/723        *
# *                                                                        *
# **************************************************************************
# *                                                                        *
# * pointlist - generate Pointlist file                                    *
# *                                                                        *
# **************************************************************************

NMSBASE=""
FIDOBASE=""

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


# 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\
pointlist {-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 [[ -z ${cfgfile} ]]; then
        if [[ -f "${NMSBASE}/etc/pointcfg" ]]; then
                cfgfile="${NMSBASE}/etc/pointcfg"
        elif [[ -f "/etc/pointcfg" ]]; then
                cfgfile="/etc/pointcfg"
        elif [[ -f "$pwddir/etc/pointcfg" ]]; then
                cfgfile="$pwddir/etc/pointcfg"
        else
                echo "Config file not found. Please specify one"
                exit -2
        fi
fi

source ${cfgfile}

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

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

if [ -f ${USERLIST} ];
then
    echo -en "${PVT_HEADER}\n;\n;\n" > ${POINTLIST}
    NODENUM=$(echo ${AKA}| awk -F'/' '{ print $2 }')
    echo -en "Boss,${AKA}" >> ${POINTLIST}

    while read -r line;
    do
        POINTLINE=$(echo ${line} | awk -F',' '{ print $1 }')
        if [[ ${POINTLINE} =~ ^[Pp][Oo][Ii][Nn][Tt] ]];
        then
            POINT=$(echo ${line} | awk -F',' '{ print $2 }')
            LNKNAME=$(echo ${line} | awk -F',' '{ print $3 }')
            CITY=$(echo ${line} | awk -F',' '{ print $4 }')
            NAME=$(echo ${line} | awk -F',' '{ print $5 }')
            TEL=$(echo ${line} | awk -F',' '{ print $6 }')
            FLAGS=$(echo ${line} | awk -F'|' '{ print $1 }' | cut -d, -f7-)
	    # Make a Pointlist File
            printf "\n,${POINT},${LNKNAME},${CITY},${NAME},${TEL},${FLAGS}" >> ${POINTLIST}
        fi
    done < ${USERLIST}
    echo -en "\n${PVT_FOOTER}" >> ${POINTLIST}
else
    echo "No userlist found! Aborting."
fi

