#!/opt/bin/sh
partLabel=""

get_part_label() {
if [ "$partLabel" != "" ]; then return; fi 
while true
do
data=$(ls /tmp/mnt -F | grep -o "@")
if [ "$(ls /tmp/mnt -F | grep -o "@")" == "" ]; then echo; echo "No partitions were found. Plug your USB drive to your Keenetic and press any key to continue."; else break; fi
read userInput
done
echo; echo "All the partitions of your USB drive are specified below:"; echo
ls /tmp/mnt -F | grep "@" | sed "s/@//g"; echo
while true
do
	echo -n "Input the label of the partition for writing audiofiles: "
	read partLabel
	if [ "$(ls /tmp/mnt/ | grep -x ${partLabel})" == "$partLabel" ] 
	then break
	else echo "ERROR! Partition '${partLabel}' doesn't exist!"
	fi
done
echo "Partition '${partLabel}' was selected."
}

call_rec() {
sed -i 's/;same => n,MixMonitor/same => n,MixMonitor/g' /opt/etc/asterisk/extensions.conf
}

start_cron() {
opkg update
opkg install cron
mkdir /opt/etc/cron.asterisk
cp /opt/etc/asterisk/scripts/wavtomp3 /opt/etc/cron.asterisk/
echo "03 * * * * root /opt/bin/run-parts /opt/etc/cron.asterisk" >> /opt/etc/crontab
sed -i '6s/^/#/' /opt/etc/crontab
sed -i '7s/^/#/' /opt/etc/crontab
sed -i '8s/^/#/' /opt/etc/crontab
sed -i '9s/^/#/' /opt/etc/crontab
sed -i '10s/^/#/' /opt/etc/crontab
sed -i '11s/^/#/' /opt/etc/crontab
/opt/etc/init.d/S10cron start
/opt/etc/init.d/S10cron status
}

