summaryrefslogtreecommitdiff
path: root/manyplayer
blob: d4dfa1710d1b5b4e757132f794c1e5e12789c171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/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