#!/bin/bash

for year in `date "+%Y"` `date "+%Y" -d "1 year ago"` 
do 
    for month in `seq -f "%02g" 1 12` 
    do 
        wget http://stats.wikimedia.org/archive/squid_reports/$year-$month/SquidReportOperatingSystems.htm
        mv SquidReportOperatingSystems.htm SquidReportOperatingSystems_${year}-${month}.htm
    done 
done

LIST_SYSTEMS="Windows Mac iPhone Linux iPad BlackBerry SymbianOS DoCoMo SunOS FreeBSD OpenBSD"
echo "Mois $LIST_SYSTEMS"
for file in *.htm 
do
    echo -n "`echo $file | cut -d . -f 1 | cut -d _ -f 2` "
    for system in $LIST_SYSTEMS
    do
        case $system in
            Windows|Mac)
                result=`grep -H $system $file | head -n 2 | tail -n 1 | sed 's/%.*//' | sed 's/.*:.*>/ /' | cut -d _ -f 2 | sed 's/.htm//' | sed 's/\./,/' | sed 's/^ *//'`
                ;;
            *)
                result=`grep -H $system $file | head -n 1 | sed 's/%.*//' | sed 's/.*:.*>/ /' | cut -d _ -f 2 | sed 's/.htm//' | sed 's/\./,/' | sed 's/^ *//'`
                ;;
        esac
        if [ -z $result ]
        then
            echo -n "0 "
        else
            echo -n "$result "
        fi
    done
    echo ""
done
