#!/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. */
#  Remove reandOnly
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 AllowedNumber="1234567890"
declare -r AllowedFtn="1234567890.:/"

echo  "#=================================================="
echo '# Show readonly list of all echos:'
echo "# in config file INSTALLDIR/usr/etc/fido/readonly.lst:"
echo  "#=================================================="
cat INSTALLDIR/usr/etc/fido/readonly.lst | grep --binary-files=text -v "#" |  $SEDT  '/ReadOnly/!d' | sort 
echo  "#=================================================="
echo
echo  "=================================================="
echo "Now enter point or node FTN number for who you whould like to remove readOnly."
echo "Sample: "
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

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 'To remove readonly you need input FTN number.'
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 'To remove readonly you need input FTN number.'
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`

echo
echo  "=================================================="
echo "Checking if this link exist..."
echo  "=================================================="
echo

ARR0=(" ""$ftnnumber1"" ")
Z=`INSTALLDIR/usr/bin/listlink  | grep -v "#" | grep "Aka " | grep -e "${ARR0[*]}" `
if [ -z "$Z" ]
then
echo "No such link in configuration."
M="1"
else
echo "Found link "$ftnnumber"."
fi

echo
echo  "=================================================="
echo "Checking if this point exist..."
echo  "=================================================="
echo
Y=`INSTALLDIR/usr/bin/viewpoint | grep -v "#" | grep "Aka " | grep -e "${ARR0[*]}" `
if [ -z "$Y" ]
then
echo "No such point."
if [ "$M" = "1" ];then
rm -rf $TMP
exit
fi
else
echo "Found point "$ftnnumber"."
fi

echo
echo  "=================================================="
echo "Generationg list of readonly lists echoes for "$ftnnumber"..."
echo  "=================================================="
echo
echo "Parsing of readonly in INSTALLDIR/usr/etc/fido/readonly.lst"
echo

grep "ReadOnly " INSTALLDIR/usr/etc/fido/readonly.lst | grep -v "#" | grep -e "${ARR0[*]}"   > $TMP/listechoread.info.tmp

ECHOINFO=`cat $TMP/listechoread.info.tmp`

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


if [ $NUMBER = "0" ];then
echo
echo  "=================================================="
echo "No echos with readOnly for "$ftnnumber" in " 
echo "INSTALLDIR/usr/etc/fido/readonly.lst"
echo  "=================================================="
echo
rm -rf $TMP
exit
fi

cat $TMP/listechoread.info.tmp | grep -v "#"| sort  > $TMP/echoselread.info.tmp

NUMBER1=`cat $TMP/echoselread.info.tmp | wc -l`
i=1
NUMBER1=$(($NUMBER1+1))
while [ "$i" -ne "$NUMBER1" ]
do
echo " "$i" `$SEDT -n ""$i"p" $TMP/echoselread.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

echo
if [ "$answer" -lt "$NUMBER1" -a "$answer" -gt 0 ]; then
echo  "=================================================="
echo
echo "You choosed:"
echo " "$answer" `$SEDT -n ""$answer"p" $TMP/echoselread.info.tmp`"
$SEDT -n ""$answer"p" $TMP/echoselread.info.tmp > $TMP/echoread.tmp
ECHONAME=`cat $TMP/echoread.tmp`

$SEDT -i "s|:|\\\:|" $TMP/echoread.tmp
$SEDT -i "s|/|\\\/|" $TMP/echoread.tmp
#$SEDT -i "s|.|\\\.|" $TMP/echoread.tmp

ECHONAMEX=`cat $TMP/echoread.tmp`

$SEDT -i "/$ECHONAMEX/d" INSTALLDIR/usr/etc/fido/readonly.lst

echo
echo  "=================================================="
echo "readOnly for "$ECHONAME" removed."
echo  "=================================================="
echo
rm -rf $TMP
fi
rm -rf $TMP
