#!/usr/local/bin/perl

#
# rc - Manage centralized rc files
#
#   This software is Public Domain. Please maintain version history.
#   Report all bugs to Greg Ercolano (erco@3dsite.com).
#
#   03/17/98 1.00 erco@3dsite.com Initial implementation
#   xx/xx/xx -    -               -
#

use strict;

%G::rcfiles = 
(
    "prman3.5"   => "Pixar RenderMan 3.5",
    "prman3.6"   => "Pixar RenderMan 3.6",
    "prman3.7"   => "Pixar RenderMan 3.7",
    "soft3.7"    => "Softimage 3.7",
    "soft3.7sp1" => "Softimage 3.7 Service Pack #1 updates",
    "cshrc"      => "Standard Cshrc",
    "login"      => "Standard Login",
    "prompt"     => "Standard Prompt",
);

# WHERE THE RC FILES LIVE
$G::dir = "/usr/local/stdrc";

$G::verbose = 0;
$G::debug   = 0;

# MAIN
{
    my $arg;

    while ( $#ARGV >= 0)
    {
	$arg = $ARGV[0];
	shift(@ARGV);

	# HANDLE KEYS
	if ( defined ( $G::rcfiles{$arg} ) )
	{
	    my $cmd = "";
	    if ( $G::verbose )
		{ $cmd = "if (\$?prompt) echo Sourcing $G::dir/$arg;" }
	    $cmd .= "source $G::dir/$arg;";
	    if ( $G::debug ) { print STDERR "$cmd\n"; }
	    print "$cmd\n";
	    next;
	}

	# LIST
	if ( $arg =~ "^-l" )
	{
	    if ( $#ARGV >= 0 )
	    {
		# PRINT MATCHING ITEMS
		foreach $arg ( @ARGV )
		{
		    printf(STDERR "$arg\n%s\n", "-"x40);
		    foreach ( sort ( keys ( %G::rcfiles ) ) )
		    {
			if ( $_ =~ /$arg/ || $G::rcfiles{$_} =~ /$arg/ )
			{
			    printf(STDERR "%-15s - %s\n", $_, $G::rcfiles{$_});
			}
		    }
		    print STDERR "\n";
		}
	    }
	    else
	    {
		# PRINT ENTIRE LIST
		foreach ( sort ( keys ( %G::rcfiles ) ) )
		{
		    printf(STDERR "%-15s - %s\n", $_, $G::rcfiles{$_});
		}
	    }
	    exit(0);
	}

	if ( $arg =~ "^-c" )
	{
	    my $notfound = ""; 
	    my $filename; 
	    foreach ( sort ( keys ( %G::rcfiles ) ) )
	    {
		$filename = "$G::dir/$_";
		if ( -e $filename )
		    { print STDERR "OK: $filename\n"; }
		else
		    { $notfound .= "    $filename\n"; }
	    }
	    if ( $notfound ne "" )
	    {
		print "\nNOT FOUND:\n$notfound\n";
		exit(1);
	    }
	    next;
	}

	if ( $arg =~ "^-d" ) { $G::debug   = 1; next; }
	if ( $arg =~ "^-v" ) { $G::verbose = 1; next; }
	if ( $arg =~ "^-h" ) { Help(); next; }
	print STDERR "rc: '$arg' unknown (ignoring)\n";
    }
    exit(0);
}

sub Help()
{
    print STDERR <<"EOF";

soft - Manage software environments

USAGE:
    soft [args] [key ..]

ARGUMENTS:
    -l	- list software keys
    -v	- verbose messages during environment loading
    -c	- check that all dot files exist


EXAMPLES:
    % rc -l				# list all files
    cshrc    - Site cshrc file
    login    - Site specific login file
    prman3.6 - Prman Version 3.6
    prman3.7 - Prman Version 3.7

    % rc -l prman			# list only prman related files
    prman3.6 - Prman Version 3.6
    prman3.7 - Prman Version 3.7

    % rc cshrc login			# sources cshrc/login silently

    % rc -v cshrc login			# verbose sourcing
    Sourcing /usr/local/stdrc/.cshrc
    Sourcing /usr/local/stdrc/.login

    % rc -d cshrc login			# debug the 'source' commands

IMPLEMENTATION NOTES
    Follow these instructions to put 'rc' to its best use:

    1) Put rc(1) in /usr/local/bin. 

    2) Put the following in /etc/cshrc:

	  set path = ( /usr/local/bin \$path )
	  alias rc 'eval `\\rc \\!*`'

    3) Put your 'global dot files' in /usr/local/stdrc/*

    4) Put the following in the user's cshrc and login files:

	 # USER .CSHRC
	 rc -v cshrc prompt prman3.6 soft3.7sp1

	 # USER .LOGIN FILE
	 rc -v login

    5) Periodically do a sanity check, to make sure dot files exist:

	rc -c

EOF
}
