#!/usr/bin/env bash
#   Copyright (c) by Falcony (2:5020/828.777). This file is part of fidoip. It is free software and it is covered
#   by the GNU general public license. See the file LICENSE for details. */
# Add new link to INSTALLDIR/usr/etc/binkd.cfg and INSTALLDIR/usr/etc/fido/routing.lst route and additional links to INSTALLDIR/usr/etc/fido/link.lst
SYSOS=`uname -s` # Packages architecture
OSTYPE=`uname -o 2>/dev/null || uname -p` 
TMP=`mktemp -d`	# Location of temp's files
SEDT="sed"  # Sed program type

T1="root"
T2="Linux"
T3="FreeBSD"
T4="DragonFly"
T5="NetBSD"
T6="OpenBSD"
T7="Cygwin" 
T8="Msys" 

if [ "$SYSOS" = "$T2" ]; then
Z1="Linux"
SEDT="sed"
fi

if [ "$SYSOS" = "$T3" ]; then
Z1="BSD"
SEDT="gsed"
fi

if [ "$SYSOS" = "$T4" ]; then
Z1="BSD"
Z2="PKGSRC"
SEDT="gsed"
fi

if [ "$SYSOS" = "$T5" ]; then
Z1="BSD"
Z2="PKGSRC"
SEDT="gsed"
fi

if [ "$SYSOS" = "$T6" ]; then
Z1="BSD"
SEDT="gsed"
fi

if [ "$OSTYPE" = "Cygwin" ]; then
Z1="Cygwin"
SEDT="sed"
fi

if [ "$OSTYPE" = "Msys" ]; then
Z1="Msys"
SEDT="sed"
fi


# Declaration of allowed symbol for user input scrubbing
declare -r AllowedChars="1234567890/., :-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
declare -r AllowedNumbers="1234567890/.:"
declare -r AllowedFtnNumbers="1234567890/:"
declare -r Alloweddns="-1234567890. -abcdefghijklmnopqrstuvwxyz"
declare -r AllowedPwd="1234567890-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
NODENUMBER=`cat INSTALLDIR/usr/etc/binkd.cfg | grep node | grep ".1 - " | $SEDT "s| -.*||g" | $SEDT "s|node ||g" | $SEDT "s|\\..*||g" `
NODENUMBER1=`echo $NODENUMBER | $SEDT 's|/|\\\/|g'`
station=`cat INSTALLDIR/usr/etc/binkd.cfg | grep sysname | $SEDT "s/sysname//g" | $SEDT 's|"||g' | $SEDT 's| ||g' `

INSTALLDIR/usr/bin/listlink
echo  
echo  "=================================================="
echo "This script list add link of fidoip's FIDONet node."
echo  "=================================================="
echo
echo -e "Enter your link full name and press press [ENTER]."
echo -n "Sample -  Stepan Zapasov: "
read linkname
if [ -z "$linkname" ]
then
echo 'You input nothing.'
echo 'Please run this script again and input something.'
rm -rf $TMP
exit
fi

# Checking user input&scrubbing
ScrubbedCheck4="${linkname//[^$AllowedChars]/}"
if [ "$linkname" = "$ScrubbedCheck4" ]; then
echo  ''
else
echo ' '
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$AllowedChars""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP
exit
fi

# Inserting \ before space
echo  "$linkname" | $SEDT 's/ /\\ /g' > $TMP/fidoiptmp
linkname1=`cat $TMP/fidoiptmp`

ARR0=(" ""$linkname"" ")
INSTALLDIR/usr/bin/listlink  | grep -v "#" | grep "Link" | grep -e "${ARR0[*]}" > $TMP/testlink
CHE0=`$SEDT -n ""1"p" "$TMP"/testlink`
if [ "$CHE0" = "" ];
then
echo
else
echo "Seems 1 links already has same name."
echo "Cannot add such name, exit."
echo "Please run listpoint and listlink commands and check names."
echo "Then start this script again and modify this name."
rm -rf $TMP
exit
fi

INSTALLDIR/usr/bin/viewpoint | grep -v "#" | grep "Link" | grep -e "${ARR0[*]}" > $TMP/testlink1
CHE1=`$SEDT -n ""1"p" "$TMP"/testlink1`
if [ "$CHE1" = "" ];
then
echo
else
echo "Seems 1 point already has same name."
echo "Please run listpoint and listlink commands and check names."
echo "Then start this script again and modify this name."
rm -rf $TMP
exit
fi


# Changing all space to _ 
echo  "$linkname" | $SEDT 's/ /\_/g' > $TMP/fidoiptmp
linkname2=`cat $TMP/fidoiptmp`


echo -e "Enter your link FTN address and press [ENTER]."
echo -n "Sample -  2:5020/732: "
read linkftnaddress
if [ -z "$linkftnaddress" ]
then
echo 'You input nothing.'
echo 'Please run this script again and input something.'
rm -rf $TMP
exit
fi

