#!/bin/bash

# **************************************************************************
# *                                                                        *
# *  Pointtools v.0.3 by Alexey Khromov AKA zxalexis AKA 2:5030/723        *
# *                                                                        *
# **************************************************************************
# *                                                                        *
# * mkpointcfg - generate Links, Passwords and Pointlist files from        *
# *     userlist file                                                      *
# *                                                                        *
# **************************************************************************

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\
mkpointcfg {-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/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

echo "Applying config: ${cfgfile}"
source ${cfgfile}

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

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

if [ -f ${NMSBASE}/etc/bforce.passwd ]; then
    BFORCE_PASSWD=${NMSBASE}/etc/bforce.passwd
fi

if [ -f ${USERLIST} ];
then
    echo -en "${PWDS_HEADER}" > ${BINKD_PASSWD}

    echo -en "${PWDS_HEADER}" > ${BFORCE_PASSWD}
    echo -en "${LINKS_HEADER}" > ${HPT_POINTS}
    
    echo -en "${PVT_HEADER}\n" > ${POINTLIST}
    NODENUM=$(echo ${AKA}| awk -F'/' '{ print $2 }')
    echo -en "Boss,${NODENUM}" >> ${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-) 
	    PSW=$(echo ${line} | awk -F'|' '{ print $2 }')
	    LNKGRP=$(echo ${line} | awk -F'|' '{ print $3 }')
	    ACCGRP=$(echo ${line} | awk -F'|' '{ print $4 }')
	    # Make a passwd file for binkd
	    echo -e "${AKA}.${POINT}\t\t${PSW}" >> ${BINKD_PASSWD}
	    if [ -n "$BFORCE_PASSWD" ]; then
                echo -e "password\t\t${AKA}.${POINT}\t\t${PSW}" >> ${BFORCE_PASSWD}
            fi
	    # Make a links file for husky-hpt
	    echo -e "Link ${LNKNAME}" >> ${HPT_POINTS}
	    echo -e "Aka ${AKA}.${POINT}" >> ${HPT_POINTS}
	    echo -e "OurAka ${AKA}" >> ${HPT_POINTS}
	    echo -e "Packer ${DefPacker}" >> ${HPT_POINTS}
	    echo -e "Password ${PSW}" >> ${HPT_POINTS}
	    echo -e "NetMailFlavour hold" >> ${HPT_POINTS}
	    echo -e "EchoMailFlavour hold" >> ${HPT_POINTS}
	    echo -e "FileEchoFlavour hold" >> ${HPT_POINTS}
	    echo -e "packNetMail ${DefPackNetMail}" >> ${HPT_POINTS}
	    echo -e "arcNetMail ${DefArcNetMail}" >> ${HPT_POINTS}
	    echo -e "LinkGrp ${LNKGRP}" >> ${HPT_POINTS}
	    echo -e "AccessGrp ${ACCGRP}" >> ${HPT_POINTS}
	    echo -e "\n" >> ${HPT_POINTS}
	
	    # 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
