#!/usr/bin/perl
;#
;# get_ds7 version 1.2.4 1998/08/01
;#                          Copyright (C) 1997,1998 Mamoru Ohno
;#                               E-mail: mamo@yk.rim.or.jp 
;# Usage get_ds7 [-DSC | -uXXX] [-v] [-help] [frame,...]
;#
;# $BIY;N%U%$%k%`(B $B%G%8%?%k%+%a%i(B DS-7 $B2hA|%G!<%?<u?.%W%m%0%i%`(B Version 1.2.4
;#
;# linux(2.0.35) $B$H(B perl(5.004) $B$GF0:n$r3NG'(B
;# 
;# --------------------------------------
;# 1.2.4 $BJQ99E@(B
;# $B%U%!%$%k=q$-9~$_$K<:GT$7$?(B(disk full$BEy(B)$B>l9g$K!"%(%i!<%a%C%;!<%8$r(B
;# $B=PNO$7!"=*N;$9$kMM$K=$@5(B
;#
;# --------------------------------------
;# 1.2.3 $BJQ99E@(B
;# $B>>K\K-MMDs6!$N!V(BXXXX.1.jpg $B7A<0(B(1$B$NItJ,$O%3%^HV9f(B)$B!W$N%U%!%$%k7A<0(B
;# $B5!G=$rDI2C!#0J2<$O>>K\K-MM$N%3%a%s%H$G$9!#(B
;# $B!V;d$O!"(Bnagano.1.jpg $B$H$+(B 970721.2.jpg $B$NMM$K(B "$BG$0U$NJ8;z(B.$B%3%^HV9f(B.
;# jpg" $B$N$h$&$K%U%!%$%kL>$rL?L>$9$k;v$,9%$-$J$N$G!"(B-u $B%*%W%7%g%s$rDI(B
;# $B2C$7$^$7$?!#!W(B
;#
;# --------------------------------------
;# 1.2.2$B&B(B $BJQ99E@(B
;# DS-10$B$GH/@8$9$kF|IU$,<hF@$G$-$J$$$?$a$K!"%U%!%$%k=PNO$K(B
;# $B<:GT$9$k%P%0$r=$@5(B
;#    $B;#1FF|IU$,<hF@$G$-$J$$>l9g$O!"8=:_;~4V$r%U%!%$%kL>$K$9$k(B
;#
;# --------------------------------------
;# 1.2.1 $BJQ99E@(B
;# get_ds7 -help $B$G=P$F$$$?%(%i!<$r=$@5(B
;# perl5.003 $B0JA0$GH/@8$9$kJ#?t%$%a!<%8%@%&%s%m!<%I;~$K(B
;# $B%U%!%$%kL><hF@$K<:GT$9$k%P%0$r=$@5(B
;#
;# --------------------------------------
;# 1.2 $BJQ99E@(B
;# $B=PNO%U%!%$%kL>7A<0$r0z?t$G@ZBX$((B
;# Usage:
;#           get_ds7 -DSC             DSC00001.JPG $B7A<0(B
;#           get_ds7                  19970429224550.jpg $B7A<0(B
;#           get_ds7 -uXXXX           XXXX.1.jpg $B7A<0(B(1$B$NItJ,$O%3%^HV9f(B)
;# Usage,version $B=PNO5!G=$rDI2C(B
;# Usage:
;#           get_ds7 -help            Usage $B=PNO(B
;#           get_ds7 -v               version $B=PNO(B
;# $BJ#?t%$%a!<%8%@%&%s%m!<%I;~$K%U%!%$%kL><hF@$K<:GT$9$k%P%0$r=$@5(B
;#
;# --------------------------------------
;# 1.1 $BJQ99E@(B
;# $B%U%8%U%#%k%`(B $B"*(B $BIY;N%U%$%k%`(B
;# $B%3%^HV9f;XDj5!G=$rDI2C(B
;# Usage:
;#           get_ds7                  $BA4%3%^<u?.(B
;#           get_ds7 -2,4-7,15,20-    $B%3%^;XDj<u?.(B(1,2,4,5,6,7,15,20,$B!D(B)

use ds7_lib;

my($version) = 1.24;
my ($i,$buf,$thumb,$num,$fname,$date,$ftype,$serial_line,$serial_speed);

;# DS-7 $B$N@\B3$5$l$?%7%j%"%k$N%U%!%$%kL>$r(B $serial_line $B$K@_Dj(B
;# COM1 $B$N>l9g$O(B "/dev/cua0"
;# COM2 $B$N>l9g$O(B "/dev/cua1"

$serial_line = "/dev/cua1";

;# $BDL?.B.EY$r(B $serial_speed $B$K@_Dj(B
;# $BDL?.B.EY$K$O!"(B57600,38400,19200,9600 $B$NCf$N$R$H$D$rA*Br(B

$serial_speed = "57600";

;#
;#
my($usage) = <<USG;
get_ds7 is a program to get the data of pictures sequentially from FUJIFILM DS-
7(DS series) and to put them into each file.
Usage: get_ds7 [-DSC | -uXX] [-v] [-help] [frame,...]
  -DSC  Output filename is same as in the DS-7.(ex. DSC00001.JPG)
  -uXX  Output filename has user specified prefix and frame No.(ex. kids.1.jpg) 
        Default filename is the date and time when the picture had taken.
        (ex. 19970429224550.jpg)

  -v    Output version information and exit.
  -help Display this help and exit.

  frame -2,4-7,15,20- -> Frame No.1,2,4,5,6,7,15,20,...
        Running get_ds7 without frame number(s),you will get all of frames
        in the DS-7.
