#!/bin/sh

downloadsite="http://www.planscalendar.com/release"

if [ "$1" == "" ]; then
       echo "You should provide the version number to the script."
       echo "Appearently, available versions are :"

       wget -q -O - $downloadsite/ \
               | awk '\
                       BEGIN { \
                               RS="HREF=\"plans_"; \
                               FS=".zip\""; \
                               text=""; \
                               sep=" - ";\
                       } \
                       /.zip/{ \
                               text = text sep $1;\
                       } \
                       END{ \
                               print text;\
                       }' \
               | awk '\
                       BEGIN { \
                               RS=" - "; \
                               FS="."; \
                               text=""; \
                               sep=" - ";\
                               vermajor=0; \
                               vermin=0; \
                               verbuild=0; \
                               verother=0; \
                               version=""; \
                       } \
                       { \
                               isMax=0; \
                               text = text sep $0; \
                               if ( $1 > vermajor ) { \
                                       isMax = 1; \
                               } else if ( $1 == vermajor ) { \
                                       if ( $2 > vermin ) { \
                                               isMax = 1; \
                                       } else if ( $2 == vermin ) { \
                                               if ( $3 > verbuild ) { \
                                                       isMax = 1; \
                                               } else if ( $3 == verbuild ) { \
                                                       if ( $4 > verother ) { \
                                                               isMax = 1; \
                                                       } \
                                               } \
                                       } \
                               } \
                               if ( isMax == 1 ) { \
                                       version = $0; \
                                       vermajor = $1; \
                                       vermin = $2; \
                                       verbuild = $3; \
                                       verother = $4; \
                               } \
                       } \
                       END{ \
                               print text; \
                               print "Latest version is : " version; \
                       }' \

       exit
fi

# The version of Plans to distribute
rev=$1

echo Fetching plans_$rev.zip...
wget $downloadsite/plans_$rev.zip 2>/dev/null >/dev/null

echo Installing Plans to: `pwd`
unzip plans_$rev.zip
mv ./plans_$rev/* ./
rm plans_$rev.zip
rm -rf ./plans_$rev

echo Install complete!
echo Point your browser at plans.cgi

