#!/bin/bash
# $Id: tt,v 1.14 2004/09/17 13:00:17 jwk Exp $
#fixed to work with 2003 teletekst
#fixed to work with 2005 teletekst (by dracula)


function GET_SOURCE {
    wget --quiet --user-agent='Opera/8.01 (Windows NT 5.1; U; en)' --header='Accept-Language: nl' -O /dev/stdout $1
}

function FORMAT_HTML {
    links -no-numbering -no-references -dump -dump-width 200
#    lynx -dump -nolist -stdin -width 1000
}

if [ ! "$1" ]; then
    echo "Usage single page: tt <pageno>";
    echo "   Multiple pages: tt m/<pageno>"
    echo "   Numbered pages: tt <pageno>/<number>";
    echo 
    echo "To enable colours, invoke as ttc."
    echo 
    echo "Copyright info from http://teletekst.nos.nl/help/help.html:"
    echo "NOS Teletekst is een gedeponeerd handelsmerk van de Nederlandse"
    echo "Omroep Stichting. De pagina's van Teletekst worden beschermd door"
    echo "de Auteurswet. Het is bijgevolg NIET toegestaan om de pagina's op"
    echo "te slaan anders dan voor persoonlijk gebruik, of ze te"
    echo "herdistribueren (bijv. via e-mail of Usenet News)."
	
    exit 1;
fi;

if [ -n "$DARK" ]; then
    HIGHLIGHT=""
else
    HIGHLIGHT=";1"
fi

# Process parameter
if echo "$1" |grep '/' 2>&1 >/dev/null; then

    page=$(echo "$1"|cut -f 1 -d'/')
    if [ "$page" = "m" ]; then
    
        # Find the number of subpages
        page=$(echo "$1"|cut -f 2 -d '/')
        subpages=$(GET_SOURCE http://teletekst.nos.nl/all/$page.html|grep aantalSubs|sed -e 's/.*aantalSubs=\([^;]*\).*/\1/')

        # Invoke self for all subpages
        for number in $(seq $subpages); do
            $0 "$page"/"$number"
	done
        exit 0
    fi

    # User specified page and subpage
    number=$(echo "$1"|cut -f 2 -d '/')
    if [ "${#number}" -lt 2 ]; then
        number=0"$number"
    fi
    echo $page/$number
    
else

   # User specified page only, use subpage 01 as default
    number=01
    page="$1"
    
fi

#lynx -dump -nolist http://teletekst.nos.nl/cgi-bin/tt/nos/page/t/"$1" | 
if [ "$(basename $0)" = "ttc" ]; then

    # Download the page, replace colors, and format the HTML
    GET_SOURCE http://teletekst.nos.nl/tekst/${page}-${number}.html |
    sed -e "s,<font color=000000>,ESC[30${HIGHLIGHT}m,g"\
        -e "s,<font color=blue>,ESC[34${HIGHLIGHT}m,g"\
        -e "s,<font color=lime>,ESC[32${HIGHLIGHT}m,g"\
        -e "s,<font color=aqua>,ESC[36${HIGHLIGHT}m,g"\
        -e "s,<font color=red>,ESC[31${HIGHLIGHT}m,g"\
        -e "s,<font color=yellow>,ESC[33${HIGHLIGHT}m,g"\
        -e "s,<font color=purple>,ESC[35${HIGHLIGHT}m,g"\
        -e "s,<font color=white>,ESC[37${HIGHLIGHT}m,g"\
        -e "s,<FONT [^>]*,,g"\
        -e "s,</FONT>,,g" | 
    FORMAT_HTML |
    sed 's/ESC//g'
        
else
    
    # Download the page, and format the HTML
    GET_SOURCE http://teletekst.nos.nl/tekst/${page}-${number}.html|
    FORMAT_HTML
    
fi|
{ 
    # Find the dash that signals the start of the actual content
    dashfound=0
    eof=0
    while [[ $dashfound == 0 && $eof == 0 ]]; do
        read line
        eof=$?
        [[ ${line:(-2)} = "Ga" ]] && dashfound=1;
        
        #DEBUG:
        #echo Head: $line Dash: $dashfound EOF: $eof
    done;
    
    # Then output 26 lines
    if [[ $dashfound == 1 ]]; then
        for i in $(seq 25); do
            read line
            echo "$line"
        done
    else
        echo "Page $page not found"
    fi
}

# Reset terminal color
if [ "$(basename $0)" = "ttc" ]; then
    echo -e "\033[0m"
    tput sgr0
else
    echo
fi