#!/usr/bin/env perl

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

############################################################################
# RCS INFORMATION:
#
# 	$RCSfile: make_distrib,v $
# 	$Author: johns $	$Locker:  $		$State: Exp $
#	$Revision: 1.87 $	$Date: 2016/10/21 23:14:09 $
#
############################################################################
# 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|AIX4|AIX5|AIX5_64|AIX6_64|IRIX6|IRIX6_64|LINUX|LINUXALPHA|LINUXIA64|LINUXPPC|LINUXPPC64|MACOSX|HPUX10|HPUX11|SOLARIS2|SOLARIS2_64|SOLARISX86|SOLARISX86_64>
#   "source" indicates this will be the source distribution
#   otherwise this will be the given binary distribution
# To make this install elsewhere, change "$distrib_dir"


@archlist=('AIX4', 'AIX5', 'AIX5_64', 'AIX6_64', 'ANDROIDARMV7A', 'BLUEWATERS', 'CRAY_XC', 'CRAY_XK', 'FREEBSD', 'HPUX11', 'IRIX6', 'IRIX6_64', 'LINUX', 'LINUXALPHA', 'LINUXAMD64', 'LINUXIA64', 'LINUXCARMA', 'LINUXPPC', 'LINUXPPC64', 'MACOSX', 'MACOSXX86', 'MACOSXX86_64', 'OPENPOWER', 'SOLARIS2', 'SOLARIS2_64', 'SOLARISX86', 'SOLARISX86_64', 'SUMMIT', 'TRU64', 'WIN32');
# @graphlist=('gl', 'opengl', 'mesa', 'text');

