#!/usr/local/bin/perl -w

open(FIL,$ARGV[0]); @lines = <FIL>; close(FIL);

open(FIL1,">".$ARGV[0].".1.sh");
open(FIL2,">".$ARGV[0].".2.sh");
foreach $line(@lines)
  {
    chomp($line);
    #$line =~ s/.* (.*)$/$1/;
    $target = $line;
    $target =~ s/[_-]checked.*//;
    $target =~ s/\.*checked.*//;
    $target =~ tr/A-Z/a-z/;   
    if ($line =~ /thesis/)
      {
	#internal report
	#$target =~ s/DESY\-//i;
	#$target =~ s/\-(19|20)/\-/;
	#$target =~ s/\-0([0-9][0-9]\.pdf)/-$1/i;
	#fuer die uebersicht
 	#$spires = $target;
	#$spires =~ s/.pdf//i;
	#$spires =~ tr/a-z/A-Z/;
	#$spires = "DESY $spires";
	#print "$spires\n";
	$target = "/afs/desy.de/group/library/preprints/desy/scan/thesis/$target";
      }
    else
	{
	    $target =~ s/DESY *\- *//i;
	    $target =~ s/ *\- *(19|20)/\-/;
	    $target =~ s/ *\- *0([0-9][0-9]\.pdf)/-$1/i;
	    $target =~ s/ //g;
	    #print "$target\n";
	    #fuer die uebersicht
	    $spires = $target;
	    $spires =~ s/.pdf//i;
	    $spires =~ tr/a-z/A-Z/;
	    $spires = "DESY $spires";
	    if  ($target =~ /[a-z]/)
	    {
		#internal report
		printf "%-40s (internal report)\n", $spires;
		$target = "/afs/desy.de/group/library/preprints/desy/scan/int_rep/$target";
	    }
	    else
	    {
		#red report
		$year = $target; $year =~ s/\-.*//;
		if ($year =~ /^(19|20)/)
		{ $target =~ s/^(19|20)//;}
		else
		{
		    if ($year =~ /^[0-2]/) { $year = "20$year"; }
		    else { $year = "19$year"; }
		}
		printf "%-40s (red report)\n", "DESY $target";
		$target = "/afs/desy.de/group/library/preprints/desy/scan/$year/desy$target";
	    }
	}
    $source = $line;
    $source =~ s/ /\\ /g;
    $cpcommand = "mv /afs/desy.de/group/library/preprints/desy/scan/in_progress/$source $target.pdf\n";
    $link = $target;
    $link =~ s/\/scan//;
    $lncommand = "ln -s $target.pdf $link.pdf\n";
    print FIL1 "$cpcommand";
    print FIL2 "$lncommand";
  }