no_call_rec() {
if [ "$(cat /opt/etc/asterisk/extensions.conf | grep -o ";same => n,MixMonitor")" != "" ]; then return; fi
sed -i 's/same => n,MixMonitor/;same => n,MixMonitor/g' /opt/etc/asterisk/extensions.conf
if [ "$(ls /opt/etc/ | grep -o "crontab")" == "" ]; then return; fi
if [ "$(cat /opt/etc/crontab | grep -o "#03 * * * * root /opt/bin/run-parts /opt/etc/cron.asterisk")" != "" ]; then return; fi
sed -i 's/03 * * * */#03 * * * */g' /opt/etc/crontab
restart_ast
}

make_dir() {
if [ "$(ls /tmp/mnt/${partLabel} | grep "asterisk")" == "" ]; then mkdir /tmp/mnt/${partLabel}/asterisk; fi
if [ "$(ls /tmp/mnt/${partLabel}/asterisk | grep "voicemail")" == "" ]; then mkdir /tmp/mnt/${partLabel}/asterisk/voicemail; fi
if [ "$(ls /tmp/mnt/${partLabel}/asterisk | grep "records")" == "" ]; then mkdir /tmp/mnt/${partLabel}/asterisk/records; fi
}

replace_label() {
sed -i "s/USBDISK/${partLabel}/g" /opt/etc/asterisk/extensions.conf
sed -i "s/USBDISK/${partLabel}/g" /opt/etc/asterisk/scripts/wavtomp3
}

symlink_to_usb() {
if [ "$(ls /opt/var/spool/asterisk | grep -o "voicemail")" == "voicemail" ]
then rm /opt/var/spool/asterisk/voicemail -r
fi
cd /opt/var/spool/asterisk && ln -s /tmp/mnt/${partLabel}/asterisk/voicemail
}

remove_core_sounds() {
if [ "$(ls /opt/share/asterisk/sounds | grep -o "en")" == "en" ]; then rm /opt/share/asterisk/sounds/en -r; echo "Asterisk Core Sounds EN have been removed."; fi
if [ "$(ls /opt/share/asterisk/sounds | grep -o "ru")" == "ru" ]; then rm /opt/share/asterisk/sounds/ru -r; echo "Asterisk Core Sounds RU have been removed."; fi
}

enable_voicemail() {
sed -i 's/;same => n,GoSub(sub-startvoicemail,s,1)/same => n,GoSub(sub-startvoicemail,s,1)/g' /opt/etc/asterisk/extensions.conf
sed -i 's/;include => voicemail/include => voicemail/g' /opt/etc/asterisk/extensions.conf
}

disable_voicemail() {
if [ "$(cat /opt/etc/asterisk/extensions.conf | grep -o ";same => n,GoSub(sub-startvoicemail,s,1")" != "" ]; then return; fi
sed -i 's/same => n,GoSub(sub-startvoicemail,s,1)/;same => n,GoSub(sub-startvoicemail,s,1)/g' /opt/etc/asterisk/extensions.conf
sed -i 's/include => voicemail/;include => voicemail/g' /opt/etc/asterisk/extensions.conf
restart_ast
}

start_ast() {
/opt/etc/init.d/S31asterisk start
sleep 1
/opt/etc/init.d/S31asterisk status
}

restart_ast() {
asterisk -x "core restart now"
}

check_nvox() {
ndmc -c "show version" | grep -o "nvox"
}
#назначить порты RTP, открыть в firewall порты SIP и RTP

telephony_type() {
local nvoxInfo="$(ndmc -c "show nvox info" | grep "tel_type:")" 
nvoxInfo=${nvoxInfo:23}
echo $nvoxInfo
}

usb_module() {
local data=$(ndmc -c "show usb" | grep -o "Keenetic Linear")
if [ "$data" == "" ]; then data=$(ndmc -c "show usb" | grep -o "Keenetic Plus DECT"); fi
echo $data
}

wait_asterisk() {
while true
do
if [ "$(asterisk -x "pjsip show transports" | grep -o "TransportId")" == "TransportId" ]; then return; fi
sleep 1
done
}

#============================ MAIN ============================
#
echo
echo "This script will help you to configure the following features:"
echo
echo "  1) call recording"
echo "  2) voice mail"
echo "  3) SIP TLS transport"

#### Call recording
echo
echo -n "Do you want to record calls to USB drive plugged into your Keenetic? <y/n> "; read userInput
if [ "$userInput" == "y" ]
then
	#### Format USB drive
	echo "You need USB drive with ext4 partition."; echo -n "Do you want to format your USB drive to ext4? <y/n> "; read userInput
	if [ "$userInput" == "y" ]; then . disk-to-ext4; fi
	get_part_label; make_dir; call_rec; replace_label; restart_ast
	#### wav to mp3
	echo
	echo -n "Do you want to automatically convert audiofiles on your USB drive from pcm to mp3 to reduce their size by about 15 times? <y/n> "; read userInput
	if [ "$userInput" == "y" ]
	then 
	start_cron; echo "Convertation process will start hourly."
	fi
	echo "BTW, you can run the script 'wavtomp3' to convert all audiofiles to mp3 immediately."	
else
	no_call_rec	
fi

#### Voicemail
echo
echo -n "Do you want to use the voicemail features with recording messages to an external USB drive? <y/n> "; read userInput
if [ "$userInput" == "y" ]
then
	if [ "$partLabel" == "" ]
	then 
		echo "You need USB drive with ext4 partition."; echo -n "Do you want to format your USB drive to ext4? <y/n> "; read userInput
		if [ "$userInput" == "y" ]
		then . disk-to-ext4
		fi
	get_part_label 
	fi
	make_dir
	echo "Creating symlink to the voicemail directory"
	symlink_to_usb
	enable_voicemail
	restart_ast
	echo "Done."
else
	disable_voicemail
	#### Asterisk Core Sounds
	echo; echo -n "Do you want to remove Asterisk Core Sounds files to free up 20 MB? <y/n> "; read userInput
	if [ "$userInput" == "y" ]; then remove_core_sounds; fi
fi

#### SIP TLS transport
if [ "$(asterisk -x "pjsip show transports" | grep -o "transport-tls")" == "" ]
then 
 echo
 echo -n "Do you want to configure SIP TLS transport? <y/n> "; read userInput
 if [ "$userInput" == "y" ]
 then
 echo
 echo -n "It will take a few minutes to create the CA key, please wait and do not interrupt the script execution. Press any key to begin."; read userInput
 echo
 /opt/etc/asterisk/keys/scripts/create_ca_ast_cert
 restart_ast
 wait_asterisk
 asterisk -x "pjsip show transports"
 fi
fi


exit

