#!/bin/bash

# --- Change the values according to your taste ---
AUDIOPATH="$HOME"/audio/
PLAYLISTDIR="$HOME"/audio/playlists
# -------------------------------------------------

AUDIOPLAYER="/usr/bin/mplayer"
PLAYLIST="$PLAYLISTDIR"/$(basename "$0")-all

if [ ! -x $AUDIOPLAYER ]; then
    echo "Install mplayer and try again."
    exit 1
fi

make_playlist () {

	TEMPLIST=$(mktemp -p "$HOME" $(basename "$0").XXXXXXXX --suffix=.tmp)

    if [ -d "$PLAYLISTDIR" ]; then
        touch "$PLAYLIST"
    else
        mkdir -p "$PLAYLISTDIR"
        touch "$PLAYLIST"
    fi

	find $AUDIOPATH -print -type f > "$TEMPLIST"
    grep -i 'mp3$' "$TEMPLIST" > "$PLAYLIST"
	grep -i 'ogg$' "$TEMPLIST" >> "$PLAYLIST"
	grep -i 'm4a$' "$TEMPLIST" >> "$PLAYLIST"
	# Mplayer doesn't support MIDI files but they're audio anyway, kind of.
	# Uncomment if wanted.
	#grep -i 'mid$' "$TEMPLIST" >> "$PLAYLIST"
	grep -i 'wma$' "$TEMPLIST" >> "$PLAYLIST"
	grep -i 'flac$' "$TEMPLIST" >> "$PLAYLIST"
	grep -i 'mp4$' "$TEMPLIST" >> "$PLAYLIST"

	rm "$TEMPLIST"

    echo Total files: $(wc -l "$PLAYLIST")
}

checkplay() {
	if [ ! -f "$PLAYLIST" ]; then
		make_playlist
		$AUDIOPLAYER -shuffle -playlist $PLAYLIST
	else
		$AUDIOPLAYER -shuffle -playlist $PLAYLIST
	fi
}

usage() {
    cat << EOF
Usage: $(basename "$0") [ARG]

  <digit>			Doze for <digit> seconds and start playing
  <digit>m			Doze for <digit> minutes and start playing
  <digit>h			Doze for <digit> hours and start playing
  -c | --create			Create playlist w/o playing it
  -h | --help			Show this help
  -d | --dir <dir>		Play all audio files in <dir>
  -t | --track <path>		Play just one track
  <digit> path-to-track		"Alarm with a certain track." Doze for <digit>
				and play specified track, continuing with
				random play

				w/o ARG playing starts immediately
EOF
}

# Show usage
if [ "$1" == "-h" -o "$1" == "--help" ]; then
    usage
    exit 0
fi

# Just create playlist w/o playing it
if [ "$1" == "-c" -o "$1" == "--create" ]; then
    make_playlist
    exit 0
fi

# Play directory
if [ "$1" == "-d" -o "$1" == "--dir" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        if [[ "$2" != */ ]]; then
            DIR="$2"/
        else
            DIR="$2"
        fi
        $AUDIOPLAYER "$DIR"*
        exit 0
    fi
fi

# Play a track
if [ "$1" == "-t" -o "$1" == "--track" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        $AUDIOPLAYER "$2"
        exit 0
    fi
fi

# Play certain playlist
if [ "$1" == "-p" -o "$1" == "--playlist" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        $AUDIOPLAYER -playlist "$2"
        exit 0
    fi
fi

# Play certain playlist in random order
if [ "$1" == "-ps" -o "$1" == "--playlistshuffle" ]; then
    if [ ! -n "$2" ]; then
        echo "No argument given. Aborting."
        exit 1
    else
        $AUDIOPLAYER -shuffle -playlist "$2"
        exit 0
    fi
fi

# Start playing immediately
if [ "$#" -eq 0 ]; then
    checkplay
    exit 0
fi

# Doze first
if [ "$#" -eq 1 ]; then
    sleep "$1"
    checkplay
    exit 0
fi

# Doze and play "alarm"
if [ "$#" -eq 2 ]; then
    sleep "$1"
    $AUDIOPLAYER "$2"
    checkplay
    exit 0
fi

