#!/usr/local/bin/perl

#
# dbcolrename
# Copyright (C) 1991-1998 by John Heidemann <johnh@isi.edu>
# $Id: dbcolrename,v 1.13 1999/11/23 02:04:29 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblibdir for details.
#
sub usage {
    print <<END;
usage: $0 [oldname newname] [oldname newname]...
	Rename columns.

Sample input:
#h account passwd uid gid fullname homedir shell
johnh * 2274 134 John_Heidemann /home/johnh /bin/bash
greg * 2275 134 Greg_Johnson /home/greg /bin/bash
root * 0 0 Root /root /bin/bash
# this is a simple database

Sample command:
cat DATA/passwd.jdb | dbcolrename fullname first_last

Sample output:
#h      account passwd  uid     gid     first_last      homedir shell
johnh * 2274 134 John_Heidemann /home/johnh /bin/bash
greg * 2275 134 Greg_Johnson /home/greg /bin/bash
root * 0 0 Root /root /bin/bash
# this is a simple database
#  | dbcolrename fullname first_last
END
    exit 1;
}

BEGIN {
    $dblibdir = "/usr/local/lib/jdb";
    push(@INC, $dblibdir);
}
require "$dblibdir/dblib.pl";
use DbGetopt;

my(@orig_argv) = @ARGV;
my($prog) = &progname;
my($dbopts) = new DbGetopt("?", \@ARGV);
my($ch);
while ($dbopts->getopt) {
    $ch = $dbopts->opt;
    if (0) {
    } else {
	&usage;
    };
};

&readprocess_header;

while ($#ARGV > 0) {
    ($old, $new) = @ARGV;
    die ("$prog: unmatched rename pair: ``$old'' without new value.\n") if ($#ARGV < 1);
    shift; shift;

    die("$prog: duplicate column names ``$new''.\n") if (defined($colnametonum{$new}));
    if (defined($colnametonum{$old})) {
    	$oldn = $colnametonum{$old};
	&col_unmapping ($old);
	&col_mapping ($new, $oldn);
    } else {
    	die ("$prog: column ``$old'' does not exist.\n");
    };
};

&write_header();

while (<STDIN>) {
    print;
};
print "#  | $prog ", join(" ", @orig_argv), "\n";
exit 0;
