#! /usr/bin/env perl

sub usage {
  print "Usage: makerefereces CLDAQ-version.tar.gz\n";
  print " e.g.) makerefereces CLDAQ-1.6.0.tar.gz\n";
  exit 1;
}

sub argcheck() {
  if ( $#ARGV != 0 ) {
    &usage();
  }

  $gARCHIVE = $ARGV[0];

  if ( ! -e $gARCHIVE || $gARCHIVE !~ /^CLDAQ-([0-9]+\.[0-9]+\.[0-9]+)\.tar\.gz$/ ) {
    &usage();
  } else {
    $ENV{'CLDAQ_INSTALL'} = "./CLDAQ";
    $gVERSION = $1;
  }
  return;
}

sub godoxygen {
  system("tar zxvf $gARCHIVE");
  system("./godoxygen");
  return;
}

sub archive {
  system("rm -rf CLDAQ");
  system("tar zcvf html.tar.gz html");
  system("mv html referecnces");
  system("mv html.tar.gz referecnces/");

  system("gzip latex/*.ps");
  system("mv latex/*.ps.gz referecnces/");

  system("tar zcvf latex.tar.gz latex");
  system("rm -rf latex");
  system("mv latex.tar.gz referecnces/");

  system("tar zcvf man.tar.gz man");
  system("rm -rf man");
  system("mv man.tar.gz referecnces/");

  system("tar zcvf referecnces-$gVERSION.tar.gz referecnces");
  system("rm -rf referecnces");
  return;
}

sub main {
  &argcheck();
  &godoxygen();
  &archive();
}

&main();

1;
