#!/usr/bin/perl -w
#<mkenvdir: populate a directory with files holding your environment.

use Getopt::Long;
use Pod::Usage;
use File::Basename;
use Carp;
use strict;
use diagnostics;

use subs qw(manpage myuuid usage version where);
$ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin /opt/sfw/bin);

my $myname = basename($0);
$myname =~ s/\.\w*$//;   # strip any extension

my %options;
my @getopt_args = (
    'h|?',    # print usage
    'm',      # print manpage
    'u',      # print UUID
    'v',      # print version
    'w',      # print source location
    );

Getopt::Long::config("noignorecase", "bundling");
usage unless GetOptions(\%options, @getopt_args);

manpage if $options{'m'};
myuuid  if $options{'u'};
version if $options{'v'};
where   if $options{'w'};
usage   if $options{'h'};

# Sanity checks.

my $dir = shift or die "no output directory specified\n";
-d $dir         or die "$dir: not a directory\n";
chdir($dir)     or die "$dir: cannot cd\n";

# Ignore variables that should be set by something else, like a shell.
# HOME is an exception; it should be set properly when you login, but if
# you're doing some weird test, it might be useful to set by hand.

my $file;
my $fh;

foreach (sort keys %ENV) {
    next if /_|LOGNAME|USER|PWD/;
    $file = $_;

    if (-f $file) {
        warn "$file exists, skipping.";
    }
    else {
        open($fh, "> $file") or die "$file: cannot open: $!\n";
        print $fh $ENV{$_}, "\n";
        close($fh);
    }
}

exit(0);

#---------------------------------------------------------------------
# Print a usage message from the comments and exit.

sub usage {
    my ($emsg) = @_;
    use Pod::Usage qw(pod2usage);
    warn "$emsg\n" if defined $emsg;
    pod2usage(-verbose => 99, -sections => "NAME|SYNOPSIS|OPTIONS");
}

sub manpage {
    my @args = ("perldoc", "$0");
    exec { $args[0] } @args;          # safe even with one-arg list
    die("should not get here\n");
}

#---------------------------------------------------------------------
# Print the UUID, current version, or source location.

sub myuuid {
    my $UUID = $1
        if q$UUID: 1d48d3fd-893d-341c-b62f-3880dc81a45b $ =~ /UUID: (.*) /;
    print "$UUID\n";
    exit(0);
}

sub version {
    my $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
    my $DATE = $1 if q$Date: 2012-07-26 17:11:48-04 $ =~ /Date: (.*) /;
    print "$myname $VERSION $DATE\n";
    exit(0);
}

sub where {
    my $SOURCE = $1
        if q$Source: /home/vogelke/bin/RCS/mkenvdir,v $ =~
          /Source: (.*) /;
    my $HOST = $1 if q$Host: sys7.com $ =~ /Host: (.*) /;
    print "file://$HOST", "$SOURCE\n";
    exit(0);
}

#---------------------------------------------------------------------
__END__

=head1 NAME

mkenvdir - populate a directory with files holding your environment

=head1 SYNOPSIS

mkenvdir [-hmuvw] dir

=head1 OPTIONS

=over 4

=item B<-h>

Print a brief help message and exit.

=item B<-m>

Print the manual page and exit.

=item B<-u>

Print the script UUID and exit.

=item B<-v>

Print the version and exit.

=item B<-w>

Print the source location and exit.

=back

=head1 DESCRIPTION

B<mkenvdir> accepts a directory on the command line and writes files
holding environment information.  Intended for use with DJB's "envdir" program.

=head1 EXAMPLE

This starts a shell with an environment ONLY set from $HOME/.env files.
Each file under $HOME/.env is named after a given environment variable,
and the first line holds the desired setting for that variable:

  me% ls $HOME/.env
  PATH  TMPDIR
 
  me% head *
  ==> PATH <==
  /usr/local/bin:/bin:/sbin:/usr/sbin:/usr/bin
 
  ==> TMPDIR <==
  /tmp
 
  me% env - /usr/local/bin/envdir $HOME/.env /bin/sh
  me$ env
  PATH=/usr/local/bin:/bin:/sbin:/usr/sbin:/usr/bin
  TMPDIR=/tmp

=head1 AUTHOR

 Karl Vogel <vogelke+unix@pobox.com>
 Array Information Technology

=cut
