#!/bin/sh

ix="index.html"
vl="vobcut.log"
ml="mplayer.log"
mi="mplayer.idx"
lk="links.html"

[ ! -d .vobcut ] && echo "no .vobcut directory" && exit 1

for vob in "$@"; do
  d=".vobcut/$vob.dir"
  v="../../$vob"
  hvob="$( echo $vob | recode utf8..h4 )"
  mkdir -p "$d"
  if [ ! -s "$d/$ix" ]; then
#  if true; then
    [ -s "$d/$vl" ] || vobcut -elm "$vob" > "$d/$vl" &
    cd "$d" || exit 1
    [ -s "$ml" ] || mplayer "$v" \
            -really-quiet -msglevel all=-1:statusline=5:vfilter=5 \
            -vc ffmpeg12 -nosound -vo png:z=2 -lavdopts \
            skiploopfilter=all:skipidct=all:skipframe=nonref:er=1:ec=0 \
            -vf framestep=I,scale=-2:72 -benchmark > "$ml"
            
    tr '/\r' ' \n' < "$ml" | grep -B1 '^I!'| grep "^V:" | cut -c3- > "$mi"
    
    echo -en '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n' > "$lk"
    ls | sort | grep '\.png$' | paste - "$mi" | while read p t f z; do
      if [ -e "$p" -a -n "$t" ]; then
        echo "<img src=$p height=72 title=\"$f ${t}s\" alt=\" -f$t\">"
      else
        echo "($p)"
      fi
    done >> "$lk"
    
    echo "<html>" > "$ix"
    echo "<h3 align=center>$hvob</h3>" >> "$ix"
    echo "<pre>" >> "$ix"
    grep '^. [ 0-9]:\|^[ 0-9]*blocks total\.' "$vl" >> "$ix"
    echo "</pre>" >> "$ix"
    echo "<hr>" >> "$ix"

    for p in 0*[02468]1.png; do
      i=$[ 1${p%%.png}-100000000 ]
      id="x$i.html"
      echo "<a href=\"$id\"><img src=$p height=72 title=\"$i\"></a>" >> "$ix"
      
      echo "<html><h4 align=center>$hvob $i</h4>" > "$id"
      echo "<h5 align=center><a href=\"x$[i-20].html\">[prev]</a><a href=index.html>[up]</a><a href=\"x$[i+20].html\">[next]</a></h5>" >> "$id"
      tail +$i "$lk" | head -60 >> "$id"
      echo "</html>" >> "$id"
    done  
    echo "</html>" >> "$ix"
    cd ../..
  fi  
done

cd .vobcut || exit 1

for c in *.cache; do
  if [ ! -e "../${c%%.cache}" ]; then
    rm "$c"
  fi
done 

for d in *.dir; do
  if [ ! -e "../${d%%.dir}" ]; then
    rm -r "$d"
  fi
done 

echo "<html>" > index.html
echo "<h4><a href=index.html>by name</a> <a href=tindex.html>by time</a>  <a href=sindex.html>by size</a></h4><hr>" >> index.html
ls ../*.vob | while read v; do
  if [ -e "${v#../}.dir" ]; then
    d="${v#../}.dir"
    n="$( echo ${d%%.dir}|recode utf8..h4)"
    t=$( head -1 "$d/$vl" | cut -f2,4 -d, )
    echo "<a href=\"$n.dir/$ix\">$n</a> ($t)<br>" 
  fi
done >> index.html
echo "</html>" >> index.html

echo "<html>" > tindex.html
echo "<h4><a href=index.html>by name</a> <a href=tindex.html>by time</a>  <a href=sindex.html>by size</a></h4><hr>" >> tindex.html
ls -t ../*.vob | while read v; do
  if [ -e "${v#../}.dir" ]; then
    d="${v#../}.dir"
    n="$( echo ${d%%.dir}|recode utf8..h4)"
    t=$( head -1 "$d/$vl" | cut -f2,4 -d, )
    echo "<a href=\"$n.dir/$ix\">$n</a> ($t)<br>" 
  fi
done >> tindex.html
echo "</html>" >> tindex.html

echo "<html>" > sindex.html
echo "<h4><a href=index.html>by name</a> <a href=sindex.html>by time</a>  <a href=sindex.html>by size</a></h4><hr>" >> sindex.html
ls -S ../*.vob | while read v; do
  if [ -e "${v#../}.dir" ]; then
    d="${v#../}.dir"
    n="$( echo ${d%%.dir}|recode utf8..h4)"
    t=$( head -1 "$d/$vl" | cut -f2,4 -d, )
    echo "<a href=\"$n.dir/$ix\">$n</a> ($t)<br>" 
  fi
done >> sindex.html
echo "</html>" >> sindex.html

