#!/bin/bash
# Copyright 2007 Paul D. Smith <paul@mad-scientist.us>
# This script is free software: you may use it, modify it, or distribute it in
# any way you like.
# This script comes WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE.


# Find the AMZ file to control the download.  If we're not given one on the
# command line, then run a file selector dialog to find one.
case $1 in
    -)  file= ;;

    '') file=`zenity --file-selection` || exit 1 ;;

    *)  file="$1"
        if [ ! -f "$file" ]; then
            zenity --error --text "No such file: $file"
            exit 1
        fi
        ;;
esac


# Make a backup copy of the .amz file, since the downloader will delete it...
# but if something goes wrong during the download you may need it again.
case $file in
    '') : do not make a copy ;;
    *)  amzdir="$HOME/.amz-backup"
        [ -d "$amzdir" ] || mkdir -p "$amzdir"
        cp -p "$file" "$amzdir" ;;
esac


# Run the downloader using Wine, in the background.
wine "$HOME/.wine/drive_c/Program Files/Amazon/MP3 Downloader/AmazonMP3Downloader.exe" ${file:+"$file"} &


# Wait for the download to finish
zenity --warning --text="WAIT until the download is complete.  Then click OK to stop the Amazon Download Manager."


# Kill the MP3 Download Manager
kill -9 %1
exit 0
