#!/usr/local/bin/perl -w
$runzip = 1;

$junk = " \\*bak \\*BAK \\*\\~";

$zip_dest_dir = "zipped";

sub zbin
{
   my ($host) = @_;
   $zipfile = "$zip_dest_dir/bin$host.zip";
   if (-f $zipfile) {unlink $zipfile;}
   $scriptdir = 'codas3/bin/csh';
   if ($host eq 'dj' or $host eq 'pc' or $host eq 'mgw' or $host eq 'cyg')
   {
      $scriptdir = 'codas3/bin/bat';
   }
   $bindir = "codas3/bin/$host";
   $cntdir = "codas3/cntfiles";
   $docdir = "codas3/doc codas3/adcp/doc";

   $cmd = "zip -ry $zipfile $bindir $scriptdir $cntdir $docdir".
          " -x $junk";
   &zipit($cmd);

   $cmd = "zip -rugy $zipfile codas3/adcp/demo".
            "  -i \\*.m \\*.m__ \\*.cnt \\*.def".
            " -x \\*/old/\\* ";
   &zipit($cmd);

   $cmd = "zip -rugy $zipfile codas3/bin codas3/adcp/demo".
            "  -i \\*.prl \\*.pl \\*.pm";
   &zipit($cmd);

   $cmd = "zip -rugy $zipfile codas3/vecplot/mapfiles".
            "  -x $junk";
   &zipit($cmd);

}

sub zsource
{
   $zipfile = "$zip_dest_dir/source.zip";
   if (-f $zipfile) {unlink $zipfile;}

   $cmd = "zip -ry $zipfile codas3/dbsource codas3/grid codas3/include codas3/ioserv codas3/make".
          " codas3/matlab codas3/ocean codas3/profstat codas3/use_db codas3/vecplot codas3/util codas3/vector codas3/vstat".
          " -x $junk";

   &zipit($cmd);

   $cmd = "zip -rugy $zipfile codas3/adcp codas3/ctd $docdir".
            " -x \\*demo\\* \\*/doc/\\* \\*.o \\*.obj $junk";
   &zipit($cmd);
}

sub zdemo
{
   $zipfile = "$zip_dest_dir/demo.zip";
   if (-f $zipfile) {unlink $zipfile;}
   $cmd = "zip -ry $zipfile codas3/adcp/demo codas3/ctd/demo".
          " -x $junk";

   &zipit($cmd);

}


sub ztdemo
{
   $zipfile = "$zip_dest_dir/tdemo.zip";
   if (-f $zipfile) {unlink $zipfile;}
   $cmd = "zip -ry $zipfile codas3/adcp/tbbdemo codas3/adcp/tnbdemo".
           " -x $junk";

   &zipit($cmd);

}

sub zcmat
{
   $zipfile = "$zip_dest_dir/cmat.zip";
   if (-f $zipfile) {unlink $zipfile;}
   $cmd = "zip -ry $zipfile matlab/codas3 matlab/misc matlab/ctd".
          " matlab/mex4 matlab/mex5".
           " -x $junk";

   &zipit($cmd);

   $cmd = "zip -ugy $zipfile matlab/adcppath.m matlab/dirnotes.doc";

   &zipit($cmd);

}


sub zipit
{
   my ($cmd) = @_;
   print "$cmd \n";

   if ($runzip)
   {
      system($cmd);
   }


}

# &zbin('sgi');


%cmds = ( 'binsgi' , '&zbin("sgi")',
          'binsol' , '&zbin("sol")',
          'bindj'  , '&zbin("dj")',
          'binlnx' , '&zbin("lnx")',
          'binmgw' , '&zbin("mgw")',
          'bincyg' , '&zbin("cyg")',
          'source' , '&zsource',
          'demo'   , '&zdemo',
          'tdemo'  , '&ztdemo',
          'cmat'   , '&zcmat'
          );


@targets = keys(%cmds);

if (@ARGV eq 0) { &usage; exit(-1); }

if ($ARGV[0] eq 'all') { @tlist = @targets; }
else
{
   foreach $t (@ARGV)
   {
      if (not defined($cmds{$t})) { die "$t is not an available target\n";}
      push(@tlist, $t);
   }
}
foreach $t (@tlist)
{
   eval("$cmds{$t}");
}


sub usage
{
   $target_list = join("\n\t\t",sort(keys(%cmds)));
   print "\n Usage: zipcod.prl target <target...>";
   print "\n\n\t where target is either \"all\" or";
   print "\n\t   one or more of the following:";
   print "\n\n\t\t$target_list\n";
   print "\n zipped files will land in directory called \"$zip_dest_dir\"\n\n";
}
