#!/bin/sh
##############################################################################
# convert-all.sh
#
# Automatic video shell converter for Sony NWZ-E438F.
#
# ----------------------------------------------------------------------------
# convert-all.sh - Automatic video converter for Sony Walkman NWZ Series
#   (C) 2009 Gerardo García Peña <gerardo@kung-foo.dhs.org>
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the Free
#   Software Foundation; either version 2 of the License, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#   more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc., 51
#   Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
###############################################################################

# if you don't like the "walk-video" or any other folder name CHANGE IT HERE!
BASEDIR="$HOME/walk-video"
INDIR="$BASEDIR/in"
OUTDIR="$BASEDIR/out"
TMPDIR="$BASEDIR/tmp"

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Auto update routine
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

update()
{
  ND=`date +%s`
  ND=`date -d @$(($ND + 15*24*3600)) +%Y%m%d%H%M`
  
  touch "$BASEDIR/.last-exec"

  if [ -e "$BASEDIR/.next-update" ]; then
    if [ "$BASEDIR/.next-update" -ot "$BASEDIR/.last-exec" ]; then
      echo "------------------------------------------------------------"
      echo "Checking for new versions..."
      wget -N "http://kung-foo.dhs.org/killabyte/code/sony-nwz-convert-tool/convert-all.sh" -P "$TMPDIR" -o "$TMPDIR/update.log"
      touch -t "$ND" "$BASEDIR/.next-update"
      echo "------------------------------------------------------------"
    fi
    if [ "$0" -ot "$TMPDIR/convert-all.sh" ]; then
      echo "------------------------------------------------------------"
      echo "THERE IS A NEW VERSION"
      echo ""
      echo -n "  Do you want to install the new version [Y/n]? "
      read R
      if [ -z "$R" -o "$R" == "Y" -o "$R" == "y" ]; then
        echo "Updating script..."
        mv -f "$TMPDIR/convert-all.sh" "$0"
        chmod +x "$0"
      fi
      echo "------------------------------------------------------------"
    fi
  else
    touch -t "$ND" "$BASEDIR/.next-update"
  fi
}

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Converstion routine
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

convert()
{
  if [ -z "$1" ]; then
    echo "  ! Debes indicar un fichero de entrada." 1>&2
    return 1
  fi

  if [ ! -e "$1" ]; then
    echo "  ! El fichero '$1' no existe." 1>&2
    return 1
  fi

  IN="$1"
  OUT="$OUTDIR/`basename "$1" | sed 's#\.[^.]\+$##'`"
  TMP="$TMPDIR/x$$x"

  if [ -e "$OUT.mp4" ]; then
    echo "  - El fichero '`basename "$IN"`' ya ha sido convertido." 1>&2
    echo "  - Borrar primero '$OUT.mp4' para volver a convertir." 1>&2
    return 1
  fi

  ffmpeg -y -i "$IN"            \
         -vframes 1 -ss 10 -an  \
         -s 160x120             \
         -qmin 20 "${TMP}%d.jpg"
  ffmpeg -y -i "$IN"            \
         -threads 2             \
         -s 320x240             \
         -r 30.00               \
         -pix_fmt yuv420p       \
         -g 300 -qmin 3 -b 512k \
         -async 50              \
         -acodec libmp3lame     \
         -ar 44100 -ac 1        \
         -ab 16k "${TMP}.mp4"
  mv "${TMP}1.jpg" "$OUT.jpg"
  mv "${TMP}.mp4" "$OUT.mp4"

  return "$?"
}

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Main program
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

mkdir -p "$INDIR" "$OUTDIR" "$TMPDIR"

update
exit 0

echo "Converting all files in '$INDIR'..."
find "$INDIR" -type f \
| while read F; do
  echo "  [Procesing $F]"
  convert "$F" || echo "  !! NO PUDE CONVERTIR '$F'"
done

echo "Done!"

exit 0