CHECKFTN=`echo "$linkftnaddress" | grep "/" `
if [ -z "$CHECKFTN" ]
then
echo 'No symbol / in FTN number.'
echo "Try to input next try. Exiting."
rm -rf $TMP
exit
fi

CHECKFTN1=`echo "$linkftnaddress" | grep ":" `
if [ -z "$CHECKFTN1" ]
then
echo 'No symbol : in FTN number.'
echo "Try to input next try. Exiting."
rm -rf $TMP
exit
fi

# Checking user input&scrubbing
ScrubbedCheck5="${linkftnaddress//[^$AllowedFtnNumbers]/}"
if [ "$linkftnaddress" = "$ScrubbedCheck5" ]; then
echo  ''
else
echo ' '
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$AllowedFtnNumbers""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP
exit
fi

# Inserting \ before /
echo  "$linkftnaddress" | $SEDT 's|/|\\/|g' > $TMP/fidoiptmp
linkftnaddress1=`cat $TMP/fidoiptmp`

SHORTNODENAME=`echo $linkftnaddress | $SEDT 's|.*:||g' | $SEDT 's|/|-|g'`

ARR1=(" ""$linkftnaddress"" ")
INSTALLDIR/usr/bin/listlink  | grep -v "#" | grep "Aka " | grep -e "${ARR1[*]}" > $TMP/testftn1
CHE2=`$SEDT -n ""1"p" "$TMP"/testftn1`
cat "$TMP"/testftn1
if [ "$CHE2" = "" ];
then
echo
else
echo "Seems 1 links already has same FTN address."
echo "Cannot add such number, exit."
echo "Please run listpoint and listlink commands and check FTN numbers."
echo "Then start this script again and modify this address."
rm -rf $TMP
exit
fi

echo "Enter your link DNS name or IP-address and press [ENTER]."
echo -n "Sample - zapasov.dyndns.org: "
read linkdnsaddress
if [ -z "$linkdnsaddress" ]
then
echo 'You input nothing.'
echo 'Please run this script again and input something.'
rm -rf $TMP
exit
fi



# Checking user input&scrubbing
ScrubbedCheck6="${linkdnsaddress//[^$Alloweddns]/}"
if [ "$linkdnsaddress" = "$ScrubbedCheck6" ]; then
echo  ''
else
echo ' '
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$Alloweddns""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP
exit
fi



echo "Enter your link password and press [ENTER]."
echo "(not bigger then 8 symbols)"
echo -n "Sample -  56789012: " 
read linkpassword
if [ -z "$linkpassword" ]
then
echo 'You input nothing.'
echo 'Please run this script again and input something.'
rm -rf $TMP
exit
fi

# Checking user input&scrubbing
ScrubbedCheck7="${linkpassword//[^$AllowedPwd]/}"
if [ "$linkpassword" = "$ScrubbedCheck7" ]; then
echo  ''
else
echo ' '
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$AllowedPwd""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP
exit
fi

grep "password "$linkpassword" " INSTALLDIR/usr/etc/fido/link.lst  > $TMP/testpass
CHE3=`$SEDT -n ""1"p" "$TMP"/testpass`
if [ "$CHE3" = "" ];
then
echo
else
echo "Seems 1 link has same password."
echo "Cannot add this password, exit."
echo "Please run listpoint and listlink commands and check passwords."
echo "Then run this script again and modify this password."
rm -rf $TMP
exit
fi

grep " "$linkpassword" " INSTALLDIR/usr/etc/binkd.cfg  | grep "##" | grep " point " | $SEDT "s|##.*||g" | $SEDT "s/.*-//" | $SEDT "s/ //"  > $TMP/testpass1
CHE4=`$SEDT -n ""1"p" $TMP/testpass1`
if [ "$CHE4" = "" ];
then
echo
else
echo "Seems 1 point has same password."
echo "Cannot add this password, exit."
echo "Please run listpoint and listlink commands and check passwords."
echo "Then start this script again and modify this password."
rm -rf $TMP
exit
fi

cat INSTALLDIR/usr/etc/fidoip/node/link.lst.template > $TMP/link.lst.template-node
cat INSTALLDIR/usr/etc/fidoip/node/routing.lst.template-node > $TMP/routing.lst.template-node 
cat INSTALLDIR/usr/etc/fidoip/node/binklink.template-node > $TMP/binklink.template-node
cat INSTALLDIR/usr/etc/fidoip/node/poll.template-node | grep binkd > $TMP/poll.template-node
cat INSTALLDIR/usr/etc/fidoip/node/macro.cfg.template-node > $TMP/macro.cfg.template-node
cp INSTALLDIR/usr/etc/fidoip/node/announce1.template-node $TMP/announce1.template-node