$| = 1; # flush after every print
$method = '';
if ($#ARGV == 0 || $#ARGV == 1) {
    if ($ARGV[0] eq 'source') {$method = 'source'};
    if ($ARGV[0] eq 'AIX4' ||
	$ARGV[0] eq 'AIX5' ||
	$ARGV[0] eq 'AIX5_64' ||
	$ARGV[0] eq 'AIX6_64' ||
	$ARGV[0] eq 'ANDROIDARMV7A' ||
	$ARGV[0] eq 'BLUEWATERS' ||
	$ARGV[0] eq 'CRAY_XC' ||
	$ARGV[0] eq 'CRAY_XK' ||
	$ARGV[0] eq 'FREEBSD' ||
	$ARGV[0] eq 'HPUX11' ||
	$ARGV[0] eq 'IRIX6' ||
	$ARGV[0] eq 'IRIX6_64' ||
	$ARGV[0] eq 'LINUX' ||
	$ARGV[0] eq 'LINUXALPHA' ||
	$ARGV[0] eq 'LINUXAMD64' ||
	$ARGV[0] eq 'LINUXIA64' ||
	$ARGV[0] eq 'LINUXCARMA' ||
	$ARGV[0] eq 'LINUXPPC' ||
	$ARGV[0] eq 'LINUXPPC64' ||
	$ARGV[0] eq 'MACOSX' ||
	$ARGV[0] eq 'MACOSXX86' ||
	$ARGV[0] eq 'MACOSXX86_64' ||
	$ARGV[0] eq 'OPENPOWER' ||
	$ARGV[0] eq 'SOLARIS2' ||
	$ARGV[0] eq 'SOLARIS2_64' ||
	$ARGV[0] eq 'SOLARISX86' ||
	$ARGV[0] eq 'SOLARISX86_64' ||
	$ARGV[0] eq 'SUMMIT' ||
	$ARGV[0] eq 'TRU64' ||
	$ARGV[0] eq 'WIN32') {
	$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 HPUX10 distribution ...\n";
	print `./make_distrib HPUX10 > /dev/tty`;
	if ($?) { die "Can't make_distrib HPUX10"; }
	print "Making HPUX11 distribution ...\n";
	print `./make_distrib HPUX11 > /dev/tty`;
	if ($?) { die "Can't make_distrib HPUX11"; }
	print "Making IRIX6 opengl distribution ...\n";
	print `./make_distrib IRIX6 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib IRIX6 opengl"; }
	print "Making IRIX6_64 opengl distribution ...\n";
	print `./make_distrib IRIX6_64 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib IRIX6_64 opengl"; }
	print "Making LINUX distribution ...\n";
	print `./make_distrib LINUX > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUX"; }
	print "Making LINUXALPHA distribution ...\n";
	print `./make_distrib LINUXALPHA > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXALPHA"; }
	print "Making LINUXAMD64 distribution ...\n";
	print `./make_distrib LINUXAMD64 > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXAMD64"; }
	print "Making LINUXIA64 distribution ...\n";
	print `./make_distrib LINUXIA64 > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXIA64"; }
	print "Making LINUXCARMA distribution ...\n";
	print `./make_distrib LINUXCARMA > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXCARMA"; }
	print "Making LINUXPPC distribution ...\n";
	print `./make_distrib LINUXPPC > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXPPC"; }
	print "Making LINUXPPC64 distribution ...\n";
	print `./make_distrib LINUXPPC64 > /dev/tty`;
	if ($?) { die "Can't make_distrib LINUXPPC64"; }
	print "Making SOLARIS2 opengl distribution ...\n";
	print `./make_distrib SOLARIS2 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib SOLARIS2 opengl"; }
	print "Making SOLARIS2 mesa distribution ...\n";
	print `./make_distrib SOLARIS2 mesa > /dev/tty`;
	if ($?) { die "Can't make_distrib SOLARIS2 mesa"; }
	print "Making SOLARIS2_64 opengl distribution ...\n";
	print `./make_distrib SOLARIS2_64 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib SOLARIS2_64 opengl"; }
	print "Making SOLARIS2_64 mesa distribution ...\n";
	print `./make_distrib SOLARIS2_64 mesa > /dev/tty`;
	if ($?) { die "Can't make_distrib SOLARIS2_64 mesa"; }
	print "Making SOLARISX86 mesa distribution ...\n";
	print `./make_distrib SOLARISX86 mesa > /dev/tty`;
	if ($?) { die "Can't make_distrib SOLARISX86 mesa"; }
	print "Making TRU64 distribution ...\n";
	print `./make_distrib TRU64 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib TRU64 opengl"; }
	print "Making WIN32 distribution ...\n";
	print `./make_distrib WIN32 > /dev/tty`;
	if ($?) { die "Can't make_distrib WIN32"; }
	print "Making WIN32 opengl distribution ...\n";
	print `./make_distrib WIN32 opengl > /dev/tty`;
	if ($?) { die "Can't make_distrib WIN32 opengl"; }
	print "All done.  Good luck on this release!\n";
	exit;
    }
}

if ($method eq '') {
    # for examples: make_distrib IRIX6
    #             : make_distrib IRIX6 OPENGL
    #             : make_distrib IRIX6 text
    #             : make_distrib source
    die "Usage: make_distrib <all|source|architecture name> [graphics]\n";
}


# optionally do executable file suffix handling for Win32 or other builds
if ($#ARGV == 1) {
  if ($ARGV[0] eq 'WIN32') {
    $EXE = '.exe';
  } else {
    $EXE = '';
  }
}

# optionally make distribution for an alternative graphics
$GRAPHICS = '';
if ($#ARGV == 1) {
  if ($ARGV[1] eq 'opengl' ||
      $ARGV[1] eq 'mesa' ||
      $ARGV[1] eq 'text' ||
      $ARGV[1] eq 'gl'
     ) {
    $GRAPHICS = $ARGV[1];
  }
}

# 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";
$distrib_pluginsrc = "$distrib_dir/plugins";

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: $!"; }
  
print "Removing old plugin src distrib directory ($distrib_pluginsrc)\n";
print `rm -rf $distrib_pluginsrc/`;
if ($?) { die "remove error: $!"; }

# if we're doing a source distribution, we need the plugins too
if ($method eq 'source') {
  print "Making new plugin src distrib directory\n";
  mkdir($distrib_pluginsrc, 0755);
  if ($?) { die "mkdir $distrib_pluginsrc: $!"; }

  # copy the plugin source tree
  print "Copying VMD plugin sources\n";
  print `(cd ../plugins; tar -cf - *) | (cd $distrib_pluginsrc; tar -xf - )`;
  if ($?) { die "Couldn't tar: $!"; }
  print `(cd $distrib_pluginsrc; rm -rf compile)`;
  if ($?) { die "Couldn't delete $!"; }
  mkdir("$distrib_pluginsrc/compile", 0755);
  if ($?) { die "mkdir $distrib_pluginsrc/compile $!"; }
}


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

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

# get the right configuration file
if ($method eq 'source') {
  # copy make_distrib script into source distribution.
  print `cp ./Makefile $distrib_vmd/`;
  print `cp ./make_distrib $distrib_vmd/`;
  if ($?) { die "Couldn't copy: $!"; }
} else {
  # Architecture specific options
  print `cp ./configure.options $distrib_vmd/configure.options`;
  if ($?) { die "Couldn't copy: $!"; }
}

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

print "Copying text files\n";
print `cp ./data/.vmdrc ./data/.vmdsensors ./data/vmd_completion.dat $distrib_vmd/data`;
if ($?) { die "Couldn't copy: $!"; }
print `cp ./doc/ig.pdf $distrib_vmd/doc/`;
if ($?) { die "Couldn't copy: $!"; }
print `cp ./doc/ug.pdf $distrib_vmd/doc/`;
if ($?) { die "Couldn't copy: $!"; }

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

print "Copying VMD scripts\n";
print `tar -cf - scripts | (cd $distrib_vmd; tar -xf - )`;
if ($?) { die "Couldn't tar: $!"; }

print "Copying VMD shaders\n";
print `tar -cf - shaders | (cd $distrib_vmd; tar -xf - )`;
if ($?) { die "Couldn't tar: $!"; }


######### copy redistributable runtime libraries
mkdir("$distrib_vmd/lib/redistrib", 0755);
if ($method ne 'source') {
  print "Copying redistributable runtime libraries\n";

  # copy the redistributable libraries to the destination runtime area
  if (-d "./lib/redistrib/lib_$ARCH") {
    print `tar cf - ./lib/redistrib/lib_$ARCH | (cd $distrib_vmd; tar xf -)`;
    if ($?) { die "Couldn't tar: $!"; }
  }
}


######### copy the library directories
@libs = ('Mesa', 'cave', 'fltk', 
	 'points', 'scripts', 'stride', 'surf', 'tachyon');

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 cf - ./lib/$lib | (cd $distrib_vmd; tar xf -)`;
    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 *.cu *.h *.y *.l *.fd *.data
@globs = ('Makefile', 'Makedata.depend', 
	  '*.C', '*.c', '*.cu', '*.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: $!"; }
  }
} 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";
}


