#! /bin/bash

NAME="charm-quark"
VERSION="1.0"

FIFO="$HOME/.quark/fifo"
PORT=25555

usage() {
    cat <<EOF
$NAME version $VERSION  Copyright (c) 2003 Ben Jansens

Usage: $0 options [--] command

Options:
  -f, --fifo=[fifopath]    Specify the path to the Quark FIFO
  -l, --listen=[hostname]  Listen mode. Default is all interfaces.
  -r, --remote=hostname    Connect to remote charm-quark.
  -p [port], --port=[port] Port on which to listen. Default is 25555.
  
Commands:
  play                     Start playback
  pause                    Pause/Resume playback
  stop                     Stop playback
  next                     Skip to next track
  prev                     Skip to previous track
  track [number]           Skip to track #

  clear                    Empty the playlist
  replace [files...]       Replace the playlist with the specified files
  append [files...]        Add a file to the playlist
  remove [number]          Remove track # from the playlist
  move [number] [to]       Move track # to a new position in the playlist

  dump                     Dump the current playlist to stdout

  loop                     Toggle looping at the end of the playlist
  random                   Toggle random play order

  connect                  Read status info

  quit                     Kill quark
EOF
exit
}

GETOPTTEST=`getopt --version`
case $GETOPTTEST in
    getopt*) # Must be GNU
        TEMP=`getopt -a -n "$0" -l remote: -l help -l fifo: -l listen:: -l port: -- hf:l::p:r: "$@"`
        ;;
    *) #POSIX?
        TEMP=`getopt hf:l::p: "$@"`
        ;;
esac

if [ $? != 0 ] ; then echo "Usage" ; exit 1; fi

eval set -- "$TEMP"

while test "X$1" != "X--" ; do
    case "$1" in
        -h|--help)
            usage
            ;;
        -f|--fifo)
            FIFO="$2"
            shift 2
            ;;
        -r|--remote)
            REMOTE=1
            REMOTEHOST="$2"
            shift 2
            ;;
        -l|--listen)
            LISTEN=1
            LISTENHOST="$2"
            shift 2
            ;;
        -p|--port)
            PORT="$2"
            shift 2
            ;;
        --) shift; break ;;
        *) break ;;
    esac
done

shift

if [ ! -z $LISTEN ] ; then
    NETCAT="$(which netcat 2>/dev/null)"
    if [ $? != 0 ] ; then
        NETCAT="$(which nc 2>/dev/null)"
        if [ $? != 0 ] ; then
            echo "Can't find netcat or nc" >&2
            exit 1;
        fi
    fi

    if [ ! -z "$1" ] ; then
        echo "Warning. No commands used in listen mode." >&2;
    fi
    
    exec $NETCAT -l -p $PORT -u $LISTENHOST > $FIFO
fi

COMMAND=""

append() {
    while test $# -ge 1; do
        FILE="$1"
	# make a full path (but dont fuck with URIs)
        test "${FILE#/}" = "$FILE" && \
            test "${FILE#*://}" = "$FILE" && FILE="$PWD/$FILE"
        COMMAND="${COMMAND}append $FILE
"
        shift 1
    done
}

case $1 in
    play|pause|stop|next|prev|clear|dump|loop|random|quit)
        COMMAND="${COMMAND}$1
"
	;;
    replace)
	test $# -lt 2 && usage
        COMMAND="${COMMAND}clear
"
        shift 1
	append "$@"
        COMMAND="${COMMAND}play
"
	;;
    append)
	test $# -lt 2 && usage
	shift 1
	append "$@"
        COMMAND="${COMMAND}play
"
	;;
    track|remove)
	test $# -lt 2 && usage
        COMMAND="${COMMAND}$1 $2
"
	;;
    move)
	test $# -lt 3 && usage
        COMMAND="${COMMAND}$1 $2 $3
"
	;;
    connect)
        #yeah this is unsafe, so don't run as root!
        if [ `id -u` -eq 0 ]; then echo "Don't run as root!"; exit 1; fi
        TEMPNAM=`mktemp -u $HOME/.quark/quark-client.XXXXXXXX` || \
            TEMPNAM="$HOME/.quark/quark-client.$$"
        mkfifo "$TEMPNAM"
        (sleep 1; echo connect "$TEMPNAM" > "$FIFO") &
        cat "$TEMPNAM"
        rm "$TEMPNAM"
	exit # nothing going to the fifo
        ;;
    *)
	usage
	;;
esac

if [ ! -z $REMOTE ] ; then
    NETCAT="$(which nc 2>/dev/null)"
    if [ $? != 0 ] ; then
        NETCAT="$(which netcat 2>/dev/null)"
        if [ $? != 0 ] ; then
            echo "Can't find netcat or nc" >&2
            exit 1;
        fi
    fi

    #echo $COMMAND | $NETCAT -u -q 0 $REMOTEHOST $PORT
    echo $COMMAND | $NETCAT -u -w 1 $REMOTEHOST $PORT
else
    if [ ! -p $FIFO ] ; then echo "$FIFO not a FIFO" >&2; exit 1; fi
    echo $COMMAND > $FIFO
fi
