#! /bin/bash

#############################################################################
#  Copyright (C) 2003  Enno Bartels   <ennobartels@t-online.de>             #
#############################################################################
#                                                                           #
#  This program is free software; you can redistribute it and/or            #
#  modify it under the terms of the GNU Library 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        #
#  Library General Public License for more details.                         #
#                                                                           #
#  You should have received a copy of the GNU Library General Public        #
#  License along with this program; see the file COPYING.LIB.  If not,      #
#  write to the Free Software Foundation, Inc., 59 Temple Place -           #
#  Suite 330,  Boston, MA 02111-1307, USA.                                  #
#                                                                           #
#############################################################################
#                                                                           #
#  Project name.....  minolta_dimage_xt                                     #
#                                                                           #
#  Modul name.......  movePictures.sh                                       #
#                                                                           #
#  Starting dates...  1.Okt.2003 (Enno Bartels)                             #
#                                                                           #
#  Version..........  0.1                                                   #
#                                                                           #
#  Author...........  Enno Bartels <ennobartels@t-online.de>                #
#                                                                           #
#  Description......  Tool for the Minolta Dimage Xt Camera                 #
#                                                                           #
#############################################################################/


# Check die Kommandozeilenargumente
if test "$#" != "2"; then
 echo "usage:  movePicture.sh von new"
 exit 
fi



# Berechne die Differenz
let "diff = $2 - $1"


###############################################################################
# Schritt 1: Aus  xxx.*  wird  tyyy.*                                         #
###############################################################################
BLA1=`find . \( -name "*.?*"  \)  -maxdepth 2 -print | sort`

for FILENAME_IN in $BLA1
do
  dir_name=`dirname $FILENAME_IN`
  base_name=`basename $FILENAME_IN`

  fn_zahl=`echo $base_name | cut -d'.' -f1 | sed "s/^0//" | sed "s/^0//" | sed "s/^0//" | sed "s/^0//"`

  fn_zahl1=`echo $base_name | cut -b1`

  fn_rest=`echo $base_name | cut -d'.' -f2-200`

  let  "fn_new = $fn_zahl + $diff"
  fn_new2=`printf %03d $fn_new`



  # Nur die Dateien umbenennnen diean 2ter Stelle  mit einer Zahl beginnen!
  if test `echo $fn_zahl1  | grep [0-9]`; then 

    # Nur die Dateien umbenennnen die größer gleich der Startzahl sind !
    if test $1 -le $fn_zahl; then  
      echo "   Working on:  $FILENAME_IN  ->  ${dir_name}/t${fn_new2}.${fn_rest}"
      mv   $FILENAME_IN  ${dir_name}/t${fn_new2}.${fn_rest}
    fi

  fi
done


echo " Teil 1 fertig"
echo ""
echo ""





###############################################################################
# Schritt 2: Aus  tyyy.*  wird  yyy.*                                         #
###############################################################################
BLA1=`find . \( -name "t*.?*"  \)  -maxdepth 2 -print | sort`

for FILENAME_IN in $BLA1
do
  dir_name=`dirname $FILENAME_IN`
  base_name=`basename $FILENAME_IN`

  fn_zahl=`echo $base_name | cut -d'.' -f1 | sed "s/^t//" | sed "s/^0//" | sed "s/^0//" | sed "s/^0//" | sed "s/^0//" `

  fn_zahl1=`echo $base_name | sed "s/^t//" | cut -b1`

  fn_rest=`echo $base_name | cut -d'.' -f2-200`

  fn_new2=`printf %03d $fn_zahl`



  # Nur die Dateien umbenennnen diean 2ter Stelle  mit einer Zahl beginnen!
  if test `echo $fn_zahl1  | grep [0-9]`; then 
      echo "   Working on: $FILENAME_IN  ->  ${dir_name}/${fn_new2}.${fn_rest}"
      mv   $FILENAME_IN  ${dir_name}/${fn_new2}.${fn_rest}
  fi
   
done

echo " Teil 2 fertig"

