#!/bin/sh
# This script demonstrates how to use the bardecode command to read images from
# one folder and copy them to another folder, renaming with the barcode value
# found in the image.
#
# For example, if the input folder is called /tmp/input and contains a file
# called image.tif, which includes a barcode of value 1234567890.
#
# Create an output folder called /tmp/output and run:
#
# ./process_folder /tmp/input /tmp/output
#
# And you should find that /tmp/output contains a file called 1234567890.tif,
# which is a copy of image.tif.

if test $# -ne 2
then
	echo "process_folder input_folder output_folder"
	exit 1
fi

input_folder=$1
output_folder=$2
path_to_bardecode=../bin/bardecode

if test ! -d $input_folder
then
	echo "$input_folder does not exist"
	exit 1
fi

if test ! -d $input_folder
then
	echo "$output_folder does not exist"
	exit 1
fi

process()
{
	type=$1

	find $input_folder -iname "*.$type" -print | {
	while read file
	do
		value=`$path_to_bardecode $opts "$file"`
		if test -n "$value"
		then
			cp "$file" "$output_folder/$value.$type"
		fi
	done
	}
}

process "tif"
process "tiff"
process "jpg"
process "jpeg"