############ Copy the Python files
#
if (1) {
    @globs = ('*.py');
    print "Copying Python files\n";
    foreach $g (@globs) {
      print `cp ./python/$g $distrib_vmd/python/`;
      if ($?) { die "Can't copy $g: $!"; }
    }
}

############ Copy the $ARCH and noarch parts of the compiled plugins directory
#
if ($method eq 'binary') {
  print "Copying compiled plugin headers\n";
  print `tar -cf - plugins/include | (cd $distrib_vmd; tar -xf - )`;
  if ($?) { die "Couldn't tar: $!"; }

  print "Copying compiled molfile plugins\n";
  print `tar -cf - plugins/$ARCH/molfile/*.so | (cd $distrib_vmd; tar -xf - )`;
  if ($?) { die "Couldn't tar: $!"; }

  print "Copying compiled Tcl plugins\n";
  print `tar -cf - plugins/$ARCH/tcl | (cd $distrib_vmd; tar -xf - )`;
  if ($?) { die "Couldn't tar: $!"; }

  # short-term fix, really these should be organized in the same way that
  # the multiseq related binaries are
  if (-d "plugins/$ARCH/bin") {
    print "Copying plugin standalone executables\n";
    print `tar -cf - plugins/$ARCH/bin | (cd $distrib_vmd; tar -xf - )`;
    if ($?) { die "Couldn't tar: $!"; }
  }

  print "Copying platform-independent Tcl script plugins\n";
  print `tar -cf - plugins/noarch | (cd $distrib_vmd; tar -xf - )`;
  if ($?) { die "Couldn't tar: $!"; }
}

############ Finally, copy the binary
#
if ($method eq 'binary') {
    print "Copying executable\n";
    mkdir("$distrib_vmd/$ARCH", 0755);
    if ($GRAPHICS eq '') {
      print `cp $ARCH/vmd_$ARCH$EXE $distrib_vmd/$ARCH/`; 
      if ($?) { die "Can't copy binary: $!"; }
    } else {
      print `cp $ARCH/vmd_$ARCH$EXE $distrib_vmd/$ARCH/vmd_$ARCH$EXE`; 
      if ($?) { die "Can't copy binary: $!"; }
    }
    if (-f "$ARCH/OptiXShaders.ptx") {
      print "Copying compiled OptiX PTX shader\n";
      print `cp $ARCH/OptiXShaders.ptx $distrib_vmd/$ARCH/`; 
      if ($?) { print "No OptiX shader found: $!"; }
    } else {
      print "No OptiX PTX shader found, proceeding...\n";
    }
} 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.src.tar";
} else {
  if ($GRAPHICS eq '') {
    $tarname = "vmd-$VERSION.bin.$ARCH.tar";
  } else {
    $tarname = "vmd-$VERSION.bin.$ARCH.$GRAPHICS.tar";
  }
}

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

if ($method eq 'source') {
  print `cd $distrib_dir; tar -cf $tarname vmd-$VERSION plugins`;
} else {
  print `cd $distrib_dir; tar -cf $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";
