#!/bin/bash

# This executable does little by itself. Setup links with:
# ln -s manyplayer next
# ln -s manyplayer same

if [ "x${MANYPLAYER_BASE_DIR}" == "x" ] ; then
  MANYPLAYER_BASE_DIR=~/.manyplayer
fi

THIS_DIR=${PWD}
THIS_DIR=${THIS_DIR//\//_}
THIS_DIR=${THIS_DIR// /_}
MANYPLAYER_DIR=${MANYPLAYER_BASE_DIR}/${THIS_DIR}

exe=$(basename $0)

function check {
  if [ ! -d ${MANYPLAYER_DIR} ] ; then
    echo "Manyplayer has not been set up for this directory (${MANYPLAYER_DIR})."
    exit 1
  fi
  if [ ! -f ${MANYPLAYER_DIR}/cmd -a ! -f ${MANYPLAYER_BASE_DIR}/cmd ] ; then
    echo "No command in manyplayer config (${MANYPLAYER_DIR}/cmd)."
    exit 1
  fi
  if [ ! -f ${MANYPLAYER_DIR}/last ] ; then
    echo "Position unspecified (${MANYPLAYER_DIR}/cmd)."
    exit 1
  fi
  if [ ! -f ${MANYPLAYER_DIR}/filename ] ; then
    echo "Filename template unspecified (${MANYPLAYER_DIR}/cmd)."
    exit 1
  fi
}

function play_last_file {
  if [ -f ${MANYPLAYER_DIR}/cmd ] ; then
    local comm=$(<${MANYPLAYER_DIR}/cmd)
  else
    echo "INFO === Manyplayer using default command"
    local comm=$(<${MANYPLAYER_BASE_DIR}/cmd)
  fi
  local this=$(<${MANYPLAYER_DIR}/last)
  local filename=$(<${MANYPLAYER_DIR}/filename)
  exec $comm "${filename/!/$this}"
}

function play_next_file {
  local thelast=$(<${MANYPLAYER_DIR}/last)
  local thenext=$(printf %02d $((10#$thelast + 1)))
  echo -n $thenext > ${MANYPLAYER_DIR}/last
  play_last_file
}

function tell_last_file {
  local this=$(<${MANYPLAYER_DIR}/last)
  local filename=$(<${MANYPLAYER_DIR}/filename)
  echo "Last: ${filename/!/$this}"
}

function setup {
  [ -d ${MANYPLAYER_DIR} ] || mkdir ${MANYPLAYER_DIR}
  
  local guess=$(echo *$1*)
  local name=${guess/$1/!}
  echo -n $name > ${MANYPLAYER_DIR}/filename

  local thelast=$(printf %02d $((10#$1 - 1)))
  echo -n $thelast > ${MANYPLAYER_DIR}/last
}

function teardown {
  if [ ! -d ${MANYPLAYER_DIR} ] ; then
    echo "Manyplayer has not been set up for this directory (${MANYPLAYER_DIR})."
    exit 1
  fi
  rm -rf ${MANYPLAYER_DIR}
}

function set_cmd {
  echo -n $1 > ${MANYPLAYER_DIR}/cmd
}

function set_position {
  echo -n $1 > ${MANYPLAYER_DIR}/last
}

function usage {
cat <<EOT
Manyplayer version 0.5
Usage:
  next                      Watch next episode
  same                      Watch the last episode again
  tell                      Print the last filename
  manyplayer setup <guess>  Setup manyplayer for this directory
  manyplayer teardown       Teardown manyplayer for this directory
  manyplayer cmd <cmd>      Set the command for this directory
  manyplayer pos <pos>      Set the position for this directory
  manyplayer help           Show this pretty useless help
EOT
}

if [ "x$exe" == "xnext" ] ; then
  check
  play_next_file
elif [ "x$exe" == "xsame" ] ; then
  check
  play_last_file
elif [ "x$exe" == "xtell" ] ; then
  check
  tell_last_file
elif [ "x$exe" == "xmanyplayer" ] ; then
  if [ "x$1" == "xsetup" ] ; then
    if [ "x$2" == "x" ] ; then
      echo "Need a guess statement, try '01'."
      exit 1
    else
      setup $2 $3
    fi
  elif [ "x$1" == "xteardown" ] ; then
    teardown
  elif [ "x$1" == "xcmd" ] ; then
    check
    if [ "x$2" == "x" ] ; then
      echo "Need a command statement, try 'mplayer'."
      exit 1
    else
      set_cmd $2
    fi
  elif [ "x$1" == "xpos" ] ; then
    check
    if [ "x$2" == "x" ] ; then
      echo "Need a last position statement, try '01'."
      exit 1
    else
      set_position $2
    fi
  elif [ "x$1" == "xhelp" ] ; then
    usage
  fi
else
  echo "'$exe' doesn't look like a manyplayer command..."
  exit 1
fi