#!/usr/bin/perl -w
#
# $Source: /home/vogelke/projects/config-sh/RCS/makecfg,v $
# $Host: sys7.com $
# $UUID: 09180db4-4514-367f-a20b-081c804a2590 $
#
#<makecfg: print some useful configuration variables generated by perl.

use Config qw(config_sh);
use File::Basename;
use strict;

my $myname  = basename($0);
my $now     = scalar(localtime(time()));
my $DATE    = $1 if q$Date: 2009/12/31 02:18:18 $ =~ /Date: (.*) /;
my $VERSION = sprintf("%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/);

$myname =~ s/\.\w*$//;   # strip any extension

# There's probably some really cool way to do this, I don't care.

print "# LOCAL SCRIPT CONFIGURATION VARIABLES\n";
print "cf_by='$myname $VERSION $DATE'\n";
print "cf_mail='root\@localhost'\n";
print "cf_time='$now'\n";
print 'cf_version=\'$', 'Revision$\'', "\n";

# -------------------------------------------------------------------------
print "\n# Defaults\n";

foreach (split(/\n/, config_sh())) {
    /^alignbytes=/    && print "$_\n";
    /^archname=/      && print "$_\n";
    /^byteorder=/     && print "$_\n";
    /^ccflags=/       && print "$_\n";
    /^doublesize=/    && print "$_\n";
    /^fpossize=/      && print "$_\n";
    /^gidsize=/       && print "$_\n";
    /^i16size=/       && print "$_\n";
    /^i16type=/       && print "$_\n";
    /^i32size=/       && print "$_\n";
    /^i32type=/       && print "$_\n";
    /^i64size=/       && print "$_\n";
    /^i64type=/       && print "$_\n";
    /^i8size=/        && print "$_\n";
    /^i8type=/        && print "$_\n";
    /^installbin=/    && print "$_\n";
    /^installprefix=/ && print "$_\n";
    /^intsize=/       && print "$_\n";
    /^issymlink=/     && print "$_\n";
    /^ldflags=/       && print "$_\n";
    /^longdblsize=/   && print "$_\n";
    /^longlongsize=/  && print "$_\n";
    /^longsize=/      && print "$_\n";
    /^lseeksize=/     && print "$_\n";
    /^maildir=/       && print "$_\n";
    /^man1dir=/       && print "$_\n";
    /^man1ext=/       && print "$_\n";
    /^man3dir=/       && print "$_\n";
    /^man3ext=/       && print "$_\n";
    /^myarchname=/    && print "$_\n";
    /^mydomain=/      && print "$_\n";
    /^myhostname=/    && print "$_\n";
    /^myuname=/       && print "$_\n";
    /^osname=/        && print "$_\n";
    /^osvers=/        && print "$_\n";
    /^ptrsize=/       && print "$_\n";
    /^sh=/            && print "$_\n";
    /^shells=/        && print "$_\n";
    /^sizesize=/      && print "$_\n";
    /^startsh=/       && print "$_\n";
    /^sysman=/        && print "$_\n";
    /^u16size=/       && print "$_\n";
    /^u16type=/       && print "$_\n";
    /^u32size=/       && print "$_\n";
    /^u32type=/       && print "$_\n";
    /^u64size=/       && print "$_\n";
    /^u64type=/       && print "$_\n";
    /^u8size=/        && print "$_\n";
    /^u8type=/        && print "$_\n";
    /^uidsize=/       && print "$_\n";
}

# -------------------------------------------------------------------------
print "\n# Standard paths\n";

# Safe default path.
print "defpath='/sbin:/usr/bin:/bin:/usr/sbin'\n";

# Standard utilities path.
my @path;

@path = qw(
    /usr/local/bin /opt/sfw/bin /usr/sfw/bin /sbin
    /usr/bin       /bin         /usr/sbin
);
# @path = setpath();   # UNCOMMENT IF YOU NEED A DEFAULT PATH.
@path = checkpath(@path);

$_ = join(":", @path);
print "stdpath='$_'\n";

# Standard utilities.
print "\n# Standard utilities\n";

push (@path, '/usr/ccs/bin');   # Don't need this in standard path.

my @utils = qw/
    ar       awk   bash  bison byacc cat  chgrp chmod chown comm
    compress cp    cpio  cpp   csh   date echo  egrep expr  find
    flex     gcc   gfind gmake grep  gzip ksh   ld    less  ln
    ls       mailx make  mkdir more  mv   nm    nroff pg    pr
    rm       sed   shar  sleep sort  tail tar   tbl   tee   test
    touch    tr    troff uname uniq  vi   yacc  zcat  zip
/;

foreach (@utils) {
    print "$_='", which("$_"), "'\n";
}

# Special cases.
my $cat = which('cat');
my $em = which('mailx');
my $hostname = which('hostname');
my $ln = which('ln');
my $less = which('less');
my $ranlib = which('ranlib');

print "\n";
print "groupcat='$cat /etc/group'\n";
print "hostcat='$cat /etc/hosts'\n";
print "lns='$ln -s'\n";
print "mail='$em'\n";
print "pager='$less'\n";
print "passcat='$cat /etc/passwd'\n";
print "phostname='$hostname'\n";
print "ranlib='$ranlib'\n";
print "spitshell='$cat'\n";

# -------------------------------------------------------------------------
print "\n# Local changes\n";

# Samba information.
print "samba_map='/PATH/TO/SAMBA/MAPFILE'\n";
print "samba_cfg='/PATH/TO/SAMBA/CONFIGFILE'\n";

# Perl invocation.
print "startperl='#!/usr/bin/perl'\n";
print "perlpath='/usr/bin/perl'\n";
print "perl5='/usr/bin/perl'\n";

# Compiler (gcc).
my $cc = which('gcc');
print "cc='$cc'\n";

print "\n# EOF\n";
exit(0);

# -------------------------------------------------------------------------
# Given an array of directories, find a program.

sub which {
    my $prog = shift;
    foreach (@path) {
        return "$_/$prog" if -x "$_/$prog";
    }
    return "$prog NOT FOUND";
}

# -------------------------------------------------------------------------
# Come up with a default path.

sub setpath {
    my @p = ();

    my @dirs = qw(
        /usr/local/bin  /coll/local/bin /usr/lbin      /opt/sfw/bin
        /usr/sfw/bin    /opt/bin        /opt/local/bin /opt/local
        /opt/lbin       /usr/5bin       /usr/gnu/bin   /usr/new
        /usr/new/bin    /usr/nbin       /opt/gnu/bin   /opt/new
        /opt/new/bin    /opt/nbin       /bin           /usr/bin
        /sys5.3/bin     /sys5.3/usr/bin /bsd4.3/bin    /bsd4.3/usr/ucb
        /bsd4.3/usr/bin /usr/bsd        /bsd43/bin     /usr/ccs/bin
        /usr/lib        /sbin           /usr/sbin      /usr/libexec
    );

    foreach (@dirs) {
        push(@p, $_) if -d "$_";
    }
    return (@p);
}

# -------------------------------------------------------------------------
# Verify a default path.

sub checkpath {
    my @p = ();
    foreach (@_) {
        push(@p, $_) if -d "$_";
    }
    return (@p);
}
