#!/bin/sh

# Copyright 2013 Israel Martín Escalona <imartin@entel.upc.edu>
# 
# This file is part of Wipos.
# 
# Wipos is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Wipos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Wipos.  If not, see <http://www.gnu.org/licenses/>.

module="pos80211"
#EXTERNAL_REDEFINITION_OF_MODULE_NAMES
device="pos80211"
mode="664"

# Unloading the module
/sbin/rmmod $module &> /dev/null

# Loading the module
/sbin/modprobe $module $* &> /dev/null
if [ $? -ne 0 ]; then echo "The module cannot be loaded!" && exit 1; fi

# Removing the stale nodes
rm -f /dev/${device}_0 || echo "Cannot remove the previously created device ${device}_0"
major=`awk "{if (\\$2 == \"$module\") print \\$1}" /proc/devices`
#major=`grep $device /proc/devices || echo -1` 
if [ "$major" -eq "-1" ]; then echo "No device found!" && exit 1; fi
mknod /dev/${device}_0 c $major 0

# Giving appropriate group/permissions and changing the group.
group="wheel"
#grep -q '^staff:' /etc/group || group="wheel"
chgrp $group /dev/${device}_0
chmod $mode /dev/${device}_0
