#!/bin/sh
#
# $Id: pcpages,v 1.3 2005/04/26 22:40:03 decibel Exp $
#
# Update static pages. Accepts a list of projects to update

export PATH=$PATH:/usr/sbin

cd /htdocs

do_wget ( ) {
    if wget -O${2}.new "http://localhost/${1}"; then
        if grep 'DB Query Failed' ${2}.new; then
            echo "Database error in ${1}"
            exit 3
        fi
        chmod 644 ${2}.new
        chown nugget:www ${2}.new
        mv ${2} ${2}.old
        mv ${2}.new ${2}
        return
    else
        echo "Error fetching ${1}"
        exit 2
    fi
}

get_project ( ) {
    do_wget pc_index.php?project_id=$1 cache/index_$1.inc
    do_wget misc/pc_countries.php?project_id=$1\&source=o cache/countries_o_$1.inc
    do_wget misc/pc_countries.php?project_id=$1\&source=y cache/countries_y_$1.inc
    if [ $1 != 3 -a $1 != 5 -a $1 != 205 ]; then
        do_wget misc/pc_money.php?project_id=$1\&source=y cache/money_$1.inc
    fi
}

if [ x$1 = x ]; then
    echo "Error! No projects specified"
    exit 1
else
    args=$@
fi

echo "Updating static pages for projects $args"
echo

for project in $args; do
    get_project $project
done
