#!/bin/sh
#setup proxy server for wget. License GPLv3 (ref: /usr/share/doc/legal).
# created by rerwin. ref: http://murga-linux.com/puppy/viewtopic.php?t=110337
#170705 imported to woofQ, replacing binary (proxy-setup.bac BaCon HUG code).

export TEXTDOMAIN=proxy-setup
export OUTPUT_CHARSET=UTF-8

TITLE=$(gettext 'Proxy server setup')

ENABLE_PROXY=false
HTTP_PROXY="http://foo.org:8080"
FTP_PROXY="ftp://foo.org:8080"

HTTP_PROXY_FILE='/etc/profile.d/http_proxy'
FTP_PROXY_FILE='/etc/profile.d/ftp_proxy'

if [ -f "$HTTP_PROXY_FILE" ] ; then
	HTTP_PROXY=`cat $HTTP_PROXY_FILE | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '`
	USERNAME=`cat $HTTP_PROXY_FILE | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '`
	PASS=`cat $HTTP_PROXY_FILE | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '`
	ENABLE_PROXY=true
fi

if [ -f "$FTP_PROXY_FILE" ] ; then
	FTP_PROXY=`cat $FTP_PROXY_FILE | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '`
	USERNAME=`cat $FTP_PROXY_FILE | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '`
	PASS=`cat $FTP_PROXY_FILE | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '`
	ENABLE_PROXY=true
fi

#-------------------------

while [ 1 ] ; do

	#-- DEFAULTS --

	DEFAULT_HTTP_PROXY=""
	DEFAULT_FTP_PROXY=""
	DEFAULT_USERNAME=""
	DEFAULT_PASS1=""
	DEFAULT_PASS2=""

	[ "$HTTP_PROXY" != "" ] && DEFAULT_HTTP_PROXY="<default>${HTTP_PROXY}</default>"
	[ "$FTP_PROXY" != "" ] && DEFAULT_FTP_PROXY="<default>${FTP_PROXY}</default>"
	[ "$USERNAME" != "" ] && DEFAULT_USERNAME="<default>${USERNAME}</default>"
	if [ "$PASS" != "" ] ; then
		DEFAULT_PASS1="<default>${PASS}</default>"
		DEFAULT_PASS2="<default>${PASS}</default>"
	fi
	[ "$PASS1" != "" ] && DEFAULT_PASS1="<default>${PASS1}</default>"
	[ "$PASS2" != "" ] && DEFAULT_PASS2="<default>${PASS2}</default>"

	#-- DIALOG
	MSG1=$(gettext 'If you connect to the Internet through a proxy server, tick the checkbox and fill in the fields, (leave Username/Password blank if not needed)')
	export PROXY_SETUP_WINDOW='<window title="'${TITLE}'" icon-name="gtk-network" window-position="1">
<vbox space-expand="true" space-fill="true">
	'$(/usr/lib/gtkdialog/xml_info fixed /usr/local/lib/X11/pixmaps/www48.png 48 "$MSG1")'
	<checkbox active="'${ENABLE_PROXY}'">
		<label>'$(gettext 'Enable Internet connection through proxy server')'</label>
		<variable>ENABLE_PROXY</variable>
		<action>if true enable:HTTP_PROXY</action>
		<action>if true enable:FTP_PROXY</action>
		<action>if true enable:USERNAME</action>
		<action>if true enable:PASS1</action>
		<action>if true enable:PASS2</action>
		<action>if false disable:HTTP_PROXY</action>
		<action>if false disable:FTP_PROXY</action>
		<action>if false disable:PASS1</action>
		<action>if false disable:PASS2</action>
		<action>if false disable:USERNAME</action>
	</checkbox>
	<frame>
		<hbox>
			<text xalign="0" space-expand="false"><label>'$(gettext 'HTTP:')'</label></text>
			<entry space-expand="true" sensitive="'${ENABLE_PROXY}'">
				<variable>HTTP_PROXY</variable>
				'${DEFAULT_HTTP_PROXY}'
			</entry>
		</hbox>
		<hbox>
			<text xalign="0" space-expand="false"><label>'$(gettext 'FTP:')'</label></text>
			<entry space-expand="true" sensitive="'${ENABLE_PROXY}'">
				<variable>FTP_PROXY</variable>
				'${DEFAULT_FTP_PROXY}'
			</entry>
		</hbox>
		<hbox>
			<text xalign="0" space-expand="false"><label>'$(gettext 'Username:')'</label></text>
			<entry space-expand="true" sensitive="'${ENABLE_PROXY}'">
				<variable>USERNAME</variable>
				'${DEFAULT_USERNAME}'
			</entry>
		</hbox>
		<hbox space-expand="true" space-fill="true">
			<text xalign="0" space-expand="false"><label>'$(gettext 'Password:')'</label></text>
			<entry space-expand="true" caps-lock-warning="true"  tooltip-markup="'$(gettext 'Enter password')'" visibility="false" sensitive="'${ENABLE_PROXY}'">
				<variable>PASS1</variable>
				'${DEFAULT_PASS1}'
			</entry>
		</hbox>
		<hbox space-expand="true" space-fill="true">
			<text xalign="0" space-expand="false"><label>'$(gettext 'Confirm password:')'</label></text>
			<entry caps-lock-warning="true"  tooltip-markup="'$(gettext 'Enter password')'" visibility="false" sensitive="'${ENABLE_PROXY}'">
				<variable>PASS2</variable>
				'${DEFAULT_PASS2}'
			</entry>
		</hbox>
	</frame>
	<hbox>
		<button>
			'$(/usr/lib/gtkdialog/xml_button-icon ok)'
			<label>'$(gettext 'OK')'</label>
			<action type="exit">OK</action>
		</button>
		<button>
			'$(/usr/lib/gtkdialog/xml_button-icon cancel)'
			<label>'$(gettext 'Cancel')'</label>
			<action type="exit">Cancel</action>
		</button>
	</hbox>
