#!/bin/sh
#
# Make sure we're in the right directory:
#
cd /cygdrive/c/iTunes/amipweb
#
# Make sure the path is correct:
#
PATH=/usr/bin:/home/local/bin
export PATH
#
# Set permissions for new files:
#
chmod 644 songrow.html
#
# Prepend the latest song to the list:
#
if [ -f songrow.html ]; then
    #
    # Convert ampersands to HTML character tags, and get rid of track numbers:
    #
    songrow=`sed -e 's/&/&amp;/g' -e 's/<td>[0-9-]* /<td>/' < songrow.html`
    #
    # We get just the track name and artist from the current song, and use
    # that to grep out older versions from the song list:
    # 
    songonly=`echo "$songrow" | cut -d'<' -f1-5`
    grep -v "$songonly" songlist.html > songlist.html.old
    #
    # Massage the rating:
    #
    rating=`echo "$songrow" | cut -d'>' -f15 | cut -d'<' -f1`
    nstars=`expr $rating / 20`
    echo $nstars
    case $nstars in
    1) stars="&bull;" ;;
    2) stars="&bull;&bull;" ;;
    3) stars="&bull;&bull;&bull;" ;;
    4) stars="&bull;&bull;&bull;&bull;" ;;
    5) stars="&bull;&bull;&bull;&bull;&bull;" ;;
    *) stars="-" ;;
    esac
    line1=`echo "$songrow" | cut -d'>' -f1-14`
    line2=`echo "$songrow" | cut -d'<' -f16-`
    echo $songrow
    songrow="$line1>$stars<$line2"
    echo $songrow
    #
    # Create a new song list:
    #
    echo $songrow > songlist.html
    #
    # Grab at most 99 songs from the previous song list:
    #
    head -99 songlist.html.old >> songlist.html
    rm songlist.html.old
fi
#
# Upload it to the server:
#
scp songlist.html somesystem:somedirectory/songlist.html
