#!/usr/local/bin/perl 
#
#      $Id: make-tarfile,v 1.10 1996-08-21 15:00:31 haley Exp $
#
#########################################################################
#                                                                       #
#              Copyright (C)  1992                                      #
#        University Corporation for Atmospheric Research                #
#              All Rights Reserved                                      #
#                                                                       #
#########################################################################
#
#   File:       make-tarfile
#
#   Author:     John Clyne
#           National Center for Atmospheric Research
#           PO 3000, Boulder, Colorado
#
#   Date:       Wed Dec 9 15:46:42 MST 1992
#
#   Description:    Write an NCAR G binary distribution tarfile.
#
#   Usage:      make-tarfile -<platform> directory
#
#   Environment:
#
#   Files:      
#       $build_root     : root of ncarg build directory 
#                   (currently /fs/scd/home1/ncargd/dist/binaries)
#
#       $build_root/INSTALL/INSTALL.TAR : install script
#
#       $build_root/$platform/{BIN,INC,LIB,MAN}.TAR : distribution tar files
#
#   Options:    
#

#
#   execute a command via system(). If $doDebug is set just print the 
#   command
#
#$doDebug = 1;
sub my_system {
    local ($cmd) = @_;

    if ($doDebug) {
        print STDERR "$cmd\n";
    }
    else {
        system ($cmd);
        if ($? != 0) {
            print "\"$cmd\" exited with error\n";
            exit 1;
        }
    }
}


##
##
##  M A I N
##
##

$do_inter = 0;          # do interactive mode?
$build_root = "/fs/scd/home1/ncargd/dist/binaries";  
$install_dir = "INSTALL";
$toc_file = "$build_root/INSTALL/var/TOC";
@platforms;             # platforms to write binaries for
$usage = "-<platform> directory";
$version;           # ncarg version
$tarfile;           # path to tarfile we'll create.


#
# Tar files we write for each requested platform
# N.B. order is important
#
@tars = ("BIN.TAR", "INCLUDE.TAR", "LIB.TAR", "MAN.TAR");   


if (@ARGV != 2) {
        print STDERR "Usage: make-tarfile $usage\nn";
    exit 1;
}

$platform = $ARGV[0];
$platform =~ s/^-//;
$directory = $ARGV[1];

$cmd = "cat $build_root/$platform/version";
$version = `$cmd`;
if ($? != 0) {
    print STDERR "\"$cmd\" exited with error : $!\n";
    exit 1;
}
chop $version;
$tarfile = "$directory/ncarg$version.$platform.tar";

print "platform=$platform, build_root=$build_root, tarfile=$tarfile\n";

#
#   generate the tar file with the install script
#
    
chdir $build_root;

$tars = "$install_dir";
foreach $tar (@tars) {
    $tars = "$tars $platform/$tar";
}
$cmd = "tar cbf 20 $tarfile $tars";
do my_system("$cmd");

exit 0;
