#!/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. */
#  change password for point or link 
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

declare -r AllowedFtn="1234567890.:/"
declare -r AllowedPwd="1234567890-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

INSTALLDIR/usr/bin/listpoint
echo
INSTALLDIR/usr/bin/listlink | grep "Aka "| $SEDT "s|Aka ||g"
echo
echo  "=================================================="
echo "This program change password for link or point..."
echo  "=================================================="
echo "Parsing of INSTALLDIR/usr/etc/fido/link.lst"
sleep 1
echo "Parsing of INSTALLDIR/usr/etc/fido/point.lst"
sleep 1
echo  "=================================================="
echo

echo "Now enter point or link FTN address for which you would like to change password"
echo -n ":"
read ftnnumber
if [ -z "$ftnnumber" ]
then
echo 'To change password need input FTN address link or point.'
echo "You input nothing. Try to input next try. Exiting."
rm -rf $TMP
exit
fi

ScrubbedCheck3="${ftnnumber//[^$AllowedFtn]/}"
if [ "$ftnnumber" = "$ScrubbedCheck3" ]; then
echo 
else
echo
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$AllowedFtn""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP
exit
fi

CHECKFTN=`echo "$ftnnumber" | 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 "$ftnnumber" | grep ":" `
if [ -z "$CHECKFTN1" ]
then
echo 'No symbol : in FTN number.'
echo "Try to input next try. Exiting."
rm -rf $TMP
exit
fi


# Inserting \ before . as dot alway brings problems with regexp
echo  "$ftnnumber" | $SEDT 's|\.|\\\.|' > $TMP/fidoiptmp
ftnnumber1=`cat $TMP/fidoiptmp`

ARR0=(" ""$ftnnumber1"" ")
echo "Checking if this FTN number exist in binkd.cfg configuration file."
Z=`cat INSTALLDIR/usr/etc/binkd.cfg | grep "##" | grep -e "${ARR0[*]}" | $SEDT "s|##.*||g" | $SEDT "s| c .*||g" `
if [ -z "$Z" ]
then
echo "No such point or link number in configuration files. Exit."
rm -rf $TMP
exit
else
echo 

cat INSTALLDIR/usr/etc/binkd.cfg | grep "##" | grep -e "${ARR0[*]}" | $SEDT "s|##.*||g" | $SEDT "s| c .*||g" > $TMP/testpass
CHE=`$SEDT -n ""2"p" $TMP/testpass`
if [ "$CHE" = "" ];
then
echo
else
echo "Not one password in configuration. Seems 2 or more your links or points have same password"
echo "Cannot change password, exit."
rm -rf $TMP
exit
fi

cat INSTALLDIR/usr/etc/fido/link.lst | grep -v "#" | grep -v "route" | grep -A 2 -e "${ARR0[*]}" | grep "password " |   $SEDT "s|password ||g"  | $SEDT  "s| ||g" > "$TMP"/testpass1
CHE1=`$SEDT -n ""1"p" $TMP/testpass1`
if [ "$CHE1" = "" ];
then
cat INSTALLDIR/usr/etc/fido/point.lst | grep -v "#" | grep -v "route" | grep -A 2 -e "${ARR0[*]}" | grep "password " |   $SEDT "s|password ||g"  | $SEDT  "s| ||g"  > "$TMP"/testpass2
GETPASSWORD=`$SEDT -n ""1"p" $TMP/testpass2`
else
GETPASSWORD=`$SEDT -n ""1"p" $TMP/testpass1`
fi

echo
echo  "=================================================="
echo "Changing password "$GETPASSWORD" to new one..."
echo  "=================================================="
echo
echo "Enter new password for "$ftnnumber" and press [ENTER]."
echo "(not bigger then 8 symbols)"
echo -n "Sample -  09876543: " 
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/passtest
CHE3=`$SEDT -n ""1"p" "$TMP"/passtest`
if [ "$CHE3" = "" ];
then
echo
else
echo "Seems 1 link has same password as you just entered."
echo "Cannot add this password, exit."
echo "Please 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/passtest1
CHE4=`$SEDT -n ""1"p" $TMP/passtest1`
if [ "$CHE4" = "" ];
then
echo
else
echo "Seems 1 point has same password as you just entered."
echo "Cannot add this password, exit."
echo "Please run this script again and modify this password."
rm -rf $TMP
exit
fi


ARR1=(" ""$GETPASSWORD"" ")
ARR2=(" ""$linkpassword"" ")

echo
echo  "=================================================="
echo "Changing password "$GETPASSWORD" to "$linkpassword" in files:"
echo  "=================================================="

echo "INSTALLDIR/usr/etc/binkd.cfg"
echo "INSTALLDIR/usr/etc/golded+/macro.cfg"

$SEDT -i -E "s|${ARR1[*]}|${ARR2[*]}|" INSTALLDIR/usr/etc/binkd.cfg 
$SEDT -i -E "s|${ARR1[*]}|${ARR2[*]}|" INSTALLDIR/usr/etc/golded+/macro.cfg

if [ "$CHE1" = "" ];
then
echo "INSTALLDIR/usr/etc/fido/point.lst"
$SEDT -i -E "s|${ARR1[*]}|${ARR2[*]}|g" INSTALLDIR/usr/etc/fido/point.lst
else
echo "INSTALLDIR/usr/etc/fido/link.lst"
$SEDT -i -E "s|${ARR1[*]}|${ARR2[*]}|g" INSTALLDIR/usr/etc/fido/link.lst
fi
rm -rf $TMP
exit
fi
rm -rf $TMP

