#!/bin/bash
SVDRPSEND=/usr/lib/vdr/svdrpsend.pl
sortext="ª"
sortpath=${1%/*}
videodir=$sortpath
while [ ! -e $videodir/.update ]; do
  videodir=${videodir%/*}
  if [ -z "$videodir" ]; then
    echo "$SVDRPSEND MESG \"Im Videoverzeichnis fehlt die Datei '.update'!\" >> /dev/null 2>&1" | at now
    exit
  fi    
done
sortpath=${sortpath%/*}
if [ -e $sortpath/.update ]; then
  echo "$SVDRPSEND MESG \"Hier kann nur nach Name sortiert werden!\" >> /dev/null 2>&1" | at now
else
  newpath=${sortpath/%$sortext/}
  newdir=`basename $newpath`
  if [ $sortpath == $newpath ]; then
    echo "$SVDRPSEND MESG \"'$newdir' wird jetzt nach Name sortiert.\" >> /dev/null 2>&1" | at now
    newpath=$newpath$sortext
  else
    echo "$SVDRPSEND MESG \"'$newdir' wird jetzt nach Datum sortiert\" >> /dev/null 2>&1" | at now
  fi
#  echo mv $sortpath $newpath
  mv $sortpath $newpath
  touch $videodir/.update
fi
