#!/usr/bin/perl
################################################################################
#                                                                              #
# bildersort v0.20                                                             #
# (c)2004-2015 Jörg Wolfram   joerg@jcwolfram                                  #
#                                                                              #
# Dieses Programm ist freie Software. Sie können es unter den Bedingungen      #
# der GNU General Public License, wie von der Free Software Foundation         #
# veröffentlicht, weitergeben und/oder modifizieren, entweder gemäss           #
# Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.        #
#                                                                              #
# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, dass es       #
# Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, auch ohne die     #
# implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN          #
# BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.      #
#                                                                              #
# Sie sollten ein Exemplar der GNU General Public License zusammen mit         #
# diesem Programm erhalten haben. Falls nicht, schreiben Sie an die Free       #
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110,    #
# USA.                                                                         #
#                                                                              #
# Jede Nutzung der Software/Informationen nonkonform zur GPL oder ausserhalb   #
# des Geltungsbereiches der GPL ist untersagt!                                 #
#                                                                              #
################################################################################

$camtime[0][0]="Camera model : C902";$camtime[0][1]="";					# Mobiltelefon
$camtime[1][0]="Camera model : Canon PowerShot A530";$camtime[1][1]=" -ta-1:00 ";	# Kamera 1
$camtime[1][0]="Camera model : Canon EOS 1100D";$camtime[1][1]=" -ta-1:00 ";		# Kamera 2

$maxcam=2;

$foffset=0;
$fmuster="*.jpg";
@images = <${fmuster}>;
$maximage=$#images;
for($i=0;$i<=$maximage;$i++)
{
	$source=$images[$i];
	$tdest = "temp-".$source;
	$dummy = `mv $source $tdest`;		#move to temporary name
	$dummy = `cp $tdest temp.jpg`;		#copy to temp

	$dummy=`jhead temp.jpg`;
	for($k=0;$k<$maxcam;$k++)
	{
		$ksearch=$camtime[$k][0];
		if ($dummy =~ /$ksearch/)
		{
			$time_offset=$camtime[$k][1];
			if ($time_offset ne "")
			{
#				printf("%s\n",$camtime[$k][0]);
				$dummy=`jhead $time_offset temp.jpg`;
				$dummy=`jhead temp.jpg`;
			}
		}
	}
	$dummy =~ /Date\/Time    : /;
	$cdate = $';
	$cdate = substr($cdate,0,19);
	$filedest[$i+$foffset]=$cdate.$tdest;
}

$foffset=$maximage+1;
$fmuster="*.JPG";
@images = <${fmuster}>;
$maximage=$#images;
for($i=0;$i<=$maximage;$i++)
{
	$source=$images[$i];
	$tdest = "temp-".$source;
	$dummy = `mv $source $tdest`;		#move to temporary name
	$dummy = `cp $tdest temp.jpg`;		#copy to temp

	$dummy=`jhead temp.jpg`;
	for($k=0;$k<$maxcam;$k++)
	{
		$ksearch=$camtime[$k][0];
		if ($dummy =~ /$ksearch/)
		{
			$time_offset=$camtime[$k][1];
			if ($time_offset ne "")
			{
				printf("%s\n",$camtime[$k][0]);
				$dummy=`jhead $time_offset temp.jpg`;
				$dummy=`jhead temp.jpg`;
			}
		}
	}
	$dummy =~ /Date\/Time    : /;
	$cdate = $';
	$cdate = substr($cdate,0,19);
	$filedest[$i+$foffset]=$cdate.$tdest;
}

$foffset=$foffset+$maximage+1;

@filedest = sort(@filedest);

for($i=0;$i<$foffset;$i++)
{
	$j=$i+1;
	$bi=$j;
	if ($j<100)
	{
		$bi="0".$bi;
	}
	if ($j<10)
	{
		$bi="0".$bi;
	}
	$source = substr($filedest[$i],19,length($filedest[$i])-19);
	$dest=$pre.$bi.".jpg";
	$dummy=`mv $source $dest`;
	$dummy=`jhead $dest`;
	$dummy =~ /Resolution   : /;
	$creso = $';
	$cresox = $creso+0;
	$creso =~ /x/;
	$creso = $';
	$cresoy = $creso+0;
	if(($cresox > 1600) || ($cresoy > 1600))
	{
		$dummy=`convert -size 1600x1600 $dest -despeckle -resize 1600x1600 temp.jpg`;
		$dummy=`jhead -te $dest temp.jpg`;
		$dummy=`rm -f $dest`;
		$dummy=`mv temp.jpg $dest`;
		$dummy=`jhead -dt $dest`;
		print "Scaling ".$dest."\n";
	}
}

$dummy = `jhead -ft -autorot *.jpg`;
$dummy = `rm -f temp.jpg`;
