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

############################################################################
#cr                                                                       
#cr            (C) Copyright 1995 The Board of Trustees of the            
#cr                        University of Illinois                         
#cr                         All Rights Reserved                           
#cr                                                                       
############################################################################

############################################################################
# RCS INFORMATION:
#
# 	$RCSfile: make_distrib,v $
# 	$Author: dalke $	$Locker: dalke $		$State: Exp $
#	$Revision: 1.2 $	$Date: 1997/03/24 21:42:54 $
#
############################################################################
# DESCRIPTION:
#  I finally gave up on tweaking the configure script for the
# installation so instead this is an ancillary script that
# does it for me.  It is much easier to use, trust me.
#
############################################################################

# Usage: make_distrib <source|IRIX5|LINUX|HPUX9|AIX3> 
#   "source" indicates this will be the source distribution
#   otherwise this will be the given binary distribution
# To make this install elsewhere, change "$distrib_dir"

$| = 1; # flush after every print
$method = '';
if ($#ARGV == 0) {
    if ($ARGV[0] eq 'source') {$method = 'source'};
    if ($ARGV[0] eq 'AIX3' ||
	$ARGV[0] eq 'LINUX' ||
	$ARGV[0] eq 'HPUX9' ||
	$ARGV[0] eq 'IRIX5') {
	$method = 'binary';
	$ARCH = $ARGV[0];
    }
    if ($ARGV[0] eq 'all') {
	# make all the distributions
	print "Making source distribution ...\n";
	print `./make_distrib source > /dev/tty`;
	if ($?) { die "Can't make_distrib source"; }
	print "Making IRIX5 distribution ...\n";
	print `./make_distrib IRIX5 > /dev/tty`;
	if ($?) { die "Can't make_distrib IRIX5"; }
	print "Making LINUX distribution ...\n";
	print `./make_distrib LINUX > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUX"; }
	print "All done.  Good luck on this release!\n";
	exit;
    }
}


if ($method eq '') {
    # for examples: make_distrib IRIX5
    #             : make_distrib source
    die "Usage: make_distrib <all|source|architecture name>\n";
}

# make sure I'm in the right directory (needs "configure" and
# "src/config.h")

if (! -f './configure' || ! -f "src/config.h") {
    die "You don't appear to be in the top of the vmd file hierarchy\n";
}

# scarf up the version from src/config.h
open(INFILE, "<src/config.h");
while (<INFILE>) {
    @terms = split;
    next if $#terms < 2;
    $terms[2] =~ s/\"//g;  # get rid of the double quotes
    $VERSION = $terms[2] if ($terms[1] eq 'VMDVERSION');
}
close(INFILE);

die "Can't get VERSION info from src/config.h" unless defined($VERSION);
if (!defined($ARCH)) {
    $ARCH = "source";  # needed to get the optionally correct .depend file
}

######## remove the whole distrib hierarchy
$distrib_dir = './distrib';
$distrib_vmd = "$distrib_dir/vmd-$VERSION";

print "Removing old distrib directory ($distrib_vmd)\n";
print `rm -rf $distrib_vmd/`;
if ($?) { die "remove error: $!"; }

print "Making new distrib directory\n";
mkdir($distrib_vmd, 0755);
if ($?) { die "mkdir $distrib_vmd: $!"; }

######## make the top-level of the vmd distribution
print "Making distribution directory hierarchy\n";
mkdir("$distrib_vmd/bin", 0755);
mkdir("$distrib_vmd/doc", 0755);
mkdir("$distrib_vmd/lib", 0755);
mkdir("$distrib_vmd/proteins", 0755);
mkdir("$distrib_vmd/src", 0755);

######## Copy the main information files
print "Copying toplevel files\n";
print `cp ./Announcement ./FEEDBACK README configure.options configure make_distrib $distrib_vmd/`;
if ($?) { die "Couldn't copy: $!"; }

# get the right configuration file
if ($method eq 'source') {
    # Get the standard options
    print `cp ./configure.options.HPUX9 ./configure.options.IRIX5 ./configure.options.LINUX ./configure.options $distrib_vmd/`;
    if ($?) { die "Couldn't copy: $!"; }
} else {
    # Architecture specific options
    print `cp ./configure.options.$ARCH $distrib_vmd/configure.options`;
    if ($?) { die "Couldn't copy: $!"; }
}

# Need to strip out the parameters from the configure script
print `awk 'flg==1{print}; /STARTPARAMS/ {flg=1} /ENDPARAMS/ {exit}' < $distrib_vmd/configure > $distrib_vmd/configure.parameters`;
if ($?) { die "Couldn't create configure.parameters: $!"; }


####### get the method independent directories
print "Copying vmd startup script\n";
print `cp ./bin/vmd $distrib_vmd/bin/`;
if ($?) { die "Couldn't copy: $!"; }

print "Copying text files\n";
print `cp ./data/vmd_help.html $distrib_vmd/doc/`;
if ($?) { die "Couldn't copy: $!"; }
print `cp ./data/.vmdrc ./data/.vmd_init ./data/.tracker $distrib_vmd/`;
if ($?) { die "Couldn't copy: $!"; }
print `cp ./doc/ig.ps $distrib_vmd/doc/`;
if ($?) { die "Couldn't copy: $!"; }

print "Copying proteins\n";
print `cp ./proteins/* $distrib_vmd/proteins/`;
if ($?) { die "Couldn't copy: $!"; }


######### copy the library directories
@libs = ('LASSP', 'Mesa', 'alpha', 'cave', 'libg++', 'mdcomm',
	 'points', 'scripts', 'stride', 'surf', 'unc', 'xforms');

print "Redoing library distribution\n";
if ($method eq 'source') {
    # get the source version of the libraries
    print `cd ./lib; ./use source`; 
    if ($?) { die "making library source distribution: $!"; }
} else {
    # get the binary version of the libraries
    print `cd ./lib; ./use $ARCH`; 
    if ($?) { die "making library binary distribution: $!"; }
}
print `cp ./lib/use $distrib_vmd/lib/`;
if ($?) { die "missing 'use' script?"; }

# Now copy each of the directories using tar (to preserve symlinks)
foreach $lib (@libs) {
    print "Copying $lib\n";
    print `tar cBf - ./lib/$lib | (cd $distrib_vmd; tar xBf -)`;
    if ($?) { die "Can't make lib $lib distribution: $!"; }
}

#### revert to local library distribution
print `cd ./lib; use local`;
if ($?) { die "Not able to make local?: $!"; }

########### copy the source if needed
# Basically get the files:
#      Makefile Makedata.depend *.C *.c *.h *.y *.l *.data
# with the special provisio that everything after the REVISION HISTORY
# line is deleted (we don't want to air all our dirty underwear as
# Bill would say).
@globs = ('Makefile', 'Makedata.depend',  '*.C', '*.c', '*.h', '*.y',
	  '*.l', '*.fd', '*.data');

if ($method eq 'source') {
    print "Copying source files\n";
    foreach $g (@globs) {
	print `cp ./src/$g $distrib_vmd/src/`; 
	if ($?) { die "Can't copy $g: $!"; }
    }
    # architecture dependent
    if (-f "./src/Makedata.depend.$ARCH") {
print `cp ./src/Makedata.depend.$ARCH" $distrib_vmd/src/Makedata.depend`;
	if ($?) { die "Can't copy $g: $!"; }
    }
    # strip out the comments
    opendir(DIR, "$distrib_vmd/src/");
    foreach $file (readdir(DIR)) {
	next if ($file eq ".");
	next if ($file eq "..");
	print "stripping $file\n";
	print `awk '/REVISION HISTORY/ {flg=1;}  flg==0;' < $distrib_vmd/src/$file > $distrib_vmd/src/$file.bak`;
	if ($?) { die "Can't strip comments from $file: $!"; }
	print `rm -f $distrib_vmd/src/$file`;
	print `mv $distrib_vmd/src/$file.bak $distrib_vmd/src/$file`;
    }

} else {
    # Makefile needs this for the install
    # architecture dependent
    if (-f "./src/Makedata.depend.$ARCH") {
print `cp ./src/Makedata.depend.$ARCH $distrib_vmd/src/Makedata.depend`;
	if ($?) { die "Can't copy Makedata.depend.$ARCH: $!"; }
    } else {
	print `touch $distrib_vmd/src/Makedata.depend`;
    }
    print "Not copying source files\n";
}


############ Finally, copy the binary
#
if ($method eq 'binary') {
    print "Copying executable\n";
    mkdir("$distrib_vmd/$ARCH", 0755);
    print `cp $ARCH/vmd_$ARCH $distrib_vmd/$ARCH/`; 
    if ($?) { die "Can't copy binary: $!"; }
} else {
    print "Not copying executable\n";
}
    

################## Now that the directory is set up, tar and gzip it
print "Making tar file\n";
if ($method eq 'source') {
    $tarname = "vmd-$VERSION.all.tar";
} else {
    $tarname = "vmd-$VERSION.bin.$ARCH.tar";
}

# remove old copies
print `rm -f $distrib_dir/$tarname $distrib_dir/$tarname.gz`;
if ($?) { die "Problem with tarname removal: $!"; }

print `cd $distrib_dir; tar -cBf $tarname vmd-$VERSION`;
if ($?) { die "Can't tar: $!"; }

print "Compressing tar file\n";
print `cd $distrib_dir; gzip $tarname`; 
if ($?) { die "Can't compress: $!"; }

print "Made $distrib_dir/$tarname.gz\n";
