#!/bin/sh

cc="openvpn"
uebergabe="";
delimiter="#"

wert (){
	IFS=$2;
	local number=$3;
	local c=0;
	uebergabe="";
	for inhalt in $1
	do
		if [ "$c" -eq "$number" ] ; then uebergabe=$inhalt; fi
		c=$((c+1));
	done
	unset IFS;
}
 
count=2;

. /mod/etc/conf/openvpn.cfg
if [ "$OPENVPN_CONFIG_COUNT" -gt 1 ]; then 
	echo "openvpn#default-Config" > /mod/etc/openvpn_multid.pkg ;
else
	rm  -f /mod/etc/openvpn_multid.pkg ;
fi
while [ $count -le $OPENVPN_CONFIG_COUNT ]
do
	IFS='';
	wert $OPENVPN_CONFIG_NAMES $delimiter $count;
	LOKAL_CONFIG_NAMES=$uebergabe;
	IFS='';
	wert $OPENVPN_AUTOSTART $delimiter $count;
	LOKAL_CONFIG_AUTOSTART=$uebergabe; 
	IFS='';
	wert $OPENVPN_OWN_KEYS $delimiter $count;
	LOKAL_OWN_KEYS=$uebergabe; 

	act="openvpn_${LOKAL_CONFIG_NAMES}"
	cc="$cc $act"
	if [ ! -d /mod/etc/default.${act} ]; then
		mkdir /mod/etc/default.${act} ;
	fi
	if [ -e /mod/etc/default.${act}/${act}_conf ]; then
		rm /mod/etc/default.${act}/${act}_conf;
	fi
	if [ -x /tmp/flash/openvpn_conf ]; then
		ln -sf /tmp/flash/openvpn_conf /mod/etc/default.${act}/${act}_conf ;
	else
		ln -sf /mod/etc/default.openvpn/openvpn_conf /mod/etc/default.${act}/${act}_conf ;
	fi
	if [ ! -x /mod/etc/init.d/rc.${act} ]; then
	    ln -s rc.openvpn /mod/etc/init.d/rc.${act}
	fi
	if [ ! -x /mod/sbin/$act ]; then
		ln -s /usr/sbin/openvpn /mod/sbin/$act
	fi



	echo ${act} >> /mod/etc/openvpn_multid.pkg ;
	
	unset IFS=;
	for deffile in `ls /mod/etc/default.openvpn/*.def`
	do
		name=${deffile##*/};
			tmpid=${name%%.*}
			tmpid="OVPN_${LOKAL_CONFIG_NAMES}_$tmpid"
			tmptitle=`grep CAPTION $deffile`
			tmptitle=${tmptitle##*=\'}
			tmptitle=${tmptitle%%(*}
			if [ "$LOKAL_OWN_KEYS" == "yes" ]; then
				 sed "s/openvpn/${act}/g"  $deffile | sed "s/\/flash/\/flash\/${act}/g" | sed "s/VPN/VPN Config ${LOKAL_CONFIG_NAMES}/g" > /mod/etc/default.${act}/$name
				if [ ! -d /tmp/flash/${act} ]; then mkdir /tmp/flash/${act} ; fi
				modreg file "$tmpid" "${LOKAL_CONFIG_NAMES}: $tmptitle" 0 "/mod/etc/default.${act}/$name"
			else
				modunreg file $tmpid
			fi
		done

	upperconfigname="`echo ${LOKAL_CONFIG_NAMES} | tr [a-z]- [A-Z]_`"
	str=`echo "OPENVPN_${upperconfigname}_ENABLED=\"${LOKAL_CONFIG_AUTOSTART}\""`
	echo "$str" > /mod/etc/conf/${act}.cfg
count=$((count+1));
done
unset IFS;

for config in `ls /mod/etc/init.d/rc.openvpn*`; do
	confname=${config##*rc.}
	tmp=`echo $cc | grep $confname`;
	if [ "$tmp" == "" ] ; then
		if [ running = `"$config" status` ]; then
			echo -n "Stop deamon of deleted config: "
			"$config" stop;
		fi
		rm $config
		if [ -d /mod/etc/default.$confname ]; then
			rm -rf /mod/etc/default.$confname;
			rm -rf /tmp/flash/$confname;
		fi
		if [ -e /mod/etc/static.pkg ]; then
			grep -v $confname /mod/etc/static.pkg > /mod/etc/static.pkg.tmp
			mv /mod/etc/static.pkg.tmp /mod/etc/static.pkg
		fi
		if [ -e /mod/etc/reg/file.reg ]; then
			for fn in `grep  "/mod/etc/default.$confname" /mod/etc/reg/file.reg | cut -d "|" -f 1`; do
				modunreg file $fn
			done
		fi
	fi		
done