ARR2=("$linkname1")
$SEDT -i -E "s/LINK-NODE-NAME/${ARR2[*]}/g" $TMP/link.lst.template-node

ARR3=("$linkftnaddress1")
$SEDT -i -E "s/LINK-NODE-ADDRESS/${ARR3[*]}/g" $TMP/link.lst.template-node

ARR31=("$linkftnaddress")
cat INSTALLDIR/usr/etc/fido/route-default.lst  | grep "route crash"   > $TMP/test-route
ROUT0=`cat $TMP/test-route | wc -l`
if [ "$ROUT0" = "0" ];
then
echo "Seems no any uplink for this system."
echo "Adding $linkname as default uplink..."
echo "route crash ${ARR31[*]} *" >> INSTALLDIR/usr/etc/fido/route-default.lst
else
echo
fi

ARR4=("$NODENUMBER1")
$SEDT -i -E "s|MYNODE-ADDRESS|${ARR4[*]}|g" $TMP/link.lst.template-node

ARR5=("$linkpassword")
$SEDT -i -E "s/LINK-NODE-PASSWORD/${ARR5[*]}/g" $TMP/link.lst.template-node

ARR6=("$linkdnsaddress")
$SEDT -i -E "s/LINK-HOST-DOMAIN-NAME/${ARR6[*]}/g" $TMP/binklink.template-node

$SEDT -i -E "s/LINK-NODE-ADDRESS/${ARR3[*]}/g"  $TMP/binklink.template-node
$SEDT -i -E "s/LINK-NODE-PASSWORD/${ARR5[*]}/g" $TMP/binklink.template-node
$SEDT -i -E "s/LINK-NODE-NAME/${ARR2[*]}/g" $TMP/binklink.template-node
$SEDT -i -E "s/LINK-NODE-ADDRESS/${ARR3[*]}/g"  $TMP/routing.lst.template-node
$SEDT -i -E "s/LINK-NODE-ADDRESS/${ARR3[*]}/g" $TMP/poll.template-node

ARR7=("$SHORTNODENAME")
$SEDT -i -E "s/SHORTNODE-NAME/${ARR7[*]}/g" $TMP/macro.cfg.template-node

$SEDT -i -E "s/LINK-NODE-PASSWORD/${ARR5[*]}/g" $TMP/macro.cfg.template-node
$SEDT -i -E "s/LINK-NODE-ADDRESS/${ARR3[*]}/g" $TMP/macro.cfg.template-node


cat $TMP/link.lst.template-node >> INSTALLDIR/usr/etc/fido/link.lst
cat $TMP/routing.lst.template-node >> INSTALLDIR/usr/etc/fido/routing.lst
cat $TMP/binklink.template-node >> INSTALLDIR/usr/etc/binkd.cfg
cat $TMP/poll.template-node >> INSTALLDIR/usr/bin/poll
cat $TMP/macro.cfg.template-node >> INSTALLDIR/usr/etc/golded+/macro.cfg

ARR8=("$station")

MYSHORTNODE=`echo $NODENUMBER | $SEDT 's|.*:||g' | $SEDT 's|/|-|g'`

$SEDT -i "s|LINK-FULL-NAME|${ARR2[*]}|g" $TMP/announce1.template-node
$SEDT -i "s|LINK-ADDRESS|${ARR3[*]}|g" $TMP/announce1.template-node
$SEDT -i "s|MYNODE-ADDRESS|${ARR4[*]}|g" $TMP/announce1.template-node
$SEDT -i "s|SYSTEM-NAME|${ARR8[*]}|g" $TMP/announce1.template-node

echo  "=================================================="
echo "Generating announce message"
echo  "=================================================="
sleep 1
echo
export FIDOCONFIG=INSTALLDIR/usr/etc/fido/config
INSTALLDIR/usr/bin/txt2pkt -nf "Dumb-robot" -xf "$NODENUMBER".1  -xt "$NODENUMBER"  -t "Powered by automatic fidoip NMS(Node Management System)" -o "https://sf.net/projects/fidoip" -s "Link ""$linkftnaddress"" is created " -e ""$MYSHORTNODE".official" -d INSTALLDIR/fido/localinb $TMP/announce1.template-node


INSTALLDIR/usr/bin/toss

echo  "=================================================="
echo "Link "$linkname" "$linkftnaddress" added to configuration files:"
echo  "=================================================="
echo "INSTALLDIR/usr/etc/binkd.cfg"
echo "INSTALLDIR/usr/etc/fido/link.lst"
echo "INSTALLDIR/usr/etc/fido/routing.lst"
echo "INSTALLDIR/usr/etc/golded+/macro.cfg"
echo "INSTALLDIR/usr/bin/poll"
echo  "=================================================="

rm -rf $TMP

