#!/bin/sh -ef

PATH=/usr/lib/alterator-net-common:$PATH
chapfile=/etc/ppp/chap-secrets

################### shell file helpers

shell_add_or_subst()
{
	local name="$1" && shift
	local value="$1" && shift
	local file="$1" && shift

	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file"; then
		/bin/sed -r -i "s,^$name.*,$name$value," -- "$file"
		return 0
	fi
	printf %s\\n "$name$value" >> "$file"
}

################### interface modificators

first_available()
{
	local path="/etc/net/ifaces/ppp"
	local i=0
	while true; do
		[ -d "$path$i" ] || { echo "ppp$i" && break; }
		i=$(($i + 1))
	done
}

list_iface()
{
	echo '('
		for i in `find /etc/net/ifaces -type d -name 'ppp*'`
		do
			[ -f $i/options ] &&
			grep -qs 'PPPTYPE=pptp' $i/options &&
			echo "(\"`basename $i`\")"
		done
	echo ')'
}

list_eth()
{
	echo '('
	iflist|
	    while read name mac;do
		printf '("%s")' "$name"
	    done
	echo ')'
}

read_iface()
{
	local name="/etc/net/ifaces/$1"
	local MPPE="#f" # mppe is off by default with pppd
	grep -q '^require-mppe' "$name/pppoptions" && MPPE="#t"
	echo '('

	local info=
	ifcheckup "$1" && info="`_ "connection established"`" || info="`_ "connection failed"`"
	printf 'info "%s"' "$info"
	
	( PPTP_SERVER=
	  REQUIRES=
	  . $name/options

	printf 'server "%s"' "$PPTP_SERVER"
	printf 'iface "%s"' "$REQUIRES"
	printf 'login "%s"' "$(grep '^user' "$name/pppoptions"|sed -r 's,user[[:space:]]*,,')"
	printf 'mppe %s' "$MPPE"
	
	)

	echo ')'
}

write_iface()
{
	[ "$1" = "/" ] && return
	local name="/etc/net/ifaces/$1"
	local prev_login="$(grep '^user' "$name/pppoptions"|sed -r 's,user[[:space:]]*,,')"
	[ -d "$name" ] || mkdir "$name"
	[ -n "$in_server" ] && shell_add_or_subst "PPTP_SERVER=" "$in_server" "$name/options"
	[ -n "$in_iface" ] && shell_add_or_subst "REQUIRES=" "$in_iface" "$name/options"
	
	if [ -n "$in_login" ] ;then
		sed -r "s,^user[[:space:]].*,user $in_login," -i "$name/pppoptions"
		sed -r "s,^$prev_login[[:space:]]*,$in_login	," -i "$chapfile"
		prev_login="$in_login"
	fi
	if [ -n "$in_password" ]; then
		in_password=$(echo "$in_password"|sed -e 's/["]/\\\\&/g')
		sed -r "s,^$prev_login[[:space:]].*,$prev_login	*	\"$in_password\"	*," -i "$chapfile"
	fi

	set_mppe

	ifdown "$1" && ifup "$1"
}

new_iface()
{
	grep -qs "^$in_login[[:space:]]" "$chapfile" && return 1

	local name="/etc/net/ifaces/$1"
	in_password=$(echo "$in_password"|sed -e 's/["]/\\\\&/g')

	[ -d "$name" ] || mkdir "$name"
	shell_add_or_subst "ONBOOT=" "yes" "$name/options"
	shell_add_or_subst "PPPTYPE=" "pptp" "$name/options"
	shell_add_or_subst "PPTP_SERVER=" "$in_server" "$name/options"
	shell_add_or_subst "REQUIRES=" "$in_iface" "$name/options"

	shell_add_or_subst "user " "$in_login" "$name/pppoptions"
	shell_add_or_subst "defaultroute" "" "$name/pppoptions"

	set_mppe

	echo "$in_login	*	\"$in_password\"	*" >>"$chapfile"

	ifup "$1"
	return 0
}


delete_iface()
{
	[ "$1" = "/" ] && return
	local name="/etc/net/ifaces/$1"
	local prev_login="$(grep '^user' "$name/pppoptions"|sed -r 's,user[[:space:]]*,,')"

	sed -r "/^$prev_login[[:space:]]/ d" -i "$chapfile"
	rm -rf "$name"

	ifdown "$1"
}

set_mppe()
{
	if [ -n "$in_mppe" ]; then
		if [ "$in_mppe" = "#t" ]; then
			MPPE_ADD="require-mppe"
			MPPE_DEL="nomppe"
		else
			MPPE_ADD="nomppe"
			MPPE_DEL="require-mppe"
		fi
		sed "/^$MPPE_ADD/d" -i "$name/pppoptions" ||:
		if grep -q "^$MPPE_DEL" "$name/pppoptions"; then
			sed "s,^$MPPE_DEL,$MPPE_ADD," -i "$name/pppoptions" ||:
		else
			echo "$MPPE_ADD" >> "$name/pppoptions"
		fi
	fi
}

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-net-pptp" "$1"
}

. /usr/share/alterator/build/backend3.sh

on_message()
{
	case "$in_action" in
		##information
		constraints)
			local required="$([ "$in_orig_action" = "new" ] && echo "#t" || echo "#f")"
			echo '('
			printf 'server (required #t match ("^[[:alnum:].]+$" "%s") label "%s")' \
				 "$(_ "invalid server name")" "$(_ "Server")"
			printf 'iface (required #t label "%s")' \
				"$(_ "Walk throw interface")"
			printf 'login (required #t label "%s")' \
				"`_ "Login"`"
			printf 'name  (required %s match ("^ppp[0-9]+$"  "%s")  default "%s" label "%s")' \
					"$required" \
					"`_ "should be ppp[number]"`" \
					"$(first_available)" \
					"`_ "Connection"`"
			printf 'password  (required %s label "%s")' \
				"$required" \
				"`_ "Password"`"
			echo ')'
			;;
		##actions
		list)
			if [ "$in__objects" = "/" ];then
				list_iface
			else
				list_eth
			fi
			;;
		read) 
			if [ "$in__objects" = "/" ];then
				echo '()'
			else
				read_iface "$in__objects"
			fi
			;;
		write)
			write_iface "$in__objects"
			echo '()'
			;;
		new) 
			if new_iface "$in_name"; then
				echo '()'
			else
				printf '(error "%s")' "`_ "same login name already in use"`"
			fi
			;;
		delete)
			delete_iface "$in__objects"
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop

