anndl: Print raw names in choice list.
[utils.git] / planime
diff --git a/planime b/planime
index 899a1c9..854ef7f 100755 (executable)
--- a/planime
+++ b/planime
-#!/bin/sh
+#!/bin/bash
 
-cmdline=(mplayer -fs)
+isnum() {
+    grep -xq '[0-9]\+' <<<"$1"
+}
+
+scorefile() {
+    if [ "${1##*.}" = mkv ]; then
+       echo 10
+    elif [ "${1##*.}" = ogm ]; then
+       echo 5
+    else
+       echo 0
+    fi
+}
+
+findbase() {
+    if [ -r aliases ]; then
+       while read alias rest; do
+           if [ "$alias" = "$1" ]; then
+               echo "$rest"
+               return
+           fi
+       done <aliases
+    fi
+    echo "$1"
+}
+
+findfile() {
+    if [ -n "$debug" ]; then echo "finding base='$1', ep='$2', qual='$3'" >&2; fi
+    local base file tail eq eqt m matches max score
+    matches=()
+    base="$(findbase "$1")"
+    for file in "$base"*; do
+       tail="${file#"$base"}"
+       eq="${tail%.*}"
+       m=n
+       if [ "${eq%% *}" -eq "$2" ] 2>/dev/null; then
+           if [[ "$eq" == *\ * ]]; then
+               eqt="${eq#* }"
+           else
+               eqt=
+           fi
+           m=y
+       elif [ "${eq:0:${#2}}" = "$2" ]; then
+           eqt="${eq:${#2}}"
+           if [ "${eqt:0:1}" = " " -o -z "$eqt" ]; then
+               eqt="${eqt# }"
+               m=y
+           fi
+       fi
+       if [ "$m" = y ]; then
+           if [ "$eqt" = "$3" -o "$eqt" = "($3)" -o "${eqt:0:2}" = "- " ]; then
+               matches=("${matches[@]}" "$file")
+           fi
+       fi
+    done
+    if [ ${#matches[@]} -lt 1 ]; then return 1; fi
+    max=
+    for m in "${matches[@]}"; do
+       score="$(scorefile "$m")"
+       if [ -n "$debug" ]; then echo "found \`$m': score $score" >&2; fi
+       if [ -z "$max" ] || [ "$score" -gt "$max" ]; then
+           max="$score"
+           file="$m"
+       fi
+    done
+    if [ -n "$debug" ]; then echo "using \`$file'" >&2; fi
+    echo "$file"
+    return 0
+}
+
+origargs=("$0" "$@")
+cmdline=(mpv -fs)
+debug=
+log=y
+unset pretend printfile
 
 while [ "${1:0:1}" = - ]; do
     a="$1"
     shift
     case "$a" in
+       -h)
+           echo "usage: planime [-fdhtCL] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
+           exit 0
+           ;;
+       -d)
+           debug=y
+           ;;
        -t)
-           cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
+           cmdline=("${cmdline[@]}" -audio-device 'alsa/hdmi:CARD=PCH,DEV=0')
+           DISPLAY=:1
+           chwp=y
+           ;;
+       -C)
+           pretend=y
+           ;;
+       -L)
+           log=
+           ;;
+       -A)
+           while :; do
+               if [ $# -lt 1 ]; then
+                   echo "planime: unterminated argument list" >&2
+                   exit 1
+               fi
+               a="$1"
+               shift
+               if [ "$a" = \; ]; then
+                   break;
+               fi
+               cmdline=("${cmdline[@]}" "$a")
+           done
            ;;
        -s)
            savepar="$1"
@@ -15,70 +118,185 @@ while [ "${1:0:1}" = - ]; do
            saveval="$1"
            shift
            ;;
+       -f)
+           printfile=y
+           ;;
+       -v)
+           cmdline=("${cmdline[@]}" -v)
+           ;;
+       --)
+           break
+           ;;
+       *)
+           echo "planime: unknown option \`$a'"
+           exit 1
+           ;;
     esac
 done
 
-if [ $# -lt 1 ]; then
-    echo "usage: planime [QUAL] EPNUM" >&2
-    exit 1
-fi
-if [ $# -eq 1 ]; then
-    ep="$1"
-    unset qual
+dirbase="$(basename "$(pwd)")"
+base=.
+nextep=n
+file=
+tqual=
+if [ $# -eq 0 ]; then
+    nextep=y
+elif [ $# -eq 1 ]; then
+    if [ -r "$1" ]; then
+       file="$1"
+    else
+       base=
+       ep="$1"
+    fi
 elif [ $# -eq 2 ]; then
-    qual="$1"
+    if findfile "$dirbase - " "$1" "$2" >/dev/null; then
+       base=""
+       ep="$1"
+       tqual="$2"
+    else
+       base="$1"
+       ep="$2"
+    fi
+else
+    base="$1"
     ep="$2"
+    tqual="$3"
 fi
-
-if [ ! -r "$ep" ]; then
-    if [ -z "$qual" ]; then
-       ep="$(basename "$(pwd)") - ${ep}"
+if [ "$ep" = . ]; then nextep=y; fi
+if [ "$nextep" = y -a -r nextep ]; then
+    ep="$(<nextep)"
+    if ! isnum "$ep"; then
+       echo "planime: nextep is non-numeric" >&2
+       exit 1
+    fi
+fi
+if [ -z "$file" ]; then
+    if [ "$base" = . ]; then
+       if [ -r curser ]; then
+           curser="$(<curser)"
+       else
+           curser=
+       fi
     else
-       ep="$(basename "$(pwd)") ${qual} - ${ep}"
+       curser="$(findbase "$base")"
     fi
-    if [ -r "${ep}.ogm" ]; then
-       ep="${ep}.ogm"
-       defopts=(-aid 1 -sid 0)
-    elif [ -r "${ep}.mkv" ]; then
-       ep="${ep}.mkv"
-       defopts=(-slang eng -alang jpn)
-    elif [ -r "${ep}.mpg" ]; then
-       ep="${ep}.mpg"
-    elif [ -r "${ep}.mp4" ]; then
-       ep="${ep}.mp4"
-    elif [ -r "${ep}.avi" ]; then
-       ep="${ep}.avi"
+    if [ -n "$curser" ]; then
+       file="$(findfile "$dirbase $curser - " "$ep" "$tqual")" || \
+           file="$(findfile "$curser - " "$ep" "$tqual")"
+    else
+       file="$(findfile "$dirbase - " "$ep" "$tqual")"
     fi
 fi
 
-if [ ! -r "$ep" ]; then
-    echo "no such episode: $ep" >&2
+if [ -z "$file" -o ! -r "$file" ]; then
+    echo "planime: no matching file found" >&2
     exit 1
 fi
 
-ifile=".${ep}.info"
+case "${file##*.}" in
+    ogm)
+       aid=1
+       sid=0
+       ;;
+    mkv)
+       alang=jpn
+       slang=eng
+       ;;
+esac
+
+ifile=".${file}.info"
 
 if [ -n "$savepar" ]; then
     if [ -r "$ifile" ]; then
-       exec 3<"$ifile"
-       rm "$ifile"
-       egrep -v "^${savepar}:" 0<&3 >"$ifile"
-       exec 3<&-
+       egrep -v "^${savepar} " "$ifile" >"$ifile.new"
+       mv -f "$ifile.new" "$ifile"
     fi
-    echo "$savepar: $saveval" >>"$ifile"
+    echo "$savepar $saveval" >>"$ifile"
     exit 0
 fi
 
 unset delay
 
 if [ -r "$ifile" ]; then
-    delay="`sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile"`"
-    cmdline=("${cmdline[@]}" -delay "$delay")
-else
-    cmdline=("${cmdline[@]}" "${defopts[@]}")
+    exec 3<&0
+    exec 0<"$ifile"
+    while read par arg; do
+       if [ "$par" = delay ]; then
+           cmdline=("${cmdline[@]}" -delay "$arg")
+       elif [ "$par" = aspect ]; then
+           cmdline=("${cmdline[@]}" -aspect "$arg")
+       elif [ "$par" = volmod ]; then
+           cmdline=("${cmdline[@]}" -af volume="$arg")
+       elif [ "$par" = alang ]; then
+           unset alang aid
+           alang="$arg"
+       elif [ "$par" = aid ]; then
+           unset alang aid
+           aid="$arg"
+       elif [ "$par" = slang ]; then
+           unset slang sid
+           slang="$arg"
+       elif [ "$par" = sid ]; then
+           unset slang sid
+           sid="$arg"
+       fi
+    done
+    exec 0<&3
+    exec 3<&-
 fi
 
-if [ "`hostname`" = pc6 ]; then chwp=y; fi
+if [ -n "$alang" ]; then
+    cmdline=("${cmdline[@]}" -alang "$alang")
+elif [ -n "$aid" ]; then
+    cmdline=("${cmdline[@]}" -aid "$((aid + 1))")
+fi
+if [ -n "$slang" ]; then
+    cmdline=("${cmdline[@]}" -slang "$slang")
+elif [ -n "$sid" ]; then
+    cmdline=("${cmdline[@]}" -sid "$((sid + 1))")
+fi
 
-if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
-"${cmdline[@]}" "$ep"
+if [ -n "$pretend" ]; then
+    echo "${cmdline[@]}" "$file"
+elif [ -n "$printfile" ]; then
+    echo "$file"
+else
+    if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
+    if [ "$log" = y ]; then
+       if [ -d $HOME/.anime ]; then
+           echo "$(date) $(basename "$file")" >>$HOME/.anime/plog
+       fi
+    fi
+    "${cmdline[@]}" "$file"
+    
+    if [ "$nextep" = y ]; then
+       echo "0. Save and continue (or Space)"
+       echo "1. Continue without saving"
+       echo "2. Save and exit (or Enter)"
+       echo "3. Exit without saving (or any key)"
+       IFS= read -sn1 c
+       save=n
+       cont=n
+       case "$c" in
+           0|" ")
+               save=y
+               cont=y
+               ;;
+           1)
+               cont=y
+               ;;
+           2|"")
+               save=y
+               ;;
+       esac
+       if [ "$save" = y ]; then
+           let ep++
+           echo "$ep" >nextep
+       fi
+       if [ "$cont" = y ]; then
+           exec "${origargs[@]}"
+       else
+           echo "nextep is $ep"
+       fi
+    fi
+fi