#!/bin/bash
#
# stop_wireless.sh v1.1 2005/06
#
# script for stoping wireless interface. to be used with start_wireless.sh
#
# very useful when used in combination with sudo to allow non-root
# users to stop wireless network.
#
# Copyright (C) 2005 < pjvenda (at) pjvenda org >
#
# Distributed under the terms of the GNU Public License Agreement (GPLv2)
# http://www.fsf.org/licensing/licenses/gpl.html or
# http://www.fsf.org/licensing/licenses/gpl.txt
#
# Changelog
# =========
#
# v1.1 - 2005/06/13
# - use first wireless device found. don't rely on static values
#
# v1.0 - 2005/03/06
# - first version
#


IWCONFIG=/usr/sbin/iwconfig
IFCONFIG=/sbin/ifconfig
DHCPCD=/sbin/dhcpcd
KILL=/bin/kill
WETH_DEVICE=$(${IWCONFIG} 2>/dev/null| tr -s " " | cut -d ' ' -f 1)

if [ -z "${WETH_DEVICE}" ]; then
	echo "no wireless devices found."
	exit 1;
else
	echo "found wireless ethernet device: ${WETH_DEVICE}"
fi
			
echo -n "stopping dhcp daemon for wireless interface ... "
DHCPCD_PID=$(ps aux | grep "${DHCPCD} ${WETH_DEVICE}" | tr -s " " | cut -d ' ' -f 1)
if [ ! -z "${PID}" ]; then
	echo -n "pid ${PID} ... "
	${KILL} -HUP ${PID}
	echo "done"
else
	echo "not found"
fi

echo -n "stopping wpa_supplicant daemon ... "
/etc/init.d/wpa_supplicant stop 2>&1 > /dev/null
echo "done"
echo -n "resetting wireless interface configurations ... "
${IWCONFIG} ${WETH_DEVICE} essid any 
echo "done"
echo -n "disabling ${WETH_DEVICE} ... "
${IFCONFIG} ${WETH_DEVICE} down
echo "done"
