#!/bin/sh -x

if [ ! -f /etc/config/smstools3 ]; then
	touch /etc/config/smstools3
	touch /etc/smstools3.user
	touch /etc/smstools3.pb
	uci add smstools3 sms >/dev/null 2>&1
	uci set smstools3.@sms[0].loglevel='1' >/dev/null 2>&1
	uci add smstools3 root_phone >/dev/null 2>&1
	uci set smstools3.@root_phone[0].whitelist_only='1' >/dev/null 2>&1
	uci commit smstools3
elif [ -z "$(uci -q get smstools3.@root_phone[0].whitelist_only)" ]; then
	uci set smstools3.@root_phone[0].whitelist_only='1' >/dev/null 2>&1
	uci commit smstools3
fi

for old_dir in /root/sms /usr/share/sms; do
	if [ -d "$old_dir" ]; then
		if [ ! -d /etc/sms ]; then
			mv "$old_dir" /etc/sms
		else
			mv -f "$old_dir"/* /etc/sms/ 2>/dev/null
			rm -rf "$old_dir"
		fi
	fi
done

if [ "$(readlink /var/spool/sms)" = "/root/sms" ] || [ "$(readlink /var/spool/sms)" = "/usr/share/sms" ]; then
	rm -f /var/spool/sms
	ln -s /etc/sms /var/spool/sms
fi
