#!/bin/sh
#
#    eee-bpw /usr/sbin/bpw-test gather system information for diagnosis
#    Copyright (C) 2008  James Cameron (quozl@laptop.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
#

echo "bpw-test: environment check ..."
date
uname -a
dpkg --list eee-bpw
echo ""

echo "bpw-test: checking if Maxon BP3 modem is present on USB ..."
if (lsusb | grep --silent --ignore-case 16d8:6280); then
    echo "bpw-test: Maxon BP3 modem is present"
    echo "bpw-test: checking if option module loaded ..."
    if (lsmod | grep --silent "^option"); then
	echo "bpw-test: option module is loaded"
    else
	echo "bpw-test: option module is NOT LOADED"
    fi
else
    echo "bpw-test: Maxon BP3 modem is NOT PRESENT"
fi
echo ""

echo "bpw-test: checking if Sierra modem is present on USB ..."
if (lsusb | grep --silent --ignore-case 1199:6855); then
    echo "bpw-test: Sierra modem is present"
    echo "bpw-test: checking if sierra module loaded ..."
    if (lsmod | grep --silent "^sierra"); then
	echo "bpw-test: sierra module is loaded"
    else
	echo "bpw-test: sierra module is NOT LOADED"
    fi
else
    echo "bpw-test: Sierra modem is NOT PRESENT"
fi
echo ""

if (test -d /dev/tts/); then
    echo "bpw-test: list of USB serial devices in /dev/tts/"
    ls -l /dev/tts/ | grep USB
    echo ""
fi

echo "bpw-test: checking if symlink by udev rule is present ..."
if (test -L /dev/bpw); then
    echo "bpw-test: bpw symlink is present"
    readlink /dev/bpw || true
else
    echo "bpw-test: bpw symlink is NOT PRESENT, ceasing test"
    exit
fi
echo ""

echo "bpw-test: checking for running pppd ..."
if (pgrep pppd); then
    echo "bpw-test: there is a pppd process running, ceasing test"
    echo "(to test the modem, please use bpwconfig to turn off automatic"
    echo "dialling, remove and reinsert the modem, then try bpw-test again)"
    exit
else
    echo "bpw-test: pppd process is NOT PRESENT"
fi
echo ""

echo "bpw-test: ask modem to tell us about itself ..."
/usr/sbin/chat -V -t20 -f /etc/chatscripts/bpw-test < /dev/bpw > /dev/bpw 2> /tmp/bpw-test-chat.log
egrep --invert-match "^$|^OK$" /tmp/bpw-test-chat.log
