#! /usr/bin/perl
#
# Copyright (c) 2000 Andreas Klemm <andreas@apsfilter.org>. Neuss.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $ApsCVS: src/apsfilter/tools/prtmgmt,v 1.5 2000/12/10 13:54:23 andreas Exp $

use diagnostics;

# read variables
sub variables {

	$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";	# set sane PATH

	$config = "./prtmgmt.conf";	# config file for prtmgmt
	$printcap = "/etc/printcap";	# location of printcap

	$debug = 0;			# turn on debugging
	$verbose = 0;			# list printer aliases as well

	$printer = "";			# printer name
	$palias1 = "";			# printer alias 1
	$palias2 = "";			# printer alias 2

	$lp = "";			# lp - device name for output
	$rm = "";			# rm - machine name for remote printer
	$rp = "";			# rp - remote printer name

	$if = "";			# if - name of input filter script
	$sdbase = "";			# spooldir base
	$sd = "";			# sd - spool directory
	$lf = "";			# lf - error logging file name
	$af = "";			# af - name of accounting file

	$mx = "mx#";			# mx - maximum file size (BUFSIZ blocks)
	$sh = "sh";			# sh - suppress printing of header page
}

# read configuration
sub read_config {

	print "DEBUG: in read_config\n" if $debug;
	print "DEBUG: verbose = $verbose\n" if $debug;

	print "Reading $config...\n" if $verbose;
	open (C, $config) or &init_config;

	while(<C>) {
		print "debug: $_" if $debug;
		# strip CR
		chop;
		# ignore comments ...
		next if /^\s*$/;
        	next if /^\s*#/;
		# XXXXX
		print $_;
	}
	close C;
}

# create empty config_file
sub init_config {
	print "Creating config file...\n" if $verbose;

	open(C, "> $config") or die "$config: $!\n";
	print C <<EOF;
#
# $config - automatically generated by prtmgmt(8)
#

EOF
	close C;
}

# write config_file
sub write_config {
	print "Saving config file...\n" if $verbose;
	rename($config, "$config.bak");

	open(C, "> $config") or die "$config: $!\n";
	print C <<EOF;
#
# $config - automatically generated by prtmgmt(8)
#

EOF
	close C;
}

sub parse_arguments {
	local(@argv) = @_;
	local($usage);
	local($list_printer);

	&usage if $#argv < 0;

	while ($_ = $argv[0], /^-/) {
	    shift @argv;
	    last if /^--$/;
	    if    (/^--?(D|debug)$/) { $debug = 1 }
	    elsif (/^--?(h|help|\?)$/) { $usage = 1 }
	    elsif (/^--?(v|verbose|\?)$/) { $verbose = 1 }
	    elsif (/^--?(a|add|\?)$/) {
		$add_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(d|\?)$/) {
		$deactivate_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(l|list|\?)$/) { $list_printer = 1 }
	    elsif (/^--?(m|modify|\?)$/) {
		$modify_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(r|remove|\?)$/) {
		$remove_printer = 1; $printer = $argv[0]; shift @argv;
	    }
	    elsif (/^--?(pa1|palias1|\?)$/) { $palias1 = $argv[0]; shift @argv }
	    elsif (/^--?(pa2|palias2|\?)$/) { $palias2 = $argv[0]; shift @argv }
	    else {
		print ("error: unknown commandline option\n");
		&usage;
	    }
	}

	# call functions
	&usage			if $usage;
	&add_printer		if $add_printer;
	&deactivate_printer	if $deactivate_printer;
	&list_printer		if $list_printer;
	&modify_printer		if $modify_printer;
	&remove_printer		if $remove_printer;
}

sub usage {
	warn <<USAGE;
usage: prtmgmt

    [-a|-add]     add printer         [-D|debug]    set debugging mode
    [-d]          deactivate printer  [-h|-help]    this help
    [-l|-list]    list printer        [-v|-verbose] increase verbosity
    [-m|-modify]  modify printer
    [-r|-remove]  remove printer      [-i|-init]    initialize some defaults

    for add/delete/modify operation:
    [-p|-printer]      printer name
    [-pa1|-palias1]    printer alias 1
    [-pa2|-palias2]    printer alias 2

printcap=$printcap sdbase=$sdbase sd=$sd
lf=$lf af=$af if=$if
lp=$lp rm=$rm rp=$rp
mx=$mx sh=$sh
USAGE
	exit 1;
}

# add printer in $printcap
sub add_printer {
	print "not yet implemented\n";
	exit 1;
}

# deactivate printer in $printcap
sub deactivate_printer {
	print "not yet implemented\n";
	exit 1;
}

# list name of printers in $printcap
sub list_printer {
	local($pname, $palias1, $palias2);

	print "DEBUG: in list_printer\n" if $debug;
	print "DEBUG: verbose = $verbose\n" if $debug;

	open(P, $printcap) || die "$printcap: $!\n";

	while (<P>) {
		print "debug: $_" if $debug;
		# strip CR
		chop;
		# ignore comments ...
		next if /^\s*$/;
        	next if /^\s*#/;
 		# ... and continued printer configuration lines
        	next if /^\s*:/;
		# read printer name
		($pname,$palias1,$palias2) = (split(/\|/, $_));
		($palias1) = (split(/:/, $palias1));
		($palias2) = (split(/:/, $palias2));
		if ($verbose) {
			print "$pname:$palias1:$palias2\n"
		} else {
			print "$pname\n";
		}
	}
	close P;
}

# modify printer in $printcap
sub modify_printer {
	print "not yet implemented\n";
	exit 1;
}

# remove printer from $printcap
sub remove_printer {
	print "not yet implemented\n";
	exit 1;
}

sub confirm_yn {
	local($message, $confirm) = @_;
	local($yes) = '^(yes|YES|y|Y)$';
	local($no) = '^(no|NO|n|N)$';
	local($read);

	if ($confirm && ($confirm =~ "$yes" || $confirm == 1)) {
		$confirm = "y";
	} else {
		$confirm = "n";
	}
	print "$message (y/n) [$confirm]: ";
	chop($read = <STDIN>);
	$read =~ s/^\s*//;
	$read =~ s/\s*$//;
	return 1 unless $read;

	if (($confirm eq "y" && $read =~ "$yes") ||
	    ($confirm eq "n" && $read =~ "$no")) {
		return 1;
	}

	if ($read !~ "$yes" && $read !~ "$no") {
		warn "Wrong value. Enter again!\a\n";
		return &confirm_yn($message, $confirm);
	}
	return 0;
}

################
# main
#

&variables;				# initialize variables
&read_config;				# read configuration
&parse_arguments(@ARGV);		# parse arguments
&write_config;				# write configuration

exit 0
