#! /bin/bash

#
# Marc Groenewegen  2012
#


if [ $# -lt 3 ]; then
  echo "pictcopy: copy all files starting at given number"
  echo
  echo "usage: pictcopy <basename> <start> <target> [nrofdigits]"
  echo "arguments:"
  echo "* Base name"
  echo "* Number of first file"
  echo
  exit
fi

#
# fill out firstfile to left with 0
#
# firstfile[321] (firstfile[0] - 9
# firstfile[32] (firstfile[1] - 9)
# firstfile[3] (firstfile[2] - 9)
#
#  of zoiets
#
# 4567
#
# 456[7-9]* = 4567 4568 4569
# 45[6-9]* = 4560 4561 4562 ... is niet goed
# 4[5-9]* = 4500 4501 4502 ... is niet goed
#
#
# 456[7-9]* = 4567 4568 4569
# 45[7-9]* = 4570 4571 4572 .. 4580 4581 .. 4590 4591 .. is wel goed
# 4[6-9]* = 4600 4601 4602 .. 4700 4701.. is wel goed
#
#


nrofdigits=4 # default

basename=$1
firstfile=$2
target=$3
firstfile=`expr $firstfile - 1`
firstfile=`printf %04d $firstfile` # fill out with zeroes
#echo "first file: $firstfile"

#
# prevent file names with spaces to be interpreted
#  as multiple commands by removing SPACE and TAB from
#  the internal variable IFS
#
#export IFS='\n'


for digit in `seq 3 -1 1`;
do
  #echo "digit: $digit"
  pattern=${firstfile:0:$digit}
  #echo "pattern: $pattern"
  last=${firstfile:$digit:1}
  last=`expr $last + 1`
  #echo "last: $last"
  if [ $last -eq 10 ]; then
    continue # skip
  fi
  copystring="$basename$pattern[$last-9]*"
  cp $copystring $target
done

