use File::Path qw(make_path remove_tree);
m|^out=(.+)$| && ($outdir=$1,next);
m|^tar=(.+)$| && ($tarfile=$1,next);
m|^tmp=(.+)$| && ($tmpdir=$1,next);
m|^ver=(.*\d+\.\d+\.\d+)-(\d+)$| && ($version = $1, $revision = $2, next);
m|^variant=(.+)$| && ($variant = $1, next);
die "please specify the tar file containing a CASA workspace with 'tar=<FILE>'" unless -f $tarfile;
die "please specify an output directory with 'out=<DIR>'" unless -d $outdir;
die "please specify a temporary directory with 'tmp=<DIR>'" unless -d $tmpdir;
die "please specify the version number, e.g. 'ver=4.6.0-47'" unless $version && $revision;
die "failed to find os version" unless $rhel;
$outdir = abs_path($outdir);
$tmpdir = abs_path($tmpdir);
copy($tarfile,$tmpdir) or die "could not copy workspace tar file to temporary directory";
my $name = basename($tarfile);
$tarfile = "$tmpdir/$name";
print "------- unpacking workspace --------------------------------------------\n";
if ( `file $tarfile` =~ m|gzip compressed data| ) {
my $output = `gunzip -v $tarfile 2>&1`;
if ( $output =~ m|replaced with (\S+)| ) {
die "unzipping of tar file ($tarfile) failed" unless -f $tarfile;
die "gunzip difficulties...";
die "$tarfile is not a tar file" unless `file $tarfile` =~ m|tar archive|;
$builddir = "$topdir/BUILD";
open(UNTAR,"tar -C $builddir -vxf $tarfile 2>&1 |");
die "found no roots" if scalar(@roots) == 0;
die "found multiple roots: @roots" if scalar(@roots) != 1;
$wsdir = "$builddir/$wsroot";
die "could not extract workspace" unless -d $wsdir;
print "------------------------------------------------------------------------\n";
$destroot = "$topdir/BUILD/$wsroot/_preinstall_";
die "could not create build root" unless make_path($destroot);
@cruft = ("code/include/pgxwin.h","code/include/wcsconfig_f77.h");
foreach ( @cruft ) { unlink($_) }
$optdir = "/opt/casa/versions/${version}R";
$optpydir = "$optdir/lib/python2.7";
$rootdir = "$destroot$optdir";
$bindir = "$rootdir/bin";
$libdir = "$rootdir/lib";
$plibdir = "$destroot$optpydir";
$incdir = "$rootdir/include";
$pgplotdir = "$rootdir/pgplot";
$usrbindir = "$destroot/usr/bin";
$usrbindir = "$destroot/usr/bin";
$optbindir = "$destroot/opt/casa/bin";