#!/bin/sh
# Based on http://www.safley.net/Iris_under_linux by Slowpoke
# Copyright : Public Domain.
# 20090312 : Rewrite by Hans Lambermont
# 20090317 : Added error(),$IRIS_SCRIPT,$APPEND_TO by Hans Lambermont
# 20090322 : port from bash to sh by Hans Lambermont
# 20090328 : fix dos \ creation, add c: by Hans Lambermont
#
# A small script to create an Iris script that will load a series of
# images and save it in the work directory

set -e # Exit immediately on error

WINE_DIR=${WINE_DIR:-"$HOME/.wine/drive_c"}
SCRIPT_DIR=${SCRIPT_DIR:-"$WINE_DIR/astro"}
LOAD_CMD=${LOAD_CMD:-"loadcfa"} # typically load or loadcfa
IRIS_SCRIPT=${IRIS_SCRIPT:-"irisload.pgm"}
APPEND_TO=${APPEND_TO:-"NO"} # set to YES if you want to append to $IRIS_SCRIPT

ScriptName=${0##*/}
InfileDir="$1"
InfilePrefix="$2"
# OutfileDir # Maybe as future extension
OutfilePrefix="$3"
StartNumber="$4"
EndNumber="$5"

VERSION=20090328
Count=0
IMAGES_DIR="$WINE_DIR/$InfileDir"

usage() {
	cat >&2 <<-EOT

This sh shell script generates an Iris script with which a series of
input images can be converted by Iris and saved in Iris' native PIC format.

Assumed operating environment is a Un*x with GNU find and seq, running
Iris under Wine.

Usage: $ScriptName InfileDir InfilePrefix OutfilePrefix StartNumber EndNumber

InfileDir is relative to WINE_DIR ($WINE_DIR/)

EOT
	exit 1
}

error() {
	cat >&2 <<-EOT

Error $1

If you think this is an error that Should Not Happen then cut-and-paste
the following in a mail to Iris_software@yahoogroups.com :

Error=[$1]
ScriptName=[$ScriptName]
VERSION=[$VERSION]
DATE=[`date`]
uname=[`uname -a`]
WINE_DIR=[$WINE_DIR]
SCRIPT_DIR=[$SCRIPT_DIR]
LOAD_CMD=[$LOAD_CMD]
IRIS_SCRIPT=[$IRIS_SCRIPT]
APPEND_TO=[$APPEND_TO]
InfileDir=[$InfileDir]
InfilePrefix=[$InfilePrefix]
Infile=[$Infile]
OutfilePrefix=[$OutfilePrefix]
StartNumber=[$StartNumber]
EndNumber=[$EndNumber]
IMAGES_DIR=[$IMAGES_DIR]
pwd=[`pwd`]
Count=[$Count]
find=[`find --version|head -1`]
seq=[`seq --version|head -1`]
sed=[`sed --version|head -1`]
images=[`ls -la $IMAGES_DIR/$InfilePrefix*`]
irisscript=[`cat $SCRIPT_DIR/$IRIS_SCRIPT`]

Abort script. Exit 1
EOT
	exit 1
}


if [ -z $InfileDir -o \
	  -z $InfilePrefix -o \
	  -z $OutfilePrefix -o \
	  -z $StartNumber -o \
	  -z $EndNumber ]; then
	usage
fi

echo ""
echo "Iris will save the targets in the Working Path. See File->Settings-Working_path."
echo ""
if [ "x$APPEND_TO" = "xNO" ]; then
	echo "#" > $SCRIPT_DIR/$IRIS_SCRIPT
else
	echo "#" >> $SCRIPT_DIR/$IRIS_SCRIPT
	echo "# Appending" >> $SCRIPT_DIR/$IRIS_SCRIPT
fi
echo "# $IRIS_SCRIPT created on `date`" >> $SCRIPT_DIR/$IRIS_SCRIPT
echo "# by $ScriptName version $VERSION" >> $SCRIPT_DIR/$IRIS_SCRIPT
echo "# with args [$1] [$2] [$3] [$4] [$5]" >> $SCRIPT_DIR/$IRIS_SCRIPT
for i in `seq $StartNumber $EndNumber`; do
	Count=$((Count + 1))
	InFile=`find $IMAGES_DIR -regex "$IMAGES_DIR/$InfilePrefix[0]*$i\(\..*\|\)"`
	if [ -z $InFile -o ! -f $InFile ]; then
		error "Abort script, Inputfile $IMAGES_DIR/$InfilePrefix*$i* not found"
	fi
	InFile=`echo C:$InFile|sed -e"s,$WINE_DIR,," -e's/\..*$//' -e's,/,\\\\\\\,g'`
	if [ -z $InFile ]; then
		error "Inputfile sed pipe error"
	fi
	echo "$InFile -> $OutfilePrefix$Count"
	echo "$LOAD_CMD $InFile" >> $SCRIPT_DIR/$IRIS_SCRIPT
	echo "save $OutfilePrefix$Count" >> $SCRIPT_DIR/$IRIS_SCRIPT
done

echo ""
echo "$Count input files found."
echo "Iris script saved in $SCRIPT_DIR/$IRIS_SCRIPT"
echo ""
exit 0

