#!/bin/bash


# read in the config file
test -r ~/.bartrc && source ~/.bartrc
# set some defaults
BARTSTART=${BARTSTART:-ROCKR}
BARTDEST=${BARTDEST:-EMBAR}
FORMAT=${FORMAT:-new}
URL_ONLY=0
date_now=$(date +"&time_mode=departs&depart_month=%m&depart_date=%d&depart_time=%I:%M+%p")
date_return=""
return_delay=0

function pommy {
    test -x /usr/games/pom && pom | grep -qi 'is full' && echo 'Full moon tonight. Watch out!'
    test -x /usr/games/pom && pom | grep -qi 'is new' && echo 'Be careful--new moon tonight.'
}


function usage {
    echo -e "Usage:\n $(basename $0) [-hondrul] <destination>\n $(basename $0) [-hondrul] <source> <destination>\n  To change defaults, set the BARTSTART and BARTDEST variables in your ~/.bartrc\n"
    echo "Flags:"
    echo -e "  -o, --old\n\tUse old format (especially if color format isn't working).\n\tAlso FORMAT=old in ~/.bartrc"
    echo -e "  -n, --new\n\tForce use of new format. Also FORMAT=new in ~/.bartrc"
    echo -e "  -d <hours>, --delay <hours>\n\tUse date <hours> hours in the future for departure time."
    echo -e "  -r <hours>, --return-delay <hours>\n\tUse date <hours> hours in the future for return trip time."
    echo -e "  -u, --url\n\tJust print a URL to a schedule suitable for printing."
    echo -e "  -l, --list\n\tList all available station codes and names."
    echo -e "  -h, --help\n\tDisplay this text and exit."

}

function liststations {
    wget -O - -o /dev/null 'http://bart.gov/index.asp?ct=1' | html2 2> /dev/null | sed '/\/html\/body\/table\/tbody\/tr\/td\/table\/tr\/td\/form\/select\/@name=origin/,/\/html\/body\/table\/tbody\/tr\/td\/table\/tr\/td\/form\/br/p;d;'  | cut -d = -f 2 | grep -v ^/ | tail -n +6 | while read i; do read j; echo -e "$i\t$j"; read blank; done
}

NORMALCOLOR='\e[0m'
NOTECOLOR='\e[0m' 

TRAINCOLOR='\e[1;31m'
TIMECOLOR='\e[1;33m'
FARECOLOR='\e[1;32m'
BOARDCOLOR='\e[1;36m'
STATIONCOLOR='\e[0;36m'

function colorformat {
    while read i
    do
	case $i in
	    \$*)	echo -e "Current time:\t${TIMECOLOR}$(date +'%l:%M%p')${NORMALCOLOR} (note that first train listed may be in the past)\n${FARECOLOR}$i";
	    		board="Board:";;
	    *train)	train=$i;
	    		departure=$arrival;
			beginning=$destination;;
	    at\ *)	arrival=${i#at };;
	    Timed|Transfer)	read junk;
	    		board="Xfer:";; 
	    *\ Allowed)	echo -e "${BOARDCOLOR}${board}\t${TIMECOLOR}${departure} ${STATIONCOLOR}${beginning} ${NORMALCOLOR}to ${STATIONCOLOR}${destination} ${TIMECOLOR}${arrival} ${TRAINCOLOR}[${train}] ${NOTECOLOR}(${i% Allowed})";
	    		board="\nBoard:";;
	    *)		destination=$i;;
	esac
    done
}




function nexttrains {
    URL="http://bart.gov/textonly/stations/quickplanner/schedule.asp?ct=1&format=quick&origin=${BARTSTART}&destination=${BARTDEST}&print=yes${date_now}${date_return}"

    if [ "$DUMPBART" = "yes" ]; then wget -O - -o /dev/null ${URL} | html2 2> /dev/null; fi
    if [ $FORMAT = "new" ] 
    then
	wget -O - -o /dev/null ${URL} | html2 2> /dev/null | grep -e '\(/html/body/div/div= \$\|/html/body/div/table/tr/td=\)' | cut -d = -f 2 | colorformat
    else
    	wget -O - -o /dev/null ${URL} | html2text -style compact -nobs | sed '/^Your Schedule/,/^<</p;d' | grep -v "^<<"
    fi	
}

while [ $# -gt 0 ]
do
    case "$1" in
	-h|--help)	usage; exit 0;;
	-l|--list) liststations; exit 0;;
	-n|--new)	FORMAT=new; shift;;
	-o|--old)	FORMAT=old; shift;;
	-d|--delay)	date_now=$(date +"&time_mode=departs&depart_month=%m&depart_date=%d&depart_time=%I:%M+%p" -d "now + $2 hours");delay=$2;shift;shift;;
	-r|--return-delay)	return_delay=$[ $delay + $2 ];date_return=$(date +"&trip_mode=roundtrip&return_month=%m&return_date=%d&return_time=%I:%M+%p" -d "now + ${return_delay} hours");shift;shift;;
	-u|--url)	URL_ONLY=1;shift;;
	--)	shift; break;;
	-*|--*)	usage; exit 1;;
	*)	break;;
    esac
done

pommy

if [ $# = 1 ]
then
    BARTDEST=$1
elif [ $# = 2 ]
then
    BARTDEST=$2
    BARTSTART=$1
fi

if test -r /usr/local/etc/bart.stations
then
	BARTDEST=$(grep -hi ${BARTDEST} /usr/local/etc/bart.stations /usr/local/etc/bart.stations.fallback | head -1 | cut -f 1)
	BARTSTART=$(grep -hi ${BARTSTART} /usr/local/etc/bart.stations /usr/local/etc/bart.stations.fallback | head -1 | cut -f 1)
fi

if [ $URL_ONLY = 1 ]
then
    echo "http://bart.gov/stations/quickplanner/schedule.asp?ct=1&origin=${BARTSTART}&destination=${BARTDEST}&print=yes${date_now}${date_return}"
    exit 0
fi

nexttrains

