#!/bin/bash # # telstracabletraffic # http://volker.top.geek.nz/soft/script/telstracabletraffic # # Download and display the TelstraClear cable internet usage. # # Copyright (C) by Volker Kuhlmann, VolkerKuhlmann, gmx.de # http://volker.top.geek.nz/contact.html # All rights reserved. # Released under the terms of the GNU General Public License (GPL) Version 2. # See http://www.gnu.org/ for details. # # Volker Kuhlmann # 25, 26, 27, 28, 29, 30, 31 May; 2, 3, 7, 11, 12, 17, 18, 19 Jun 2006 # 3, 24 Jul; 18, 19 Nov 2006 # 28, 30 Aug; 3 Oct; 19 Nov 2007 # 26 Apr 2008 # chr0(){ echo @;};chr1(){ echo .;} VERSION="1.9, 26 Apr 2008" AUTHOR="Volker Kuhlmann " COPYRIGHT="Copyright (C) 2006-2008" #### #### Constants and initialised variables # # The usage meter location: #url="https://telstraclear.co.nz//usagemeter/" url="https://www.telstraclear.co.nz//usagemeter/" url_page1="${url}index.cfm?s=c" # from 13Nov07: url="https://www.telstraclear.co.nz/tools/usagemeter/" url_page1="${url}index.cfm?s=t" # # Form POST data, with special shell characters escaped so the two variables can # be eval'd: formpost='fquid=\&pass=\&acc=$ACCOUNTNO\&pik=$PASSWORD\&usagelogin=Submit' # Directory which holds the authentication information, and files created # during the download process. authdir="$HOME/.telstraclear" # Files in $authdir. loginauthfile="$authdir/.telstraclearlogin" formdatafile="$authdir/.formdata" cookiefile="$authdir/.cookies" # Downloaded files loginfile="$authdir/login.html" summaryfile="$authdir/summary.html" monthfile="$authdir/month.html" daysfile="$authdir/days.html" # Directory for log files logdir="$authdir/log" # Directory in which CSV files are stored. outdir="." outdir="$authdir" # Pages/files which are younger than this (in seconds) are not fetched again. # (59 minutes cause the files do be re-loaded with an hourly run.) download_min_age=3540 # User agent string. No need to put the head up, let's keep a low profile. useragent='Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko)' #### #### Version, Usage, Help # show_version() { echo "${0##*/} version $VERSION $COPYRIGHT by $AUTHOR" } show_usage() { echo " Usage: ${0##*/} OPTIONS Version $VERSION $COPYRIGHT by $AUTHOR " } show_help() { show_usage echo "\ Download and display your TelstraClear cable internet usage. OPTIONS: -h|--help Show help --help-install Show how to install this program. --version Show version -s|--summary Download and display this month's traffic usage summary. --short As --summary, but in an easily parsable format. -u|--used Show only the number of megabytes used so far this month. -r|--remaining Show only the number of megabytes remaining this month. -l|--line Download and display a summary line. Implies --add. --csv Download current month's usage as CSV file and store in directory ${outdir/$HOME/~}/ -a|--add Add up the total megabytes from this month's CSV file (downloaded if necessary), or of the specified file ('-' for stdin). -p|--pretty As --add, but pretty-print it to stdout. --logline As --line, but appended to a monthly log file. --out DIR Change the storage directory for CSV files to DIR. --minage SEC Pages already downloaded are only downloaded again if they're older than SEC seconds. Default $download_min_age. -0 Same as --minage 0. Force download of requested pages. -9 Sets --minage to infinity, preventing downloads. --sf SFILE Use the given summary file SFILE instead of the current month's for the following operations. Implies -9. -v|--verbose Show some progress info, or more detail (--line, --logline). --authinfo Store authentication details for this program. Run this after changing your password. --dlfile OFILE URL Download URL, saving to OFILE. Only way to get old data. --debug Debug downloads, or debug program if given twice. --login Download the login page. --todo Show the TODO and bug list. Arguments are parsed left to right. " } show_install() { echo " http://volker.top.geek.nz/soft/script/telstracabletraffic Installation: * Download the script, and move it into a directory which is in the path searched for executable programs, e.g. \$HOME/bin/ * Make it executable: chmod 755 \$HOME/bin/telstracabletraffic The program asks for some necessary details the first time it is run. Otherwise, run telstracabletraffic --authinfo Prerequisites (all standard Linux basics): * bash (works with 3.00.16(1)-release, 3.1.17(1)-release, won't work with 1.x) * wget (works with 1.10.1, 1.10.2) * gawk (works with 3.1.4, 3.1.5) * GNU coreutils (works with 5.3.0, 6.4) " show_todo } show_todo() { echo " TODO: * Download and deal with previous month(s). Only the previous 3 are available. * Make some cron framework which can issue warnings when usage goes above a certain number, or increases by X within Y. * Handle additional usage blocks better: display MB remaining in last-purchased block. * Log this script's activities better (debug, ..) BUGS: * none known Bug reports, suggestions, comments appreciated. There is some more information on the internals of the usage meter here: http://www.linux.net.nz/pipermail/nzlug/2006-May/004561.html http://www.linux.net.nz/pipermail/nzlug/2006-May/004582.html http://www.linux.net.nz/pipermail/nzlug/2006-May/004583.html Since 15 June 06, the CSV data is only updated hourly. " } # TODO: # * Download hourly usage. NOT NEEDED - use csv. Version() { show_version; exitwith ErrVersion; } Usage() { show_help; exitwith ErrUsage; } Help() { test "$1" && exitwith ErrHelp show_help; show_help; exitwith ErrOK; } Help_install() { show_version; show_install; } Help_todo() { show_version; show_todo; } #### #### Error/Exit codes # exitwith() { exec 1>&2 # write stdout on stderr instead case "$1" in ErrOK|ErrVersion) exit 0;; ErrUsage|ErrHelp) # Output generated by function (program) $2, if given test -n "$2" && "$2" exit 1;; # more codes in here ErrAuthInfo) echo "Setting $2 is missing in $loginauthfile." echo "See ${0##*/} --help-install" exit 2;; ErrAuthFail) echo "\ Repeated download of the same page returns the login page. Are your account number and password correct? The problem could also be caused by the usage meter being non-functional at the moment. Check at http://telstraclear.co.nz/usagemeter" exit 2;; ErrNoFile) echo "No file: '${2/$HOME/~}'" exit 2;; # more codes in here ErrBadoption) echo "Bad option '$2'." echo "Call with -h for help." exit 9;; ErrMissingParameter) echo "A required parameter for option $2 is missing." echo "Call with -h for help." exit 9;; *) echo "Internal error: exitwith() called with illegal error code '$1'." exit 19;; esac } #### #### Functions # # Print date as YYYY-MM-DD date_ymd() { date "$@" '+%Y-%m-%d' } # Print date as year, month, day, and time DATEFMT_ymdtime="%Y-%m-%d %H:%M" date_ymdtime() { date "$@" "+$DATEFMT_ymdtime" } # Print date as full time stamp, single-word date_stamp() { date "$@" '+%Y%m%d_%H%M%S' # Date and time columns are more difficult to seperate visually with "T" #date "$@" '+%Y%m%dT%H%M%S' } # Print date/time in seconds since epoch date_s() { date "$@" '+%s' } # Ensure file $1 ends with a newline - add one if not. # Return: false if $1 doesn't exist. ensure_trailing_NL() { test -r "$1" && { test "`tail -c1 "$1"`" = "" || echo >>"$1"; } } # Download the given URL, storing to the given file. # In: $1: file name to store the URL into. # $2: URL download_url() { local outfile="$1" tmpfile="$1.$$" shift local -a wget_args debug_args wget_args=( --no-check-certificate --cache=off --load-cookies="$cookiefile" --save-cookies="$cookiefile" -e dirstruct=off -e timestamping=off -U "$useragent" ) debug_args=( --save-headers -S ) test -e "$cookiefile" || touch "$cookiefile" if [ "$debug" -o "$verbose" ]; then echo 1>&2 "GET: $outfile" fi if [ "$debug" ]; then ( set -x wget "${wget_args[@]}" "${debug_args[@]}" -O "$tmpfile" "$@" ) else local verb=--quiet test "$verbose" && verb=--no-verbose wget "${wget_args[@]}" $verb -O "$tmpfile" "$@" fi if [ -s "$tmpfile" ]; then # Remove CRs from file. Needed for the CSV files, but here's a # good place to do it for all files. tr -d '\r' < "$tmpfile" > "$outfile" rm "$tmpfile" else # file is empty or missing test -e "$tmpfile" && rm "$tmpfile" echo 1>&2 "Download failure (no data) with '${outfile/$HOME/~}'." fi } # In: $1: file # $2: age in seconds # Return: true if file is older than the given seconds or is missing. is_older() { local timefile test ! -e "$1" && return test ! -s "$1" && return timefile=`date -r "$1" +%s 2>/dev/null` && { test $(($timefile + $2)) -lt `date +%s` } } # In: $1: file # Return: true if the given file is the login page. is_login() { test -e "$1" || return test "`filter_login "$1"`" == "yes" } # Download file if it doesn't exist, is too old, or the existing file is the # login page. # In: $1: file to store download data in # $2-: arguments 2.. for download_url() download_file() { local file="$1" shift if test $download_min_age -lt $NEVER \ && { is_older "$file" $download_min_age || is_login "$file"; }; then download_url "$file" --post-file="$formdatafile" "$@" ensure_trailing_NL "$file" else if [ "$debug" -o "$verbose" ]; then echo 1>&2 "SKIP: $file" fi fi if is_login "$file"; then exitwith ErrAuthFail fi } # Download the login form. download_login() { # Don't use download_file here, that sends the auth data. download_url "$loginfile" "$@" "$url" } # Download the usage summary page. download_summary() { download_file "$summaryfile" "$@" "$url_page1" # This redirects, so no point setting LAST_URL here. if is_login "$summaryfile"; then download_file "$summaryfile" "$@" "$url_page1" fi } download_month() { download_summary read_summary local ref="$url/index.cfm?g=${USAGE[8]}&s=c&p=usagetodate&display_service=1" local thisurl="$url${USAGE[7]}" download_file "$monthfile" --referer "$ref" "$@" "$thisurl" LAST_URL="$thisurl" } # Return the file name for this month's CSV data in $CSVFILE. # Return: false if current month can't be established. get_csv_month_filename() { CSVFILE="$outdir/usage-month-${USAGE[4]}.csv" test -n "${USAGE[4]}" -a "${USAGE[4]}" != "1970-01-01" } # Download this month's CSV usage data. download_csv_month() { download_month local thisurl="$url`filter_month`" get_csv_month_filename || { echo 1>&2 "Can't establish current month." return 1 } ## Downloading this gif doesn't work - "internal server error" ## Most likely deliberately blocked, but I can't find out how. #( #thisurl="$url`filter_month_bargraph`" #download_file "${CSVFILE/csv/gif}" --referer "$LAST_URL" "$@" "$thisurl" #) download_file "$CSVFILE" --referer "$LAST_URL" "$@" "$thisurl" LAST_URL="$thisurl" } download_days() { download_month local thisurl="$url`filter_month`" download_file "$daysfile" --referer "$LAST_URL" "$@" "$thisurl" LAST_URL="$thisurl" } # Find the CSV file of the current month, or the one given, or stdin # for "-". # In: $1: CSV file to use, may be empty # Out: $csvfile: CSV file to use # Return: false if file download failed current_csv_file() { csvfile="$1" if [ -z "$csvfile" ]; then download_summary read_summary #get_csv_month_filename download_csv_month || { csvfile="/dev/null" return 1 } csvfile="$CSVFILE" fi test "$csvfile" != "-" || csvfile="/dev/stdin" } # Add up the usage MBytes of the given CSV file, or download and add up the # current month's usage if no CSV file given. add_csv() { local csvfile current_csv_file "$1" && filter_add_csv < "$csvfile" } # Pretty-print the given CSV file, or download and pretty-print the # current month's usage if no CSV file given. pretty_csv() { local csvfile current_csv_file "$1" && filter_pretty_csv < "$csvfile" } # Check whether the given page is the login page. # In: $1: File to check # Out: prints "yes" to stdout, nothing otherwise # Note: The login word line was originally within the first 20 lines, but is on # line 124 on 28Aug07 (and may have been there for much longer). filter_login() { test -r "$1" || return 1 gawk < "$1" \ ' BEGIN { IGNORECASE=1 } /META.*Keywords.*login/ { print "yes"; exit } /CONTENT.*Form.*login.*TelstraClear/ { print "yes"; exit } NR > 250 { exit } ' } # Extract this month's total usage, some plan details, and the URL for the next # web page from the top-level usage page. # Note the output of this function may be appended to but must not change, for # parsing stability! filter_summary() { test -r "$summaryfile" || return 1 gawk < "$summaryfile" \ ' BEGIN { USE=-1; USEPC=-1; LEFT=-1; START="1-Jan-1970"; FINISH=START; ADDBLOCKS=0; ADDCOST=0; IGNORECASE=1; q="'\''" } { gsub ("^[ \t]*", "") } { gsub (" * *", " ") } { gsub ("", "") } #{ printf "%s\n", $0 > "/tmp/out-debug" } /have used/ { #print USE=gensub(".*have used ([0-9][0-9.]*) [a-z]*(| which.*)", "\\1", 1) USEPC=gensub(".*which is ([0-9][0-9]*)% of.*", "\\1", 1) LEFT=gensub(".*have ([0-9][0-9]*) [a-z]* left.*", "\\1", 1) if (index(USE, ".") > 0) { USE = sprintf("%d", USE * 1024 + 0.5) } } /of your [a-z]* traffic/ { USEPC=gensub(".* ([0-9][0-9]*)% of your.*traffic allocati.*", "\\1", 1) LEFT=gensub(".*have ([0-9][0-9.]*) [a-z]* left.*", "\\1", 1) if (index(LEFT, ".") > 0) { LEFT = sprintf("%d", LEFT * 1024 + 0.5) } } /have used.*additional/ { # original version of meter LEFT=0 ADDBLOCKS=gensub(".*additional ([0-9][0-9]*) block.*", "\\1", 1) ADDCOST=gensub(".*additional.*block.*cost.*\\$([0-9.]*[0-9]).*", "\\1", 1) } /100%.*of your .*traffic allocation, plus/ { # first redesign LEFT=0 ADDBLOCKS=gensub(".*additional [^0-9]*([0-9][0-9]*)[^0-9]* block.*","\\1",1) if (ADDBLOCKS == $0) { ADDBLOCKS=-1 } # parse error ADDCOST=gensub(".*block.*cost.*\\$([0-9.]*[0-9]).*", "\\1", 1) } /popUpWindow.*usagemeter.index.cfm/ { # The gensub() regex must match the whole line! # up to 23Aug07: URL=gensub(".*popUpWindow.*/(index.cfm.[a-z0-9=&_-]*).,.*current.*", "\\1", 1) if (URL == $0) URL="PARSE_ERROR" #print "===" URL >"/dev/stderr" split(gensub("^.*\\?", "", 1, URL), URLS, "&") } /valign=.bottom.*usagemeter.index.cfm/ { # from 23Aug07: URL=gensub(".*/(index.cfm.[a-z0-9=&_-]*).>.*", "\\1", 1) if (URL == $0) URL="PARSE_ERROR" split(gensub("^.*\\?", "", 1, URL), URLS, "&") } /class=.(usg_hdrTxtBig|metertitle).*>[a-z0-9]* Service [0-9]*<.(td|span)/ { PLAN1=gensub(".*>([a-z0-9 ]*)[a-z0-9]* [a-z0-9]*<.(td|span)/ { PLAN2=gensub(".*>([a-z0-9 ]+)[^>]* ([0-9]*-[a-zA-Z]*-....).*usage cycle.*end.* ([0-9]*-[a-z]*-....)]*).>.*", "\\1", 1) if (URL == $0) URL="PARSE_ERROR" } END { print URL } ' } # Extract the URL for the bargraph of this month's daily usage. filter_month_bargraph() { test -r "$monthfile" || return 1 gawk < "$monthfile" \ ' BEGIN { IGNORECASE=1; URL="PARSE_ERROR"; q="'\''" } { gsub ("^[ \t]*", "") } /src=.https:/ { URL=gensub(".*src=.*(index.cfm[^\"]*).>.*", "\\1", 1) if (URL == $0) URL="PARSE_ERROR" } END { print URL } ' } # Extract the URL for the CSV of the day's hourly usage. filter_day() { gawk < "$daysfile" \ ' BEGIN { IGNORECASE=1; URL="PARSE_ERROR"; q="'\''" } { gsub ("^[ \t]*", "") } /onClick.*popUpWindow/ { URL=gensub(".*popUpWindow.*/(index.cfm[^,]*).,.*", "\\1", 1) if (URL == $0) URL="PARSE_ERROR" } END { print URL } ' } # Pretty-print CSV file filter_pretty_csv_NOTUSED_TOOSLOW() { while read; do case "$REPLY" in \"*) date="${REPLY%%\",*}" line="${REPLY#*,}" echo `date_ymdtime -d "${date#\"}"`", $line";; *) echo "$REPLY";; esac done \ | \ gawk -F, \ ' /^[^0-9"]/ { printf "%-16s,%10s,%10s,%10s,%8s,%8s,%8s\n", \ "Interval", "Up", "Down", "Total", "Up MB", "Dwn MB", "Tot MB" next } { printf "%s,%10d,%10d,%10d,%8.2f,%8.2f,%8.2f\n", $1, $2, $3, $4, $5, $6, $7 } ' } filter_pretty_csv() { gawk -F, \ ' BEGIN { IGNORECASE=1 months["Jan"]=1; months["Feb"]=2; months["Mar"]=3; months["Apr"]=4 months["May"]=5; months["Jun"]=6; months["Jul"]=7; months["Aug"]=8 months["Sep"]=9; months["Oct"]=10; months["Nov"]=11; months["Dec"]=12 } NR == 1 { printf "%-16s,%10s,%10s,%10s,%8s,%8s,%8s\n", \ "Interval", "Up", "Down", "Total", "Up MB", "Dwn MB", "Tot MB" if (/^[^0-9"]/) { next } } /^"?[0-9]/ { #print $0 split($1, d, "[-:\" ]") m=d[3] date=d[4] " " months[m] " " d[2] " " d[5] " " d[6] " " d[7] #print date date=strftime("'"$DATEFMT_ymdtime"'", mktime(date)) printf "%s,%10d,%10d,%10d,%8.2f,%8.2f,%8.2f\n", date, $2, $3, $4, $5, $6, $7 } ' } # Sum up the usage bytes in the CSV file, output as Mbytes. # (This might bomb a gawk on 32 bit numbers.) filter_add_csvB() { gawk -F, \ ' BEGIN { sumb=-1 } /^.[0-9]/ { sumb+=$4 } END { if (sumb > -1) printf "%.0f\n", (sumb + 524289.0)/1024/1024 else print "-1" } ' } # Sum up the usage megabytes in the CSV file. filter_add_csv() { gawk -F, \ ' BEGIN { sum=-1.0 } /^.[0-9]/ { sum+=$7 } END { if (sum > -1.0) { sum+=1.0 } printf "%.0f\n", sum } ' } # Read summary, filter, and store in a variable. # Do nothing if already in variable. read_summary() { test ${#USAGE[@]} -gt 1 && return #USAGE=( `filter_summary` ) exec 3< <( filter_summary ) USAGE=() while read -r; do USAGE=( "${USAGE[@]}" "$REPLY" ) done <&3 USAGE[4]="`date_ymd -d "${USAGE[4]}"`" USAGE[5]="`date_ymd -d "${USAGE[5]}"`" #showargs "${USAGE[@]}" } display_summary() { download_summary test -r "$summaryfile" || exitwith ErrNoFile "$summaryfile" read_summary local periodend periodstart daysleft daysused local secondsend secondsstart upd now=$(date_s) # End day of plan's period periodend="${USAGE[4]}" # First day of plan's period periodstart="`date_ymd -d "$periodend last month"`" periodstart="`date_ymd -d "$periodstart tomorrow"`" # Days left until period ends secondsend=$(date_s -d $periodend) daysleft=0 test $now -lt $secondsend \ && daysleft=$(( ($secondsend - $now) / (24 * 3600) + 1 )) # Days elapsed since period began secondsstart=$(date_s -d $periodstart) daysused=$now test $now -ge $secondsend && daysused=$secondsend daysused=$(( ($daysused - $secondsstart) / (24 * 3600) + 1)) # Date/time of last update of usage figures #upd="`date +'%Y-%m-%d %H:%M'`" upd="`date_ymdtime -r "$summaryfile"`" # MB/day left mbleft=0 test $daysleft -gt 0 && mbleft=$((${USAGE[2]} / $daysleft )) if [ -n "$debug" ]; then echo 1>&2 "\ *periodstart: '$periodstart', daysused: '$daysused' *periodend: '$periodend', daysleft: '$daysleft', \${USAGE[4]}='${USAGE[4]}' *Last update: '$upd' *Now: `date_ymdtime`" fi printf "\ TelstraClear cable internet usage summary at $upd Used this month: %6d Mbyte, %4d %%, %3d days, %5d Mbyte/day Left this month: %6d Mbyte, %4d %%, %3d days, %5d Mbyte/day Monthly volume: %7s This month ends: %s Active since: %s Plan name: %s " ${USAGE[0]} ${USAGE[1]} $daysused $((${USAGE[0]} / $daysused)) \ ${USAGE[2]} $((100 - ${USAGE[1]})) "$daysleft" $mbleft \ "${USAGE[3]##* }" "$periodend" "${USAGE[5]}" "${USAGE[6]}" test ${USAGE[11]} -gt 0 && \ echo "\ Surcharge: ${USAGE[11]} traffic block(s) for \$${USAGE[12]}" } display_summary_short() { download_summary filter_summary } display_usage_only() { display_summary_short | head -1 } display_remaining_only() { display_summary_short | head -3 | tail -1 } display_summary_line() { local endperiod now csvsum download_summary read_summary endperiod="${USAGE[4]}" now="`date_stamp -r "$summaryfile"`" csvsum="`add_csv`" test -n "$verbose" && echo \ "Date/Time Usage as at Month ends Used/summary Used/CSVsum" #"Date/Time Usage as at Month ends Used Used(CSVsum)" printf "%s %s %s %6d Mbyte %6d Mbyte\n" \ "`date_stamp`" "$now" "$endperiod" ${USAGE[0]} "$csvsum" } # Add a summary line of usage to log file log_summary_line() { logfile="$logdir/`date +%Y-%m`.log" mkdir -p "$logdir" test -n "$verbose" && echo "Adding summary line to '$logfile'" display_summary_line >>"$logfile" } # Prepare authentication info in a format suitable for the web server. # Make sure umask is suitable!! create_auth_info() { # No need for auth info if downloads are effectively disabled. test $download_min_age -ge $NEVER && return test -e "$loginauthfile" || missing_auth_info source "$loginauthfile" test "$formdatafile" -nt "$loginauthfile" && return test "$ACCOUNTNO" || exitwith ErrAuthInfo ACCOUNTNO test "$PASSWORD" || exitwith ErrAuthInfo PASSWORD eval formpost="$formpost" echo -n "$formpost" > "$formdatafile" } missing_auth_info() { test -e "$authdir" || echo "Directory '$authdir' is missing." test -e "$loginauthfile" || echo "File '$loginauthfile' is missing." echo " Creating file ($loginauthfile) with your TelstraClear usage meter authentication data. If you don't want to do this, press Ctrl-C now, but this program won't work without. This also overwrites any existing content of that file. " echo "Enter your TelstraClear account number (from the top right of" read -p "an invoice): " ACCOUNTNO echo "Enter your password for the usage meter, NOT your internet account one." read -p "Password: " PASSWORD mkdir -p "${loginauthfile%/*}" echo > "$loginauthfile" "\ # TelstraClear usage meter authentication details. # Shell-style comments are allowed, spaces around the equal sign are not. ACCOUNTNO=$ACCOUNTNO PASSWORD=$PASSWORD" echo " File '$loginauthfile' created. If you ever want to re-create or change it, edit it with a text editor, or run ${0##*/} --authinfo " exit } #### #### Functions borrowed from functions_vk.bash (see there for comments) allowoptargminus= checkargmm() { test "$1" != "--" -a "$2" != "--" \ && { test -n "$allowoptargminus" \ || test \( "${1:0:1}" != "-" -o "$1" == "-" \) \ -a \( "${2:0:1}" != "-" -o "$2" == "-" \); } } checkarg2() { # 1 required test $# -ge 2 && checkargmm "$2" && return exitwith ErrMissingParameter "$1" } checkarg3() { # 2 required test $# -ge 3 && checkargmm "$2" "$3" && return exitwith ErrMissingParameter "$1" } #### #### Main # declare -a USAGE NEVER=2147483648 # 2^31 # Umask to use when creating the password file, or any downloaded files. umask 077 debug= test $# -eq 0 && Usage while [ $# -gt 0 ]; do case "${1#-}" in -debug) test -n "$debug" && set -x; debug=1;; h|help|-help) Help;; -usage) Usage;; -version) Version;; -help-install) Help_install;; -todo) Help_todo;; # Options: v|-verbose) verbose=1;; -minage) download_min_age="$2"; shift;; 0) download_min_age=0;; 9) download_min_age=$NEVER;; -out) checkarg2 "$@"; outdir="$2"; shift;; -sf|-summaryfile) checkarg2 "$@"; summaryfile="$2"; shift # This script re-downloads "too old" files; prevent accidents download_min_age=$NEVER;; # Actions: -authinfo) missing_auth_info;; -login) create_auth_info; download_login;; s|-summary) create_auth_info; display_summary;; -short) create_auth_info; display_summary_short;; u|-used) create_auth_info; display_usage_only;; r|-remaining) create_auth_info; display_remaining_only;; -csv) create_auth_info; download_csv_month;; a|-add) shift; add_csv "$@"; exit;; p|-pretty) shift; pretty_csv "$@"; exit;; l|-line) create_auth_info; display_summary_line;; -logline) log_summary_line;; -dlfile) checkarg3 "$@"; shift create_auth_info; download_file "$@"; exit;; -days) create_auth_info; download_days;; # DEBUG, undocumented: get) shift; download_url "$@";; get1|-log) shift; download_url "$loginfile" "$@" "$url";; get2a) shift; download_url "$summaryfile" "$@" "$url_page1";; get2) shift; download_url "$summaryfile" --post-file="$formdatafile" "$@" "$url_page1";; get3) display_summary_short; read_summary #shift; download_url "$monthfile" --referer "${url_page1%\?*}" "$@" "$url${USAGE[3]}";; #shift; download_url "$monthfile" --referer 'https://telstraclear.co.nz//usagemeter/index.cfm?s=c' "$@" "$url/${USAGE[3]}";; #shift; download_url "$monthfile" --referer 'https://telstraclear.co.nz//usagemeter/index.cfm' "$@" "$url/${USAGE[3]}";; ref="https://telstraclear.co.nz//usagemeter/index.cfm?g=${USAGE[4]}&s=c&p=usagetodate&display_service=1" # https://telstraclear.co.nz//usagemeter/index.cfm?g=6A62FA2E-A81A-6757-144FA182ED95583F&s=c&p=usagetodate&display_service=1 #shift; download_url "$monthfile" --referer "$ref" "$@" "$url/${USAGE[3]}";; #shift; download_url "$monthfile" --referer "$ref" --post-data='' "$@" "$url/${USAGE[3]}";; shift; download_url "$monthfile" --referer "$ref" --post-file="$formdatafile" "$@" "$url/${USAGE[3]}";; rs) read_summary;; fs) v="${2:+eval summaryfile=\"\$2\"}"; $v filter_summary; exit;; fm) v="${2:+eval monthfile=\"\$2\"}"; $v filter_month; exit;; fmb) filter_month_bargraph; exit;; fd) filter_day; exit;; fl) shift; filter_login "${1:-/dev/stdin}";; ff) shift; "$@"; exit $?;; # Error: *) Help err;; esac shift done