#!/opt/gnu/bin/perl
# ************************************************************ #
# TO INSTALL - run this perl script, BUT first !!!
#
# Edit the variables declarations at the top
# 
#    BINDIR - where you want the executable to be put.
#    LIBDIR - where you want all the psmulti library files put.
#    MANDIR - where you want the man pages installed
#    PERLEXE - the execution path for Perl
#
#    INSTALL - which install to use
#
# NOTE: these should be full pathnames!
# Any directories that are missing will be created!
#
# ************************************************************ #

# ************************************************************ #
# SCCS: @(#)Install	2.7 5/26/92
# HISTORY:
#       murray - Apr  3, 1992: Created.
#       dna    - Apr 27, 1997: Parametrize the installer
#       dna    - May 17, 2000: uses mkdirhier instead of mkdir
# ************************************************************ #

# ============================================================ #
#   Site Dependent Settings
# ============================================================ #

my $BINDIR = '/opt/bin';
my $LIBDIR = '/opt/lib/psmulti';
my $MANDIR = '/opt/man';
my $PERLEXE = '/opt/gnu/bin/perl';

my $INSTALL = '/usr/ucb/install';

# ============================================================ #
#    Variable Declarations
# ============================================================ #

my $USEAGE = 'Install [-b BinDir] [-l LibDir] [-m ManDir]';

my @Executables = ( 'psmulti', 'psfix' );

my @Library =
  (
    'format-standard.pl', 'format-standard.ps',
    'format-raw.pl', 'format-raw.ps',

    'border-psmulti.ps', 'border-shadow.ps', 'border-pborder.ps',
    'border-mborder.ps', 'border-iborder.ps', 'border-frame.ps',

    'lcvp.ps', 'shadowstring.ps', 'bbox-util.ps', 'tilebox.ps',
    'param-util.ps', 'wrap-ops.ps', 'gstate-util.ps',
    'psmulti-wrapops.ps', 'psmulti-tm.ps',

    'pspp-util.pl', 'bbox-util.pl', 'file-util.pl', 'dsc-util.pl',
    'page-configure.pl',

    'psfix-border.ps', 'psfix-showpage.ps'
  );

my @ManPages = ( 'psmulti.n', 'psfix.n' );

my $OrigLibDir = '/home/murray/PS/psmulti';
my $OrigPerlDir = '/usr/local/bin/perl';

# ============================================================ #
#             Argument Processing
# ============================================================ #

while ( @ARGV ) {
    $_ = shift @ARGV;

    if ( /^-b/ ) {
		$BINDIR = shift @ARGV;
	} elsif ( /^-l/ ) {
		$LIBDIR = shift @ARGV;
	} elsif ( /^-m/ ) {
		$MANDIR = shift @ARGV;
	} else {
		print STDERR "usage: $USEAGE\n"; exit 1;
	}
}

 
if ( ! -d $BINDIR ) {
	print STDERR "Creating Directory: $BINDIR\n"; mkDirHier($BINDIR);
}

if ( ! -d $LIBDIR ) {
	print STDERR "Creating Directory: $LIBDIR\n"; mkDirHier($LIBDIR);
}

if ( ! -d $MANDIR ) {
	print STDERR "Creating Directory: $MANDIR\n"; mkDirHier($MANDIR);
}

# ============================================================ #
#           Install Exectuables
# ============================================================ #

foreach my $File ( @Executables ) {
    if ( -f $File ) {
		open(INPUT, $File);
	} elsif ( -f "$File.pl" ) {
		open(INPUT, "$File.pl");
    } elsif ( -f "SCCS/$File" ) {
		system "sccs get $File";
        system "chmod a+x $File";
        open(INPUT, $File);
	} elsif ( -f "SCCS/s.$File.pl" ) {
		system "sccs get $File.pl";
        system "chmod a+x $File.pl";
        open(INPUT, "$File.pl");
	} else {
		print STDERR "Warning: could not find $File\n"; next;
	}

    open(OUTPUT, ">/tmp/$File");

    while( <INPUT> ) {
		s!$OrigPerlDir!$PERLEXE!ge; 
        s!$OrigLibDir!$LIBDIR!ge;
        print OUTPUT $_;
	}

    close(INPUT); close(OUTPUT);

    system "$INSTALL -m 0755 /tmp/$File $BINDIR";
    unlink("/tmp/$File");
}

# ============================================================ #
#           Install Library Files
# ============================================================ #

foreach my $File (@Library) {
    if ( -f $File ) {
		system "$INSTALL -m 0644 $File $LIBDIR";
	} elsif ( -f "SCCS/s.$File" ) {
		system "sccs get $File";
        system "$INSTALL -m 0644 $File $LIBDIR";
	} else {
		print STDERR "Warning: could not find file $File\n";
	}
}

# ------------------------------------------------------------ #
# And Finally ... The Manual Page
# ------------------------------------------------------------ #

foreach my $File (@ManPages) {
	my $Section = 'n';
	$Section = $1 if $File =~ /[^\.]\.(.)/;

    if ( ! -d "$MANDIR/man$Section" ) {
		print STDERR "Creating Directory: $MANDIR/man$Section\n";
        mkdir("$MANDIR/man$Section",0755);
	}

    if ( -f $File ) {
		system "$INSTALL -m 0644 $File $MANDIR/man$Section\n";
	} elsif ( -f "SCCS/s.$File" ) {
		system "sccs get $File";
        system "$INSTALL -m 0644 $File $MANDIR/man$Section\n";
	} else {
		print STDERR "Warning: Could not find manual page $File\n";
	}
  }


# creates the necessary directories,
sub mkDirHier() {
  local $_;
  my @elem = split(m%/%, $_[0]);
  my $lpath = '';
  foreach (@elem) {
    $lpath .= "$_/";
    if (-e $lpath) {
      die "$lpath exists but is not a directory\n" unless -d _;
    } else {
      mkdir $lpath, 0755 || die "Can't make directory '$lpath': $!\n";
    }
  }
}

