#!/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 readOnly for echo 
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

echo  "=================================================="
echo "Generationg list of active echoes..."
echo "Parsing of echo in INSTALLDIR/usr/etc/fido/config."
echo  "=================================================="
echo
INSTALLDIR/usr/bin/listecho | grep --binary-files=text -v "#" |  $SEDT "s|.*/||g" | $SEDT '/./!d' > $TMP/listecho.info.tmp

NUMBER=`cat $TMP/listecho.info.tmp | wc -l`

declare -r AllowedNumber="1234567890"
declare -r AllowedFtn="1234567890.:/"

if [ $NUMBER = "0" ];then
echo "No active echos found."
rm -rf $TMP 
exit
fi

cat $TMP/listecho.info.tmp | sort | $SEDT "s|#.*||g" > $TMP/echosel.info.tmp

NUMBER1=`cat $TMP/echosel.info.tmp | wc -l`
i=1
NUMBER1=$(($NUMBER1+1))
while [ "$i" -ne "$NUMBER1" ]
do
echo " "$i" `$SEDT -n ""$i"p" $TMP/echosel.info.tmp`"
i=$(($i+1))
done

echo "Enter number to choose variant from the list:"
read -p " ?" answer
# Checking user input&scrubbing
ScrubbedCheck4="${answer//[^$AllowedNumber]/}"
if [ "$answer" = "$ScrubbedCheck4" ]; then
echo
else
echo
echo " Error. You entered wrong symbols. Allowed symbols are: "
echo -n ' '
echo -n ""$AllowedNumber""
echo -n '               '
echo 'Please run this script again and be more carefull during inputing.'
echo -n '               '
rm -rf $TMP 
exit
fi

if [ "$answer" -lt "$NUMBER1" -a "$answer" -gt 0 ]; then
echo
echo
echo  "=================================================="
echo "You choosed:"
echo " "$answer" `$SEDT -n ""$answer"p" $TMP/echosel.info.tmp`"
echo  "=================================================="
$SEDT -n ""$answer"p" $TMP/echosel.info.tmp > $TMP/echo.tmp
ECHONAME=`cat $TMP/echo.tmp`
echo
INSTALLDIR/usr/bin/listlink | grep "Aka "
echo
echo "Here is list of points:"
INSTALLDIR/usr/bin/listpoint | grep -v "#"
echo
echo "Now enter point or node full FTN number to setup readOnly."
echo "Sample - 2:5020/828.5: "
echo -n ":"
read ftnnumber
if [ -z "$ftnnumber" ]
then
echo "To setup readOnly you need input FTN number."
echo "You input nothing. Try to input next try. Exiting."
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


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

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

ARR0=(" ""$ftnnumber1"" ")
ARR1=("$ftnnumber"" ""$ECHONAME")
INSTALLDIR/usr/bin/listlink  | grep -v "#" | grep "Aka " | grep -e "${ARR0[*]}" > $TMP/testlink
CHE0=`$SEDT -n ""1"p" "$TMP"/testlink`
if [ "$CHE0" = "" ];
then
echo
else
echo "Found 1 links with such FTN number."
echo "Setting up readOnly..."
echo "ReadOnly ${ARR1[*]}"
echo "ReadOnly ${ARR1[*]}" >> INSTALLDIR/usr/etc/fido/readonly.lst
rm -rf $TMP
exit
fi

INSTALLDIR/usr/bin/viewpoint | grep -v "#" | grep "Aka " | grep -e "${ARR0[*]}" > $TMP/testlink1
CHE1=`$SEDT -n ""1"p" "$TMP"/testlink1`
if [ "$CHE1" = "" ];
then
echo
else
echo "Found 1 point with such FTN number."
echo "Setting up readOnly..."
echo "ReadOnly ${ARR1[*]}"
echo "ReadOnly ${ARR1[*]}" >> INSTALLDIR/usr/etc/fido/readonly.lst
rm -rf $TMP
exit
fi
rm -rf $TMP
fi
rm -rf $TMP


