#! /usr/bin/perl
#----------------------------------------------------------------------
#
#
#----------------------------------------------------------------------

opendir ( DIR, "$ARGV[1]" ) || die "opendir failed\n";
@files = readdir (DIR);
closedir (DIR);


$pic_dir = $ARGV[1];
    
$newpic_dir = $ARGV[1] . "/" . "newpics";
system "mkdir $newpic_dir";

if ( $ARGV[0] eq "-r" )  {
  &rotateright;
}
elsif ($ARGV[0] eq "-l" ) {
  &rotateleft;
}
elsif ($ARGV[0] eq "-n" ) {
    &normal;
}

elsif ($ARGV[0] eq "-tiff" ) {
    &tiff;
}


#----------------------------------------------------------------------
sub rotateleft
{
  foreach $i (@files) 
  {
    $j = $i;
    $i =~ s/.+\/(.*)$/$1/g;       #bevarer KUN filnavn
    $i =~ /(.+)(\..*)$/g;         #kutter endelsen i filnavnet
    system "hpcdtoppm -l -3 $pic_dir/$j | cjpeg > $newpic_dir/$1_big.jpg";
    system "djpeg $newpic_dir/$1_big.jpg | pnmscale -height 512 | cjpeg -optimize > $newpic_dir/$1.jpg";
    print "files : $j : dollar_1 = $1 _ dollar2 : $2\n";
  }
}

#----------------------------------------------------------------------
sub rotateright
{
  foreach $i (@files)
  {
    $j = $i;
    $i =~ s/.+\/(.*)$/$1/g;       #bevarer KUN filnavn
    $i =~ /(.+)(\..*)$/g;         #kutter endelsen i filnavnet
    system "hpcdtoppm -r -3 $pic_dir/$j | cjpeg > $newpic_dir/$1_big.jpg";
    system "djpeg $newpic_dir/$1_big.jpg | pnmscale -height 512 | cjpeg -optimize > $newpic_dir/$1.jpg";
    print "files : $pic_dir/$j : dollar_1 = $1 _ dollar2 : $2\n"; 
  }
}

#----------------------------------------------------------------------
sub normal
{
 foreach $i (@files) 
  {
    $j = $i;
    $i =~ s/.+\/(.*)$/$1/g;       #bevarer KUN filnavn
    $i =~ /(.+)(\..*)$/g;         #kutter endelsen i filnavnet
    system "hpcdtoppm -3 $pic_dir/$j | cjpeg > $newpic_dir/$1_big.jpg";
    system "djpeg $newpic_dir/$1_big.jpg | pnmscale -width 512 | cjpeg -optimize > $newpic_dir/$1.jpg";
     
    print "Normal\n";
    #print "files : $pic_dir/$j : dollar_1 = $1 _ dollar2 : $2\n"; 
  }


}

#----------------------------------------------------------------------

sub tiff
{
    print "tiff\n";
  foreach $i (@files)
  {
    if (! -d $i )
    {
       $j = $i;
       $i =~ s/.+\/(.*)$/$1/g;       #bevarer KUN filnavn
       $i =~ /(.+)(\..*)$/g;         #kutter endelsen i filnavnet
       $k = $1;
       $k =~ s/\s/_/g;
       system "/usr/local/netpbm/tifftopnm \"$j\" | pnmscale -width 512 | cjpeg -optimize > $newpic_dir$k.jpg";
      # print "dollar i : $i \t dollar_k : $k \t\n ";
    }
   }
}     
   

#----------------------------------------------------------------------