USG
;#
;#
$ftype = 0;
$prefix = "" ;
foreach $i (@ARGV){		# $B%3%^%s%I0z?t%A%'%C%/(B

    $ftype = 1 if($i eq "-DSC"); # $B%3%^%s%I%*%W%7%g%s$K(B -DSC $B$r;XDj!)(B

    if($i =~ /^-u(.*)/ ){	# $B%3%^%s%I%*%W%7%g%s$K(B -u $B$r;XDj(B
	$ftype = 2 ;		# 2$B$O(Buser mode 
	if( $1 eq "" ){
            $prefix = "frame" ;
	} else {
	    $prefix = $1 ;
	}
    }

    if($i eq "-help") {		# Usage $B$rI=<((B
	die ("$usage");
    }

    if($i eq "-v") {		# version$BI=<((B
	my($lib_version)=&ds7lib_version;
	die ("This is get_ds7,version $version-Lib$lib_version.\n");
    }
}

$num = open_ds7(cua1,$serial_line); # DS-7 $B$r%*!<%W%s$9$k(B($num$B$K%3%^?t(B)
&set_speed(cua1,$serial_speed) || # $BDL?.B.EY@_Dj(B
    die "Can't set speed $serial_speed";
printf("%d images\n",$num);	# $B<}O?Kg?tI=<((B

@framelist = &extend_param($num); # $B%3%^HV9f<hF@(B
foreach $i (@framelist)		# $B;XDj$5$l$?%3%^HV9f$K$D$$$F<B9T(B
{
    $fname = &image_name(cua1,$i); # $B%U%!%$%kL><hF@(B(DSC000??.JPG $B7A<0(B)
    printf("%s\n",$fname);
    $buf = &get_image(cua1,$i);	# $B%$%a!<%8<hF@(B

    if ($ftype == 0) {
	$date = &image_date($buf);	# $B;#1F;~4V<hF@(B(1997:05:01 18:14:49)
	$date =~ s/[\s:]//g;	# $B%3%m%s$H%9%Z!<%9$r:o=|(B
	if($date != ""){	
	    $fname = "$date.jpg";
	} else {
	    # $B;#1F;~4V$,<hF@$G$-$J$$(B(DS-10$BEy(B)$B>l9g$O8=:_;~4V@_Dj(B
	    my($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
	    if($year > 70) {
		$year = sprintf("19%2d",$year);
	    } else {
		$year = sprintf("20%2d",$year);
	    }
	    $fname = sprintf("%4d%02d%02d%02d%02d%02d.jpg",
			     $year,$mon+1,$mday,$hour,$min,$sec);
	}
    } elsif($ftype == 2) {
	# $B%f!<%6;XDj$N(Bprefix
	$_ = $fname ;
	/DSC0*(\d+)/ ;	# $1$B$,<h$l$J$+$C$?;~$N%(%i!<=hM}$bI,MW$+$J$"!)(B 
	$fname = sprintf("%s.%s.jpg", $prefix,$1);
    }
    open(FP,"> $fname") || die "Can't open $fname\n"; # $B%U%!%$%k=PNO(B
    $written = syswrite(FP,$buf,length($buf));
    die "System write error: $!\n" unless defined $written;
    close(FP);
}
close_ds7(cua1);

;# $B;XDj$5$l$?%3%^HV9f$rG[Ns$GJV5Q$9$k!#(B
;# Usage:
;#           &extend_param($num)
;#                          $num:             $BA4%3%^?t(B
;#                          RETURN            $B;XDj$5$l$?%3%^HV9f$NG[Ns(B
sub extend_param
{
    my($num) = @_;
    my($i,@F,$s,$e,%b,@a);
    foreach $i (@ARGV){
	push(@F,split(/,/,$i)); # $B%3%s%^$G6h@Z$C$FJ#?t;XDj(B
    }
    foreach (@F) {
	if (($s) = /^(\d+)$/) {	# $BC1;XDj(B
	    $e = $s;
	} elsif( ($s,$e) = /^(\d*)-(\d*)$/) { # $BHO0O;XDj(B
	    $s=1 if $s eq '';	# -10 $B;XDj7A<0(B
	    $e=$num if $e eq ''; # 10- $B;XDj7A<0(B
	} else {		# $B;XDj%(%i!<(B
	    next ;
	}
	$e=$num if $e > $num;	# $B:GBgCM8m$jJd@5(B
	push @a , ($s..$e);	# $BHO0OJd4V(B
    }
    if($#ARGV == -1 || 
       (($#ARGV == 0) && ($ARGV[0] eq "-DSC" || $ARGV[0] =~ /^-u/ ))) {
				# $B%3%^HV9f;XDj$,$J$$>l9g$OA4;XDj(B
	push @a , (1..$num);
    }
    foreach (@a) {		# $B=EJ#$rL5$/$9(B
	$b{$_} = $_ if $_ ne 0;	# 0 $B$,;XDj$5$l$F$bL5;k(B
    }
    @a = sort{$a<=>$b}(keys %b); # $B%=!<%H$7$FJV5Q(B
}
