#!/usr/bin/perl -w # # $Id: buildenv,v 1.6 2008/11/02 23:56:06 vogelke Exp $ # # NAME: # buildenv # # SYNOPSIS: # buildenv [-f file] [-v] # # DESCRIPTION: # "buildenv" reads the $HOME/.envrc file, and creates # equivalent Bourne-shell and C-shell files for setting # environment variables. # # OPTIONS: # "-f file" reads settings from "file" instead of "$HOME/.envrc". # "-v" prints the current version and exits. # # EXAMPLE: # me% cat in # # Pager # PAGER "less" # LESS "-Cden" # # Printer # PRINTER bldg25a # # X-windows # HOST "`uname -n`" # DISPLAY "$HOST:0.0" # KDEDIR "/usr/local/kde" # # Misc # SHELL "/bin/zsh" # TMPDIR "/tmp" # # me% buildenv -f in # Rebuilding in.sh ... # Rebuilding in.csh ... # # me% cat in.sh # # Pager # PAGER="less"; export PAGER # LESS="-Cden"; export LESS # # Printer # PRINTER=bldg25a; export PRINTER # # X-windows # HOST="`uname -n`"; export HOST # DISPLAY="$HOST:0.0"; export DISPLAY # KDEDIR="/usr/local/kde"; export KDEDIR # # Misc # SHELL="/bin/zsh"; export SHELL # TMPDIR="/tmp"; export TMPDIR # # me% cat in.csh # # Pager # setenv PAGER "less" # setenv LESS "-Cden" # # Printer # setenv PRINTER bldg25a # # X-windows # setenv HOST "`uname -n`" # setenv DISPLAY "$HOST:0.0" # setenv KDEDIR "/usr/local/kde" # # Misc # setenv SHELL "/bin/zsh" # setenv TMPDIR "/tmp" # # AUTHOR: # Karl Vogel # Sumaria Systems, Inc. use strict; use subs qw/older usage version/; use Getopt::Std; use vars qw/$opt_f $opt_v/; $ENV{"PATH"} = "/bin:/usr/sbin:/usr/local/bin"; my $bourne; # output file holding Bourne-shell env variables. my $csh; # output file holding C-shell env variables. my $first; # first word in env setting line; env variable name. my $myname; # script basename. my $rc; # input file holding env variable settings. my @line; # remaining words in env setting line; env value. #--------------------------------------------------------------------- # Handle command line arguments (if any). ($myname = $0) =~ s!.*/!!; usage() unless getopts("f:v"); version() if $opt_v; $rc = $ENV{"HOME"} . "/.envrc"; $rc = $opt_f if $opt_f; # # Read settings file, write Bourne- and C-shell equivalents. # $bourne = $rc . ".sh"; $csh = $rc . ".csh"; my ($ifh, $ofh); if (older($bourne, $rc)) { print "Rebuilding $bourne ...\n"; open($ifh, "$rc") || die "$rc: not found: $!\n"; open($ofh, "> $bourne") || die "$bourne: can't write: $!\n"; while (<$ifh>) { chomp; if (/^#|^[ \t]*$/) { print $ofh "$_\n"; } else { @line = split; $first = shift(@line); print $ofh "$first=@line; export $first\n"; } } close($ifh); close($ofh); } if (older($csh, $rc)) { print "Rebuilding $csh ...\n"; open($ifh, "$rc") || die "$rc: not found: $!\n"; open($ofh, "> $csh") || die "$csh: can't write: $!\n"; while (<$ifh>) { chomp; if (/^#|^[ \t]*$/) { print $ofh "$_\n"; } else { print $ofh "setenv $_\n"; } } close($ifh); close($ofh); } exit(0); #--------------------------------------------------------------------- # Return a true value if the first file entered is older than # or the same age as the second file. sub older { my ($file1, $file2) = @_; my ($mtime1) = (stat($file1))[9]; my ($mtime2) = (stat($file2))[9]; if ($mtime1 && $mtime2) { return ($mtime1 <= $mtime2); } return (1); } #--------------------------------------------------------------------- # Print a short usage message from the comment header and exit. sub usage { my ($emsg) = @_; my $ufh; if (open($ufh, "$0")) { while (<$ufh>) { last if /^# NAME:/; } print STDERR "\n NAME:\n"; while (<$ufh>) { last if /^\s*$/; last if /^# AUTHOR:/; s/^#//; print STDERR; } close($ufh); } warn($emsg); exit(1); } #--------------------------------------------------------------------- # Return the current version. sub version { $_ = '$RCSfile: buildenv,v $ $Revision: 1.6 $ ' . '$Date: 2008/11/02 23:56:06 $'; s/RCSfile: //; s/.Date: //; s/,v . .Revision: / v/; s/\$//g; print "$_\n"; exit(0); }