#!/bin/bash
#
# Download and rename new weebl-episode
#
# This script assumes that:
# - You name your episodes $EPISODENUMBER-$EPISODENAME
#   and that $EPISODENUMBER is a three digit number.
# - You have a file "number.txt", which contains the number
#   of the latest episode in your archive (this will be 
#   incremented by the script).
#
# You can start this script either with no arguments, or with
# the name of the episode as an argument.
#
# NB: The name of the episode _must_ contain the .swf suffix.
#     Of course you can change that behaviour by editing the
#     script, but I prefer it this way.
#
# -Rune Hammersland

# The comics reside here:
URL="http://www.weebl.jolt.co.uk/comics/"

# If name of comic wasn't supplied on commandline:
if [ ! $# == 1 ]; then
    # Ask for it
    echo -n "Name of comic: "
    read COMIC
else
    COMIC=$1
fi

# Download it
wget $URL/$COMIC

# If wget succeeds
if [ $? == 0 ]; then
    # Read latest episode-number we have, and increment
    NUMBER=`cat number.txt`
    let NUMBER++

    # If less than 100:
    if [ $NUMBER -lt "100" ]; then
    # Apply a leading 0
        mv $COMIC 0$NUMBER-$COMIC
    else
        mv $COMIC $NUMBER-$COMIC
    fi

    # Update number of episodes
    echo $NUMBER > number.txt
# Else: Error message
else
	echo "Failed to get $COMIC..."
fi
