#!/bin/sh
#set -ex

# Copyright (c) 2004 Torbjrn Svensson <azoff@se.linux.org>.
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

LOGFILE=`mktemp -p /tmp`
KVERS=`uname -r`

log()
{
    echo -e "$*" 2>&1 >> $LOGFILE
}

log_cmd()
{
    /bin/sh -c "$*" 2>&1 >> $LOGFILE
}

newblock()
{
    log "------------------------------------\n"
}

# utils
log "utils:"
log_cmd 'ls -l /sbin/loadndisdriver'
log_cmd 'ls -l /usr/sbin/ndiswrapper'

# the kernel
newblock
log "kernel:"
log_cmd 'cat /proc/version'
if [ -d /lib/modules/$KVERS/build/include ]; then
    log "kernel sources are in /lib/modules/$KVERS/build"
else
    log "kernel sources missing"
fi

# the verion of gcc
newblock
log "gcc --version:"
log_cmd 'gcc --version'

# try to get kernelconfig
newblock
log "kernel configuration:"
if [ -r /proc/config.gz ]; then
	log_cmd 'gzip -d < /proc/config.gz'
else 
	if [ -r /lib/modules/$KVERS/build/.config ]; then
		log_cmd "cat /lib/modules/$KVERS/build/.config"
	else
		log "kernel config missing"
	fi
fi

# installed drivers
newblock
log "installed drivers:"
log_cmd 'ls -lR /etc/ndiswrapper'

# module information
newblock
log "module information:"
log_cmd 'modprobe -c | grep ndis'
log "module is at:"
log_cmd "find /lib/modules/$KVERS/ -name ndiswrapper\* | xargs ls -l"
log ""

while(true); do
    echo "Is it okay to shutdown 'wlan0' interface and reload the module?"
    echo "If loading the module crashes kernel, then say N here"
    echo -n "Reload ndiswrapper module? [N/y]:"
    read res

    # default to N, (nothing entered)
    if [ "$res" == "" ]; then 
	res="N" 
    fi

    case $res in
	y|Y)
	    log "Reloading ndiswrapper..."
	    log_cmd 'ifconfig wlan0 down;  rmmod ndiswrapper;
			modprobe ndiswrapper; dmesg|tail -n 100'
	    break
	    ;;
	n|N)
	    log "Not reloading ndiswrapper..."
	    log ""
	    log_cmd 'dmesg|tail -n 100'
	    break
	    ;;
	*)
	    echo "No input.. retry."
	    ;;
    esac
done

gzip -c $LOGFILE > /tmp/ndiswrapper-buginfo.gz

echo "please attach /tmp/ndiswrapper-buginfo.gz to your bugreport!"
\rm -f $LOGFILE
