#!/bin/sh -
PATH=$PATH:/l/palm/bin

# by week, updated Fri afternoons:		 	30 15 * * 3,5,6
#urls='http://www.aaflix.com/'

# by today thru Thu, ordering isn't significant:	 30 15 * * *
#urls="http://www.mlive.com/movies/index.ssf/cgi-bin/search.cgi/mlive"
##urls="$urls/search_theater.ata?HOUSE_ID=6349&HOUSE_ID=3109&HOUSE_ID=3279"
#urls="$urls/search_theater.ata?HOUSE_ID=6349&HOUSE_ID=3279"	# no Briarwood
#urls="$urls&HOUSE_ID=3136&HOUSE_ID=2860&HOUSE_ID=3164"
# BROKEN, now requires login cookie, perhaps new HTML parsing
#urls="http://www.mlive.com/movies/index.ssf?finder=mlive&dotheater=yes"
#urls="$urls&theaterArray=AALSL&theaterArray=AAISG&theaterArray=AAJHZ"
#urls="$urls&theaterArray=AAFRN&theaterArray=AACUH"	# no Briarwood

# by day for next 3 days, ordering is significant:	30 15 * * *
##url='http://www.movietickets.com/house_print.asp?ShowDate=1&house_id'
#url='http://www.movietickets.com/house_detail.asp?ShowDate=1&house_id'
#urls="$url=3279 $url=3136 $url=6349 $url=3164 $url=3109"
##     Michigan  State     Quality16 Showcase  Village7
#url='http://www.movietickets.com/house_detail.asp?ShowDate=2&house_id'
#urls="$urls $url=3279 $url=3136 $url=6349 $url=3164 $url=3109"
#url='http://www.movietickets.com/house_detail.asp?ShowDate=3&house_id'
#urls="$urls $url=3279 $url=3136 $url=6349 $url=3164 $url=3109"
# KINZLER: They changed the page format again.  This script needs major
#	   reworking again to clean up the text.  I give up.  Switching to
#	   AvantGo. ... But AvantGo really sucks.

# by today thru Thu:	 				30 15 * * *
urls='http://www.mlive.com/movies/index.ssf?finder=mlive&region=WASHTENAW'

title='Movies Showing'
dest="${PALMINST-$HOME/l/palm/install}/movies.pdb"

# clipmovies - Palm web clipper for local movie theater showings
# Steve Kinzler, steve@kinzler.com, Jan 99/Jan 01/Apr 02/Apr 05/Mar 08
# https://kinzler.com/me/home.html#palm

txt=
case "$1" in
-t)	txt=t; shift;;
esac

case "$#" in
0)	;;
*)	echo "usage: $0 [ -t ]" 1>&2; exit 1;;
esac

tmpA=/tmp/cmA$$
tmpB=/tmp/cmB$$
trap "rm -f $tmpA $tmpB; exit" 0 1 2 13 15

for url in $urls
do
	lynx -source "$url" |
	case "$url" in
	*movietickets*_print*)
		sed -n '/"hsp6"/,$p' |
		sed '/<strong>/s/<\/div>$//; s/<div class="hsp13">/<br>/' |
		html2pdbtxt - | sed 1d |
		sed '/MovieTickets/d; /Personal Box Office/d;
		     /No Passes/d; /^Passes$/d; s/ //g;
		     /^>>>/d; /^Ctrl-P/d; /^Cmd-P/d; s/^Showing On *//' | uniq
		echo ================;;
	*movietickets*_detail*)
		html2pdbtxt - | uniq
		echo ================;;
	*)	sed 's/<img [^>]*\/images\/0\([0-9]\.[0-9]\)\.gif">/\1\/4*/';;
	esac
done > $tmpA
grep '^lynx: ' $tmpA && exit $?

# mlive can require a fix to html2pdbtxt to avoid segfault w/ perl 5.6.0:
# 114s/^/#/; 114a	s!(^|[\n\r])[ \t]+([\n\r]|$)!$1$2!g;
case "$urls" in
*aaflix*)
	html2pdbtxt -t"$title" $tmpA |
	sed '/^ *$/d; s/^\[.*//; s/—/-/g; s/^(\*)/\
==== /; / \[[GPRXN]/s/^/ /
	     /^Review$/d; /^Dinner and a Movie/d; s/^S un-/Sun-/' |
	uniq | tr \\177 \\225 > $tmpB;;
#*mlive*)
#	html2pdbtxt -t"$title" $tmpA |
#	sed 's/<!--//g; s/-->//g; s/  */ /g
#	     s/ *$//; s/ *,$//; s/^—.*/¯¯¯¯¯¯¯¯¯¯/; /^[[ »]/d; s/ //g
#	     /^ *$/d; /^SPEAK UP!$/d; /^OUR AFFILIATES$/d; /^Search Results$/d
#	     /^(Click on /d; /^Search Again$/,$d; /document\.write(/d
#	     /(Map)$/i\
#
#	     /(Map)$/s/^/¶ /; s/ *(Map)$//
#	     /^•/s/\.,/,/; /^•/s/AM/am/g; /^•/s/PM/pm/g
#	     s/^[^•¶].*,/–&/; s/^• *//; s/^–/•/
#	     /^•/s/ *,/,/g; /^•/s/\.0\(\/.\*\)/\1/' > $tmpB;;
*mlive*)
	html2pdbtxt -t"$title" $tmpA |
	sed 's/ / /g; s/  */ /g; /^ $/d; s/^ //; s/ $//
	     3,/^WASHTENAW$/d; /^Search Again:/,$d
	     s/^—.*/¯¯¯¯¯¯¯¯¯¯/; s/ (Map)$//; /^Showtimes:$/d
	     s/^\([A-Z][a-z][a-z]\),\( [A-Z][a-z][a-z] \)/\1\2/
	     s/\(:[0-9][0-9]\),\( [0-9]\)/\1\2/g' > $tmpB;;
*movietickets*)
	sed '$s/=/\//g' < $tmpA > $tmpB;;
*)	cat < $tmpA > $tmpB;;
esac

case "$txt" in
?*)	cat $tmpB;;
*)	txt2pdbdoc "$title" $tmpB "$dest";;
esac