</vbox>

</window>
'

	. /usr/lib/gtkdialog/xml_info gtk
	I=$IFS; IFS=""
	for STATEMENTS in  $(gtkdialog --center --program PROXY_SETUP_WINDOW); do
		eval $STATEMENTS
	done
	IFS=$I

	[ "$EXIT" != "OK" ] && break

	if [ "$ENABLE_PROXY" = "true" ] ; then

		if [ "$PASS1" != "$PASS2" ] ; then
			/usr/lib/gtkdialog/box_ok "$TITLE" error "$(gettext 'Sorry, the password entries are not the same')"
			continue
		fi
		if [ "$HTTP_PROXY" != "" ] ; then
			case $HTTP_PROXY in
				"http:"*) PROTOCOL="http://" ; PROXY=${HTTP_PROXY//http:\/\/} ;;
				"https:"*) PROTOCOL="https://" ; PROXY=${HTTP_PROXY//https:\/\/} ;;
				*) PROTOCOL="http://" ; PROXY=${HTTP_PROXY//http:\/\/} ;;
			esac
			if [ "$USERNAME" != "" ] ; then
				if [ "$PASS1" = "" ] ; then
					PROXY="${USERNAME}@${PROXY}"
				else
					PROXY="${USERNAME}:${PASS1}@${PROXY}"
				fi
			fi
			echo "export http_proxy=${PROTOCOL}${PROXY}" > ${HTTP_PROXY_FILE}
		fi

		if [ "$FTP_PROXY" != "" ] ; then
			case $FTP_PROXY in
				"ftp:"*) PROTOCOL="ftp://" ; PROXY=${FTP_PROXY//ftp:\/\/} ;;
				"ftps:"*) PROTOCOL="ftps://" ; PROXY=${FTP_PROXY//ftps:\/\/} ;;
				*) PROTOCOL="ftp://" ; PROXY=${FTP_PROXY//ftp:\/\/} ;;
			esac
			if [ "$USERNAME" != "" ] ; then
				if [ "$PASS1" = "" ] ; then
					PROXY="${USERNAME}@${PROXY}"
				else
					PROXY="${USERNAME}:${PASS1}@${PROXY}"
				fi
			fi
			echo "export ftp_proxy=${PROTOCOL}${PROXY}" > ${FTP_PROXY_FILE}
		fi

		/usr/lib/gtkdialog/box_ok "$TITLE" info "$(gettext 'Great, you have modified the proxy server settings. Note, you can see the result in files /etc/profile.d/http_profile and /etc/profile.d/ftp_profile. However, MOST IMPORTANT, you must reboot for this to take effect')"
		break

	else
		/usr/lib/gtkdialog/box_ok "$TITLE" info "$(gettext 'You have chosen NOT to use a proxy server. Note, if you previously did have a proxy server enabled, you MUST REBOOT for the change to take effect')"
		rm -f ${HTTP_PROXY_FILE}
		rm -f ${FTP_PROXY_FILE}
		break
	fi

done

### END ###
