<packagegui>
	<name>per-user-bandwidth-distribution</name>
	<version>1.0.1</version>
	<title>Services: Per User Bandwidth</title>
	<aftersaveredirect>pkg_edit.php?xml=per-user-bandwidth-distribution.xml&amp;id=0</aftersaveredirect>
	<menu>
		<name>PerUserBW</name>
		<tooltiptext>Enforce a fair use bandwidth policy</tooltiptext>
		<section>Services</section>
		<url>pkg_edit.php?xml=per-user-bandwidth-distribution.xml&amp;id=0</url>
	</menu>
	<service>
		<name>PerUserBW</name>
		<rcfile>per_user_bandwidth_distribution.sh</rcfile>
	</service>
	<configpath>installedpackages->package->$packagename->configuration->settings</configpath>
	<fields>
		<field>
			<fielddescr>Incoming Bandwidth</fielddescr>
			<fieldname>incoming_bandwidth</fieldname>
			<description>Enter the bandwidth you would like to distribute fairly among the clients:</description>
			<type>input</type>
		</field>
		<field>
			<fielddescr>Incoming Bandwidth Type</fielddescr>
			<fieldname>incoming_bandwidthtype</fieldname>
			<type>select</type>
			<options>
			    <option>
				<value>Byte/s</value>
				<name>Byte/s</name>
			    </option>
			    <option>
				<value>KByte/s</value>
				<name>KByte/s</name>
			    </option>
			    <option>
				<value>MByte/s</value>
				<name>MByte/s</name>
			    </option>
			</options>			
		</field>
		<field>
			<fielddescr>Outgoing Bandwidth</fielddescr>
			<fieldname>outgoing_bandwidth</fieldname>
			<description>Enter the bandwidth you would like to distribute fairly among the clients:</description>
			<type>input</type>
		</field>
		<field>
			<fielddescr>Outgoing Bandwidth Type</fielddescr>
			<fieldname>outgoing_bandwidthtype</fieldname>
			<type>select</type>
			<options>
			    <option>
				<value>Byte/s</value>
				<name>Byte/s</name>
			    </option>
			    <option>
				<value>KByte/s</value>
				<name>KByte/s</name>
			    </option>
			    <option>
				<value>MByte/s</value>
				<name>MByte/s</name>
			    </option>
			</options>			
		</field>
	</fields>
	<custom_add_php_command>
		$wanif = get_real_wan_interface();
		$fd = fopen("/usr/local/etc/rc.d/per_user_bandwidth_distribution.sh","w");
		fwrite($fd, "# This file was automatically generated by the pfSense\n");
		fwrite($fd, "# package manager system\n\n");
		fwrite($fd, "/sbin/kldload ipfw\n");
		fwrite($fd, "/sbin/kldload dummynet\n");
		fwrite($fd, "/sbin/ipfw delete 10\n");
		fwrite($fd, "/sbin/ipfw pipe 1 config mask src-ip 0x000000ff bw {$_POST['incoming_bandwidth']}{$_POST['incoming_bandwidthtype']}\n");
		fwrite($fd, "/sbin/ipfw pipe 2 config mask dst-ip 0x000000ff bw {$_POST['outgoing_bandwidth']}{$_POST['outgoing_bandwidthtype']}\n");
		fwrite($fd, "/sbin/ipfw add 10 pipe 2 ip from any to any out via {$wanif}\n");
		fwrite($fd, "/sbin/ipfw add 10 pipe 1 ip from any to any in via {$wanif}\n");
		fwrite($fd, "/sbin/ipfw add 10 allow all from any to any\n");
		fclose($fd);
		mwexec("chmod a+rx /usr/local/etc/rc.d/per_user_bandwidth_distribution.sh");
		mwexec_bg("/usr/local/etc/rc.d/per_user_bandwidth_distribution.sh");
	</custom_add_php_command>
	<custom_php_deinstall_command>
		mwexec("/sbin/ipfw delete 10");
		mwexec("/sbin/kldunload dummynet");
		mwexec("/sbin/kldunload ipfw");
	</custom_php_deinstall_command>
</packagegui>