#!/bin/bash
# wikiimg --- Get and put SVG and PNG files to Oddmuse wikis
# 
# Copyright (C)  2006  Alex Schroeder <alex@emacswiki.org>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if [ -z "$2" ]; then
    echo Usage:
    echo
    echo image get PageName -- gets PageName from the wiki and saves
    echo it as PageName.svg in the current directory.
    echo
    echo image put PageName -- saves PageName.svg as PageNameSource
    echo and PageName.png as PageNameImage. You are responsible for
    echo exporting the SVG file as PNG when you are done.
    exit
fi

WIKI=http://www.communitywiki.org/en
USER=`id -un`
WIKIUSER=${WIKIUSER:-USER}

case $1 in
    get)
	F=`basename "$2" Image`.svg
	# make sure both are accepted
	IMG=`basename "$2" Image`Image
	wget -O $F $WIKI/$IMG
	;;
    put)
	# make sure all are accepted
	PAGE=$2
	PAGE=`basename $PAGE .svg`
	PAGE=`basename $PAGE .png`
	for f in "$PAGE.svg" "$PAGE.png"; do 
	    if [ ! -f "$f" ]; then
		echo There is no file called $f
		exit
	    fi
	done
	wikiupload -u "$WIKIUSER" "$PAGE.png" "$WIKI/${PAGE}Image"
	wikiupload -t "image/svg+xml" -u "$WIKIUSER" \
	    "$PAGE.svg" "$WIKI/${PAGE}Source"
	;;
    *)
	echo You must use either get or put as first parameter.
	exit
esac
