#!/bin/bash
#
# Quick script to upload my iTunes library to a remote server
# no more often than a specified period of time.  This script
# assumes you have some sort of rsa/dsa/etc. keypair in place
# so that ssh/scp sessions do not require a password.
#
# Copyright (c) 2005 Kevin Dougherty
#

library="/Users/your_user_name/Music/iTunes/iTunes Music Library.xml"
remote_path="public_html/itunes.xml"
remote_user="your_remote_user_name"
remote_host="my.server.com"
last_run_path="/Users/your_user_name/Music/iTunes/.itunes_last_upload"
seconds_between_uploads=3600 # one hour
cur_time=`/bin/date '+%s'`
last_run=$(($cur_time - $seconds_between_uploads))

if [ -e $last_run_path ]; then
        last_run=`/usr/bin/stat -t '%s' "$last_run_path" | awk '{ print $9 }' | cut -c2-11`
fi

seconds_elapsed=$(($cur_time - $last_run))

if [ $seconds_elapsed -ge $seconds_between_uploads ]; then
        /usr/bin/scp -C -q "$library" $remote_user@$remote_host:$remote_path-tmp
        /usr/bin/ssh -q $remote_user@$remote_host "mv $remote_path-tmp $remote_path"
        /usr/bin/touch "$last_run_path"
fi
