#!/usr/local/bin/perl 
#
#      $Id: chown-root,v 1.2 1998-07-31 22:41:28 haley Exp $
#
#########################################################################
#									#
#			   Copyright (C)  1993				#
#	     University Corporation for Atmospheric Research		#
#			   All Rights Reserved				#
#									#
#########################################################################
#
#	File:		
#
#	Author:		John Clyne
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#	Date:		Fri Jul 9 11:20:49 MDT 1993
#
#	Description:	chown-root changes the ownership of the contents of 
#			all of the .TAR files in the /ncargbin directory to
#			root, and changes the group to daemon. This script
#			should be run after 'make all install' is executed
#			in the $NCARG/install/install directory and *before*
#			any tarfile, tape, cd-roms, etc are generated from
#			the /ncargbin directory.
#
#	Usage:		make-cdrom 	[-debug] 
#					[-root <directory>]
#
#	Environment:
#
#	Files:		
#
#	Options:	-debug			: print commands and exit.
#			-root <directory>	: root of ncarg build directory
#						  default is /ncargbin
#
#	globals
#
#
#
#
#
#
#
$doDebug = 0;

sub     cleanup {
        local ($ec) = @_;

	do my_system("/bin/rm -fr $scratch_dir");

	exit $ec;
}


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

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

sub     sigint_handler {
        local ($sig) = @_;

        print STDOUT "Caught a SIG$sig -- shutting down\n";
	
	do cleanup(1);
}


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


$build_root = "/fs/scd/home1/X/ncarg/binaries";	
$scratch_dir = "/tmp/chown-root.$$";	# scratch directory.
$usage = "[-root <path>] [-debug]";


#
#	create a scratch directory
#
mkdir("$scratch_dir", 0777);	# don't check err code in case dir already exist
if (! -d $scratch_dir || ! -w $scratch_dir) {
	print STDERR "Can't create directory ($scratch_dir)\n";
	exit 1;
}

#
#       create a scratch directory
#
mkdir("$scratch_dir", 0777);    # don't check err code in case dir already exist
if (! -d $scratch_dir || ! -w $scratch_dir) {
	print STDERR "Can't create directory ($scratch_dir)\n";
	exit 1;
}


#
# register a signal handler to cleanup the scratch directory
#
$SIG{'INT'} = 'sigint_handler';


while ($ARGV[0] =~ /-/) {
        $_ = shift @ARGV;

        if (/-debug/) {
                $doDebug = 1;
        }
	elsif (/-root/) {
		$build_root = shift @ARGV;
	}
	else {
		print STDERR "Usage: $usage\n";
		do cleanup(1);
	}
}


if (@ARGV) {
        print STDERR "Usage: $usage\n";
	do cleanup(1);
}

#
#	Must run as root 
#
$_ = `whoami`;
chop;
if ($_ ne "root" && ! $doDebug) {
	print STDERR "Permission denied: must be root\n";
	do cleanup(1);
}

chdir "$scratch_dir" || die "Can't cd to $scratch_dir: $!\n";

open(FIND, "cd $build_root; find . -name '*.TAR' -print |") || die "find failed $!\n";
while ($tarfile = <FIND>) {
	chop $tarfile;

	$tarfile = "$build_root" . "/" . $tarfile;

	#
	# scrub the scratch directory
	#
	$cmd = "/bin/rm -fr $scratch_dir/*";
	do my_system($cmd);

	$cmd = "/bin/tar -xbf 20 $tarfile";
	do my_system($cmd);

	#
	# change owner of tar files contents to root and change group
	# to daemon
	#
	$cmd = "find . -exec chown root {} \\; -exec chgrp daemon {} \\;";
	do my_system($cmd);

	$cmd = "tar -cbf 20 $tarfile .";
	do my_system($cmd);
}

do cleanup(0);

exit 0;
