package fs; # $Id$

use diagnostics;
use strict;

use MDK::Common::System;
use common;
use log;
use devices;
use partition_table qw(:types);
use run_program;
use swap;
use detect_devices;
use commands;
use modules;
use fsedit;
use loopback;


sub read_fstab {
    my ($prefix, $file, $all_options) = @_;

    map {
	my ($dev, $mntpoint, $type, $options, $freq, $passno) = split;

	$options = 'defaults' if $options eq 'rw'; # clean-up for mtab read

	$type = fs2type($type);
	if ($type eq 'supermount') {
	    # normalize this bloody supermount
	    $options = join(",", grep {
		if (/fs=(.*)/) {
		    $type = $1;
		    0;
		} elsif (/dev=(.*)/) {
		    $dev = $1;
		    0;
		} else {
		    1;
		}
	    } split(',', $options));
	}
	my $h = { device => $dev, mntpoint => $mntpoint, type => $type, options => $options, if_($all_options, freq => $freq, passno => $passno) };

	($h->{major}, $h->{minor}) = unmakedev((stat "$prefix$dev")[6]);

	if ($dev =~ m,/(tmp|dev)/,) {
	    my $symlink = readlink("$prefix$dev");
	    $dev =~ s,/(tmp|dev)/,,;

	    if ($symlink =~ m|^[^/]+$|) {
		$h->{device_alias} = $dev;
		$h->{device} = $symlink;
	    } else {
		$h->{device} = $dev;
	    }
	}
	$h;
    } cat_("$prefix$file");
}

sub merge_fstabs {
    my ($fstab, @l) = @_;

    foreach my $p (@$fstab) {
	my ($p2) = grep { fsedit::is_same_hd($_, $p) } @l or next;
	@l       = grep { !fsedit::is_same_hd($_, $p) } @l;

	$p2->{type} && $p->{type} ne $p2->{type} && type2fs($p) ne type2fs($p2) && $p->{type} ne 'auto' && $p2->{type} ne 'auto' and
	  log::l("err, fstab and partition table do not agree for $p->{device} type: " . (type2fs($p) || type2name($p->{type})) . " vs ", (type2fs($p2) || type2name($p2->{type}))), next;
	
	$p->{mntpoint} = $p2->{mntpoint} if delete $p->{unsafeMntpoint};

	$p->{type} ||= $p2->{type};
	$p->{options} = $p2->{options} if $p->{type} eq 'defaults';
	add2hash($p, $p2);
	$p->{device_alias} ||= $p2->{device_alias} || $p2->{device} if $p->{device} ne $p2->{device};
    }
    @l;
}

sub add2all_hds {
    my ($all_hds, @l) = @_;

    @l = merge_fstabs([ fsedit::get_really_all_fstab($all_hds) ], @l);

    foreach (@l) {
	my $s = 
	    isNfs($_) ? 'nfs' :
	    isThisFs('smbfs', $_) ? 'smb' :
	    'special';
	push @{$all_hds->{$s}}, $_;
    }
}

sub get_major_minor {
    eval {
	my (undef, $major, $minor) = devices::entry($_->{device});
	($_->{major}, $_->{minor}) = ($major, $minor);
    } foreach @_;
}

sub merge_info_from_mtab {
    my ($fstab) = @_;

    my @l1 = map { my $l = $_; 
		   my %l = (type => fs2type('swap')); 
		   $l{$_} = $l->{$_} foreach qw(device major minor); 
		   \%l;
	       } read_fstab('', '/proc/swaps');
    
    my @l2 = map { read_fstab('', $_) } '/etc/mtab', '/proc/mounts';

    foreach (@l1, @l2) {
	if ($::isInstall && $_->{mntpoint} eq '/tmp/hdimage') {
	    $_->{real_mntpoint} = delete $_->{mntpoint};
	    $_->{mntpoint} = common::usingRamdisk() && "/mnt/hd"; #- remap for hd install.
	}
	$_->{isMounted} = $_->{isFormatted} = 1;
	delete $_->{options};
    } 
    merge_fstabs($fstab, @l1, @l2);
}

sub merge_info_from_fstab {
    my ($fstab, $prefix, $uniq) = @_;
    my @l = grep { !($uniq && fsedit::mntpoint2part($_->{mntpoint}, $fstab)) } read_fstab($prefix, "/etc/fstab", 'all_options');
    merge_fstabs($fstab, @l);
}

sub write_fstab {
    my ($all_hds, $prefix) = @_;
    $prefix ||= '';

    my @l1 = (fsedit::get_really_all_fstab($all_hds), @{$all_hds->{special}});
    my @l2 = read_fstab($prefix, "/etc/fstab", 'all_options');

    my %new;
    my @l = map { 
	my $device = 
	  $_->{device} eq 'none' || member($_->{type}, qw(nfs smbfs)) ? 
	      $_->{device} : 
	  isLoopback($_) ? 
	      ($_->{mntpoint} eq '/' ? "/initrd/loopfs" : "$_->{loopback_device}{mntpoint}") . $_->{loopback_file} :
	  do {
	      my $dir = $_->{device} =~ m|^/| ? '' : '/dev/';
	      eval { devices::make("$prefix$dir$_->{device}") };
	      "$dir$_->{device}";
	  };

	my $real_mntpoint = $_->{mntpoint} || ${{ '/tmp/hdimage' => '/mnt/hd' }}{$_->{real_mntpoint}};
	mkdir("$prefix/$real_mntpoint", 0755);
	my $mntpoint = loopback::carryRootLoopback($_) ? '/initrd/loopfs' : $real_mntpoint;

	my ($freq, $passno) =
	  exists $_->{freq} ?
	    ($_->{freq}, $_->{passno}) :
	  isTrueFS($_) ? 
	    (1, $_->{mntpoint} eq '/' ? 1 : loopback::carryRootLoopback($_) ? 0 : 2) : 
	    (0, 0);

	if (($device eq 'none' || !$new{$device}) && ($mntpoint eq 'swap' || !$new{$mntpoint})) {
	    #- keep in mind the new line for fstab.
	    $new{$device} = 1;
	    $new{$mntpoint} = 1;

	    my $options = $_->{options};
	    my $type = type2fs($_);

	    my $dev = $_->{device_alias} ? "/dev/$_->{device_alias}" : $device;

	    # handle bloody supermount special case
	    if ($options =~ /supermount/) {
		$options = join(",", "dev=$dev", "fs=$type", grep { $_ ne 'supermount' } split(':', $options));
		($dev, $type) = ($mntpoint, 'supermount');
	    }

	    [ $dev, $mntpoint, $type, $options || 'defaults', $freq, $passno ];
	} else {
	    ()
	}
    } grep { $_->{device} && ($_->{mntpoint} || $_->{real_mntpoint}) && $_->{type} } (@l1, @l2);

    log::l("writing $prefix/etc/fstab");
    output("$prefix/etc/fstab", map { join(' ', @$_) . "\n" } sort { $a->[1] cmp $b->[1] } @l);
}

sub auto_fs() {
    grep { chop; $_ && !/nodev/ } cat_("/etc/filesystems");
}

sub mount_options {
    my %non_defaults = (
			sync => 'async', noatime => 'atime', noauto => 'auto', ro => 'rw', 
			user => 'nouser', nodev => 'dev', noexec => 'exec', nosuid => 'suid',
		       );
    my @user_implies = qw(noexec nodev nosuid);
    \%non_defaults, \@user_implies;
}

# simple function
# use mount_options_unpack + mount_options_pack for advanced stuff
sub add_options(\$@) {
    my ($option, @options) = @_;
    my %l; @l{split(',', $$option), @options} = (); delete $l{defaults};
    $$option = join(',', keys %l) || "defaults";
}

sub mount_options_unpack {
    my ($part) = @_;
    my $packed_options = $part->{options};

    my ($non_defaults, $user_implies) = mount_options();

    my @auto_fs = auto_fs();
    my %per_fs = (
		  iso9660 => [ qw(unhide) ],
		  vfat => [ qw(umask=0) ],
		  nfs => [ qw(rsize=8192 wsize=8192) ],
		  smbfs => [ qw(username= password=) ],
		 );
    while (my ($fs, $l) = each %per_fs) {
	isThisFs($fs, $part) || $part->{type} eq 'auto' && member($fs, @auto_fs) or next;
	$non_defaults->{$_} = 1 foreach @$l;
    }

    $non_defaults->{supermount} = 1 if member(type2fs($part), 'auto', @auto_fs);

    my $defaults = { reverse %$non_defaults };
    my %options = map { $_ => '' } keys %$non_defaults;
    my @unknown;
    foreach (split(",", $packed_options)) {
	if ($_ eq 'user') {
	    $options{$_} = 1 foreach ('user', @$user_implies);
	} elsif (exists $non_defaults->{$_}) {
	    $options{$_} = 1;
	} elsif ($defaults->{$_}) {
	    $options{$defaults->{$_}} = 0;
	} elsif (/(.*?=)(.*)/) {
	    $options{$1} = $2;
	} else {
	    push @unknown, $_;
	}
    }
    # merge those, for cleaner help
    $options{'rsize=8192,wsize=8192'} = delete $options{'rsize=8192'} && delete $options{'wsize=8192'}
      if exists $options{'rsize=8192'};

    my $unknown = join(",", @unknown);
    \%options, $unknown;
}

sub mount_options_pack {
    my ($part, $options, $unknown) = @_;

    my ($non_defaults, $user_implies) = mount_options();
    my @l;

    if (delete $options->{user}) {
	push @l, 'user';
	foreach (@$user_implies) {
	    if (!delete $options->{$_}) {
		# overriding
		$options->{$non_defaults->{$_}} = 1;
	    }
	}
    }
    push @l, map_each { if_($::b, $::a =~ /=$/ ? "$::a$::b" : $::a) } %$options;
    push @l, $unknown;

    $part->{options} = join(",", grep { $_ } @l);
}

sub mount_options_help {
    my %help = map { $_ => '' } @_;
    my %short = map { if_(/(.*?)=/, "$1=" => $_) } keys %help;

    foreach (split(':', $ENV{LANGUAGE}), '') {
	my $manpage = "/usr/share/man/$_/man8/mount.8.bz2";
	-e $manpage or next;

	my ($tp, $option);
	foreach (`bzip2 -dc $manpage`) {
	    my $prev_tp = $tp;
	    $tp = /^\.(TP|RE)/;
	    my ($s) = /^\.B (.*)/;
	    if ($prev_tp && $s eq '\-o' .. /X^/) {
		if (my $v = $prev_tp && $s =~ /^[a-z]/i .. $tp) {
		    if ($v == 1) {
			$s = $short{$s} || $s;
			$option = exists $help{$s} && !$help{$s} ? $s : '';
		    } elsif ($v !~ 'E0') {
			s/\\//g;
			s/\s*"(.*?)"\s*/$1/g if s/^\.BR\s+//;
			s/^\.B\s+//;
			$help{$option} .= $_ if $option;
		    }
		}        
	    }
	}
    }
    %help;
}

sub set_default_options {
    my ($part, $is_removable, $useSupermount, $security, $iocharset, $codepage) = @_;

    my ($options, $unknown) = mount_options_unpack($part);

    if ($is_removable) {
	$options->{supermount} = $useSupermount;
	$part->{type} = 'auto';	# if supermount, code below will handle choosing the right type
    }

    my $is_auto = isThisFs('auto', $part);

    if ($options->{supermount} && $is_auto) {
	# this can't work, guessing :-(
	$part->{type} = fs2type($part->{media_type} eq 'cdrom' ? 'iso9660' : 'vfat');
	$is_auto = 0;
    }

    if ($part->{media_type} eq 'cdrom') {
	$options->{ro} = 1;
    }

    if ($part->{media_type} eq 'fd') {
	# slow device so don't loose time, write now!
	$options->{sync} = 1;
    }

    if (isNfs($part)) {
	put_in_hash($options, { 
			       nosuid => 1, 'rsize=8192,wsize=8192' => 1,
			      });
    }
    if (isFat($part) || $is_auto) {

	put_in_hash($options, {
			       user => 1, exec => 1,
			      }) if !exists $part->{rootDevice}; # partition means no removable media

	put_in_hash($options, {
			       'umask=0' => $security < 3, 'iocharset=' => $iocharset, 'codepage=' => $codepage,
			      });
    }
    if (isThisFs('ntfs', $part) || $is_auto) {
	put_in_hash($options, { 'iocharset=' => $iocharset });
    }
    if (isThisFs('iso9660', $part) || $is_auto) {
	put_in_hash($options, { user => 1, exec => 1, });
    }
    if (isThisFs('reiserfs', $part)) {
	$options->{notail} = 1;
    }
    if (isLoopback($part) && !isSwap($part)) { #- no need for loop option for swap files
	$options->{loop} = 1;
    }

    # rationalize: no need for user
    if ($options->{autofs} || $options->{supermount}) {
	$options->{user} = 0;
    }

    # have noauto when we have user
    $options->{noauto} = 1 if $options->{user}; 

    if ($options->{user}) {
	# ensure security  (user_implies - noexec as noexec is not a security matter)
	$options->{$_} = 1 foreach 'nodev', 'nosuid';
    }

    mount_options_pack($part, $options, $unknown);
}

sub set_all_default_options {
    my ($all_hds, $useSupermount, $security, $iocharset, $codepage) = @_;

    my @removables = @{$all_hds->{raw_hds}};

    foreach my $part (fsedit::get_really_all_fstab($all_hds)) {
	set_default_options($part, member($part, @removables), $useSupermount, $security, $iocharset, $codepage);
    }
}

sub set_removable_mntpoints {
    my ($all_hds) = @_;

    my %names;
    foreach (@{$all_hds->{raw_hds}}) {
	my $name = $_->{media_type};
	if (member($name, 'hd', 'fd')) {
	    if (detect_devices::isZipDrive($_)) {
		$name = 'zip';
	    } elsif ($name eq 'fd') {
		$name = 'floppy';
	    } else {
		log::l("set_removable_mntpoints: don't know what to with hd $_->{device}");
		next;
	    }
	}
	if ($name) {
	    my $s = ++$names{$name};
	    $_->{mntpoint} ||= "/mnt/$name" . ($s == 1 ? '' : $s);
	}
    }
}

sub get_raw_hds {
    my ($prefix, $all_hds) = @_;

    $all_hds->{raw_hds} = 
      [ 
       detect_devices::floppies(), detect_devices::cdroms(), 
       (map { $_->{device} .= '4'; $_ } detect_devices::zips())
      ];
    get_major_minor(@{$all_hds->{raw_hds}});

    my @fstab = read_fstab($prefix, "/etc/fstab", 'all_options');
    $all_hds->{nfss} = [ grep { isNfs($_) } @fstab ];
    $all_hds->{smbs} = [ grep { isThisFs('smbfs', $_) } @fstab ];
    $all_hds->{special} = [
       { device => 'none', mntpoint => '/proc', type => 'proc' },
       { device => 'none', mntpoint => '/dev/pts', type => 'devpts', options => 'mode=0620' },
    ];
}

################################################################################
# formatting functions
################################################################################
sub disable_forced_fsck {
    my ($dev) = @_;
    run_program::run("tune2fs", "-c0", "-i0", devices::make($dev));
}

sub format_ext2($@) {
    #- mke2fs -b (1024|2048|4096) -c -i(1024 > 262144) -N (1 > 100000000) -m (0-100%) -L volume-label
    #- tune2fs
    my ($dev, @options) = @_;
    $dev =~ m,(rd|ida|cciss)/, and push @options, qw(-b 4096 -R stride=16); #- For RAID only.
    push @options, qw(-b 1024 -O none) if arch() =~ /alpha/;
    run_program::run("mke2fs", @options, devices::make($dev)) or die _("%s formatting of %s failed", "ext2", $dev);
}
sub format_ext3 {
    my ($dev, @options) = @_;
    format_ext2($dev, "-j", @options);
    disable_forced_fsck($dev);
}
sub format_reiserfs {
    my ($dev, @options) = @_;
    #TODO add -h tea
    run_program::run("mkreiserfs", "-f", "-q", @options, devices::make($dev)) or die _("%s formatting of %s failed", "reiserfs", $dev);
}
sub format_xfs {
    my ($dev, @options) = @_;
    run_program::run("mkfs.xfs", "-f", "-q", @options, devices::make($dev)) or die _("%s formatting of %s failed", "xfs", $dev);
}
sub format_jfs {
    my ($dev, @options) = @_;
    run_program::run("mkfs.jfs", "-f", @options, devices::make($dev)) or die _("%s formatting of %s failed", "jfs", $dev);
}
sub format_dos {
    my ($dev, @options) = @_;
    run_program::run("mkdosfs", @options, devices::make($dev)) or die _("%s formatting of %s failed", "dos", $dev);
}
sub format_hfs {
    my ($dev, @options) = @_;
    run_program::run("hformat", @options, devices::make($dev)) or die _("%s formatting of %s failed", "HFS", $dev);
}
sub real_format_part {
    my ($part) = @_;

    $part->{isFormatted} and return;

    my @options = $part->{toFormatCheck} ? "-c" : ();
    log::l("formatting device $part->{device} (type ", type2name($part->{type}), ")");

    if (isExt2($part)) {
	push @options, "-F" if isLoopback($part);
	format_ext2($part->{device}, @options);
    } elsif (isThisFs("ext3", $part)) {
        format_ext3($part->{device}, @options);
    } elsif (isThisFs("reiserfs", $part)) {
        format_reiserfs($part->{device}, @options, if_(c::kernel_version() =~ /^\Q2.2/, "-v", "1"));
    } elsif (isThisFs("xfs", $part)) {
        format_xfs($part->{device}, @options);
    } elsif (isThisFs("jfs", $part)) {
        format_jfs($part->{device}, @options);
    } elsif (isDos($part)) {
        format_dos($part->{device}, @options);
    } elsif (isWin($part)) {
        format_dos($part->{device}, @options, '-F', 32);
    } elsif (isThisFs('hfs', $part)) {
        format_hfs($part->{device}, @options, '-l', "Untitled");
    } elsif (isAppleBootstrap($part)) {
        format_hfs($part->{device}, @options, '-l', "bootstrap");
    } elsif (isSwap($part)) {
	my $check_blocks = grep { /^-c$/ } @options;
        swap::make($part->{device}, $check_blocks);
    } else {
	die _("I don't know how to format %s in type %s", $_->{device}, type2name($_->{type}));
    }
    $part->{isFormatted} = 1;
}
sub format_part {
    my ($raids, $part, $prefix) = @_;
    if (isRAID($part)) {
	require raid;
	raid::format_part($raids, $part);
    } elsif (isLoopback($part)) {
	loopback::format_part($part, $prefix);
    } else {
	real_format_part($part);
    }
}

################################################################################
# mounting functions
################################################################################
sub formatMount_part {
    my ($part, $raids, $fstab, $prefix, $callback) = @_;

    if (isLoopback($part)) {
	formatMount_part($part->{loopback_device}, $raids, $fstab, $prefix, $callback);
    }
    if (my $p = up_mount_point($part->{mntpoint}, $fstab)) {
	formatMount_part($p, $raids, $fstab, $prefix, $callback) unless loopback::carryRootLoopback($part);
    }

    if ($part->{toFormat}) {
	$callback->($part) if $callback;
	format_part($raids, $part, $prefix);
    }
    mount_part($part, $prefix);
}

sub formatMount_all {
    my ($raids, $fstab, $prefix, $callback) = @_;
    formatMount_part($_, $raids, $fstab, $prefix, $callback) 
      foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab;

    #- ensure the link is there
    loopback::carryRootCreateSymlink($_, $prefix) foreach @$fstab;

    #- for fun :)
    #- that way, when install exits via ctrl-c, it gives hand to partition
    eval {
	local $SIG{__DIE__} = 'ignore';
	my ($type, $major, $minor) = devices::entry(fsedit::get_root($fstab)->{device});
	output "/proc/sys/kernel/real-root-dev", makedev($major, $minor);
    };
}

sub mount {
    my ($dev, $where, $fs, $rdonly) = @_;
    log::l("mounting $dev on $where as type $fs");

    -d $where or commands::mkdir_('-p', $where);

    my @fs_modules = qw(vfat hfs romfs ufs reiserfs xfs jfs ext3);

    if (member($fs, 'smb', 'nfs') && $::isStandalone) {
	system('mount', $dev, $where) == 0 or die _("mount failed");
    } elsif (member($fs, 'ext2', 'proc', 'usbdevfs', 'iso9660', @fs_modules)) {
	$dev = devices::make($dev) if $fs ne 'proc' && $fs ne 'usbdevfs';
	$where =~ s|/$||;

	my $flag = c::MS_MGC_VAL();
	$flag |= c::MS_RDONLY() if $rdonly;
	my $mount_opt = "";

	if ($fs eq 'vfat') {
	    $mount_opt = 'check=relaxed';
	} elsif ($fs eq 'reiserfs') {
	    #- could be better if we knew if there is a /boot or not
	    #- without knowing it, / is forced to be mounted with notail
	    # if $where =~ m|/(boot)?$|;
	    $mount_opt = 'notail'; #- notail in any case
	} elsif ($fs eq 'ext2') {
	    run_program::run("fsck.ext2", "-a", $dev);
	    $? & 0x0100 and log::l("fsck corrected partition $dev");
	    $? & 0xfeff and die _("fsck failed with exit code %d or signal %d", $? >> 8, $? & 255);
	}
	if (member($fs, @fs_modules)) {
	    eval { modules::load($fs) };
	} elsif ($fs eq 'iso9660') {
	    eval { modules::load('isofs') };
	}
	if ($fs eq 'ext3' && $::isInstall) {
	    # ext3 mount to use the journal
	    syscall_('mount', $dev, $where, $fs, $flag, $mount_opt) or die _("mount failed: ") . "$!";
	    syscall_('umount', $where);
	    # really mount as ext2 during install for speed up
	    $fs = 'ext2';
	}
	log::l("calling mount($dev, $where, $fs, $flag, $mount_opt)");
	syscall_('mount', $dev, $where, $fs, $flag, $mount_opt) or die _("mount failed: ") . "$!";
    } else {
	log::l("skipping mounting $fs partition");
	return;
    }
    local *F;
    open F, ">>/etc/mtab" or return; #- fail silently, /etc must be read-only
    print F "$dev $where $fs defaults 0 0\n";
}

#- takes the mount point to umount (can also be the device)
sub umount {
    my ($mntpoint) = @_;
    $mntpoint =~ s|/$||;
    log::l("calling umount($mntpoint)");
    syscall_('umount', $mntpoint) or die _("error unmounting %s: %s", $mntpoint, "$!");

    substInFile { $_ = '' if /(^|\s)$mntpoint\s/ } '/etc/mtab'; #- don't care about error, if we can't read, we won't manage to write... (and mess mtab)
}

sub mount_part {
    my ($part, $prefix, $rdonly) = @_;

    #- root carrier's link can't be mounted
    loopback::carryRootCreateSymlink($part, $prefix);

    if ($part->{isMounted} && $part->{real_mntpoint} && $part->{mntpoint}) {
	log::l("remounting partition on $prefix$part->{mntpoint} instead of $part->{real_mntpoint}");
	if ($::isInstall) { #- ensure partition will not be busy.
	    require install_any;
	    install_any::getFile('XXX');
	}
	umount($part->{real_mntpoint});
	rmdir $part->{real_mntpoint};
	symlinkf "$prefix$part->{mntpoint}", $part->{real_mntpoint};
	delete $part->{real_mntpoint};
	$part->{isMounted} = 0;
    }

    return if $part->{isMounted};

    unless ($::testing) {
	if (isSwap($part)) {
	    swap::swapon($part->{device});
	} else {
	    $part->{mntpoint} or die "missing mount point for partition $part->{device}";

	    my $dev = $part->{device};
	    my $mntpoint = ($prefix || '') . $part->{mntpoint};
	    if (isLoopback($part)) {
		eval { modules::load('loop') };
		$dev = $part->{real_device} = devices::set_loop($part->{device}) || die;
	    } elsif (loopback::carryRootLoopback($part)) {
		$mntpoint = "/initrd/loopfs";
	    }
	    mount($dev, $mntpoint, type2fs($part), $rdonly);
	    rmdir "$mntpoint/lost+found";
	}
    }
    $part->{isMounted} = $part->{isFormatted} = 1; #- assume that if mount works, partition is formatted
}

sub umount_part {
    my ($part, $prefix) = @_;

    $part->{isMounted} || $part->{real_mntpoint} or return;

    unless ($::testing) {
	if (isSwap($part)) {
	    swap::swapoff($part->{device});
	} elsif (loopback::carryRootLoopback($part)) {
	    umount("/initrd/loopfs");
	} else {
	    umount(($prefix || '') . $part->{mntpoint} || devices::make($part->{device}));
	    c::del_loop(delete $part->{real_device}) if isLoopback($part);
	}
    }
    $part->{isMounted} = 0;
}

sub mount_all($;$$) {
    my ($fstab, $prefix) = @_;

    #- TODO fsck, create check_mount_all ?
    log::l("mounting all filesystems");

    #- order mount by alphabetical ordre, that way / < /home < /home/httpd...
    foreach (sort { $a->{mntpoint} cmp $b->{mntpoint} } grep { isSwap($_) || $_->{mntpoint} && isTrueFS($_) } @$fstab) {
	mount_part($_, $prefix);
    }
}

sub umount_all($;$) {
    my ($fstab, $prefix) = @_;

    log::l("unmounting all filesystems");

    foreach (sort { $b->{mntpoint} cmp $a->{mntpoint} } @$fstab) {
	$_->{mntpoint} and umount_part($_, $prefix);
    }
}

################################################################################
# various functions
################################################################################
sub df {
    my ($part, $prefix) = @_;
    my $dir = "/tmp/tmp_fs_df";

    return $part->{free} if exists $part->{free};

    if ($part->{isMounted}) {
	$dir = ($prefix || '') . $part->{mntpoint};
    } elsif ($part->{notFormatted} && !$part->{isFormatted}) {
	return; #- won't even try!
    } else {
	mkdir $dir;
	eval { mount($part->{device}, $dir, type2fs($part), 'readonly') };
	if ($@) {
	    $part->{notFormatted} = 1;
	    $part->{isFormatted} = 0;
	    unlink $dir;
	    return;
	}
    }
    my (undef, $free) = MDK::Common::System::df($dir);

    if (!$part->{isMounted}) {
	umount($dir);
	unlink($dir)
    }

    $part->{free} = 2 * $free if defined $free;
    $part->{free};
}

sub up_mount_point {
    my ($mntpoint, $fstab) = @_;
    while (1) {
	$mntpoint = dirname($mntpoint);
	$mntpoint ne "." or return;
	$_->{mntpoint} eq $mntpoint and return $_ foreach @$fstab;
    }
}

1;
85'>485</a>
<a id='n486' href='#n486'>486</a>
<a id='n487' href='#n487'>487</a>
<a id='n488' href='#n488'>488</a>
<a id='n489' href='#n489'>489</a>
<a id='n490' href='#n490'>490</a>
<a id='n491' href='#n491'>491</a>
<a id='n492' href='#n492'>492</a>
<a id='n493' href='#n493'>493</a>
<a id='n494' href='#n494'>494</a>
<a id='n495' href='#n495'>495</a>
<a id='n496' href='#n496'>496</a>
<a id='n497' href='#n497'>497</a>
<a id='n498' href='#n498'>498</a>
<a id='n499' href='#n499'>499</a>
<a id='n500' href='#n500'>500</a>
<a id='n501' href='#n501'>501</a>
<a id='n502' href='#n502'>502</a>
<a id='n503' href='#n503'>503</a>
<a id='n504' href='#n504'>504</a>
<a id='n505' href='#n505'>505</a>
<a id='n506' href='#n506'>506</a>
<a id='n507' href='#n507'>507</a>
<a id='n508' href='#n508'>508</a>
<a id='n509' href='#n509'>509</a>
<a id='n510' href='#n510'>510</a>
<a id='n511' href='#n511'>511</a>
<a id='n512' href='#n512'>512</a>
<a id='n513' href='#n513'>513</a>
<a id='n514' href='#n514'>514</a>
<a id='n515' href='#n515'>515</a>
<a id='n516' href='#n516'>516</a>
<a id='n517' href='#n517'>517</a>
<a id='n518' href='#n518'>518</a>
<a id='n519' href='#n519'>519</a>
<a id='n520' href='#n520'>520</a>
<a id='n521' href='#n521'>521</a>
<a id='n522' href='#n522'>522</a>
<a id='n523' href='#n523'>523</a>
<a id='n524' href='#n524'>524</a>
<a id='n525' href='#n525'>525</a>
<a id='n526' href='#n526'>526</a>
<a id='n527' href='#n527'>527</a>
<a id='n528' href='#n528'>528</a>
<a id='n529' href='#n529'>529</a>
<a id='n530' href='#n530'>530</a>
<a id='n531' href='#n531'>531</a>
<a id='n532' href='#n532'>532</a>
<a id='n533' href='#n533'>533</a>
<a id='n534' href='#n534'>534</a>
<a id='n535' href='#n535'>535</a>
<a id='n536' href='#n536'>536</a>
<a id='n537' href='#n537'>537</a>
<a id='n538' href='#n538'>538</a>
<a id='n539' href='#n539'>539</a>
<a id='n540' href='#n540'>540</a>
<a id='n541' href='#n541'>541</a>
<a id='n542' href='#n542'>542</a>
<a id='n543' href='#n543'>543</a>
<a id='n544' href='#n544'>544</a>
<a id='n545' href='#n545'>545</a>
<a id='n546' href='#n546'>546</a>
<a id='n547' href='#n547'>547</a>
<a id='n548' href='#n548'>548</a>
<a id='n549' href='#n549'>549</a>
<a id='n550' href='#n550'>550</a>
<a id='n551' href='#n551'>551</a>
<a id='n552' href='#n552'>552</a>
<a id='n553' href='#n553'>553</a>
<a id='n554' href='#n554'>554</a>
<a id='n555' href='#n555'>555</a>
<a id='n556' href='#n556'>556</a>
<a id='n557' href='#n557'>557</a>
<a id='n558' href='#n558'>558</a>
<a id='n559' href='#n559'>559</a>
<a id='n560' href='#n560'>560</a>
<a id='n561' href='#n561'>561</a>
<a id='n562' href='#n562'>562</a>
<a id='n563' href='#n563'>563</a>
<a id='n564' href='#n564'>564</a>
<a id='n565' href='#n565'>565</a>
<a id='n566' href='#n566'>566</a>
<a id='n567' href='#n567'>567</a>
<a id='n568' href='#n568'>568</a>
<a id='n569' href='#n569'>569</a>
<a id='n570' href='#n570'>570</a>
<a id='n571' href='#n571'>571</a>
<a id='n572' href='#n572'>572</a>
<a id='n573' href='#n573'>573</a>
<a id='n574' href='#n574'>574</a>
<a id='n575' href='#n575'>575</a>
<a id='n576' href='#n576'>576</a>
<a id='n577' href='#n577'>577</a>
<a id='n578' href='#n578'>578</a>
<a id='n579' href='#n579'>579</a>
<a id='n580' href='#n580'>580</a>
<a id='n581' href='#n581'>581</a>
<a id='n582' href='#n582'>582</a>
<a id='n583' href='#n583'>583</a>
<a id='n584' href='#n584'>584</a>
<a id='n585' href='#n585'>585</a>
<a id='n586' href='#n586'>586</a>
<a id='n587' href='#n587'>587</a>
<a id='n588' href='#n588'>588</a>
<a id='n589' href='#n589'>589</a>
<a id='n590' href='#n590'>590</a>
<a id='n591' href='#n591'>591</a>
<a id='n592' href='#n592'>592</a>
<a id='n593' href='#n593'>593</a>
<a id='n594' href='#n594'>594</a>
<a id='n595' href='#n595'>595</a>
<a id='n596' href='#n596'>596</a>
<a id='n597' href='#n597'>597</a>
<a id='n598' href='#n598'>598</a>
<a id='n599' href='#n599'>599</a>
<a id='n600' href='#n600'>600</a>
<a id='n601' href='#n601'>601</a>
<a id='n602' href='#n602'>602</a>
<a id='n603' href='#n603'>603</a>
<a id='n604' href='#n604'>604</a>
<a id='n605' href='#n605'>605</a>
<a id='n606' href='#n606'>606</a>
<a id='n607' href='#n607'>607</a>
<a id='n608' href='#n608'>608</a>
<a id='n609' href='#n609'>609</a>
<a id='n610' href='#n610'>610</a>
<a id='n611' href='#n611'>611</a>
<a id='n612' href='#n612'>612</a>
<a id='n613' href='#n613'>613</a>
<a id='n614' href='#n614'>614</a>
<a id='n615' href='#n615'>615</a>
<a id='n616' href='#n616'>616</a>
<a id='n617' href='#n617'>617</a>
<a id='n618' href='#n618'>618</a>
<a id='n619' href='#n619'>619</a>
<a id='n620' href='#n620'>620</a>
<a id='n621' href='#n621'>621</a>
<a id='n622' href='#n622'>622</a>
<a id='n623' href='#n623'>623</a>
<a id='n624' href='#n624'>624</a>
<a id='n625' href='#n625'>625</a>
<a id='n626' href='#n626'>626</a>
<a id='n627' href='#n627'>627</a>
<a id='n628' href='#n628'>628</a>
<a id='n629' href='#n629'>629</a>
<a id='n630' href='#n630'>630</a>
<a id='n631' href='#n631'>631</a>
<a id='n632' href='#n632'>632</a>
<a id='n633' href='#n633'>633</a>
<a id='n634' href='#n634'>634</a>
<a id='n635' href='#n635'>635</a>
<a id='n636' href='#n636'>636</a>
<a id='n637' href='#n637'>637</a>
</pre></td>
<td class='lines'><pre><code><span class="hl kwa">package</span> printer<span class="hl opt">;</span>
<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head1 NAME</span>
<span class="hl com"></span>
<span class="hl com">printer - supply methods for manage the printer related files directory handles</span>
<span class="hl com"></span>
<span class="hl com">=head1 SYNOPSIS</span>
<span class="hl com"></span>
<span class="hl com">use printer;</span>
<span class="hl com"></span>
<span class="hl com">=head1 DESCRIPTION</span>
<span class="hl com"></span>
<span class="hl com">Use the source.</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl kwa">use</span> diagnostics<span class="hl opt">;</span>
<span class="hl kwa">use</span> strict<span class="hl opt">;</span>


<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Exported variable</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>
<span class="hl kwa">use</span> vars <span class="hl str">qw(</span><span class="hl ipl">%thedb</span> <span class="hl str"></span><span class="hl ipl">%thedb_gsdriver</span> <span class="hl str"></span><span class="hl ipl">%printer_type</span> <span class="hl str"></span><span class="hl ipl">%printer_type_inv</span> <span class="hl str"></span><span class="hl ipl">$printer_type_default</span> <span class="hl str"></span><span class="hl ipl">&#64;papersize_type</span> <span class="hl str"></span><span class="hl ipl">%fields</span> <span class="hl str"></span><span class="hl ipl">$spooldir</span> <span class="hl str"></span><span class="hl ipl">&#64;entries_db_short</span> <span class="hl str"></span><span class="hl ipl">&#64;entry_db_description</span> <span class="hl str"></span><span class="hl ipl">%descr_to_db</span> <span class="hl str"></span><span class="hl ipl">%db_to_descr</span><span class="hl str">)</span><span class="hl opt">;</span>
<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Imports</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>
<span class="hl kwa">use</span> Data<span class="hl opt">::</span>Dumper<span class="hl opt">;</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 pixel imports</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl kwa">use</span> common <span class="hl str">qw(:common :system)</span><span class="hl opt">;</span>
<span class="hl kwa">use</span> commands<span class="hl opt">;</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Examples and types</span>
<span class="hl com"></span>
<span class="hl com">=over 4</span>
<span class="hl com"></span>
<span class="hl com">=item *</span>
<span class="hl com"></span>
<span class="hl com">an entry in the &apos;printerdb&apos; file, which describes each type of</span>
<span class="hl com">supported printer:</span>
<span class="hl com"></span>
<span class="hl com">	StartEntry: DeskJet550</span>
<span class="hl com">	  GSDriver: cdj550</span>
<span class="hl com">	  Description: {HP DeskJet 550C/560C/6xxC series}</span>
<span class="hl com">	  About: { \</span>
<span class="hl com">		     This driver supports the HP inkjet printers which have \</span>
<span class="hl com">		     color capability using both black and color cartridges \</span>
<span class="hl com">		     simultaneously. Known to work with the 682C and the 694C. \</span>
<span class="hl com">		     Other 600 and 800 series printers may work \</span>
<span class="hl com">		     if they have this feature. \</span>
<span class="hl com">		     If your printer seems to be saturating the paper with ink, \</span>
<span class="hl com">		     try added an extra GS option of &apos;-dDepletion=2&apos;. \</span>
<span class="hl com">		     Ghostscript supports several optional parameters for \</span>
<span class="hl com">		     this driver: see the document &apos;devices.doc&apos; \</span>
<span class="hl com">		     in the ghostscript directory under /usr/doc. \</span>
<span class="hl com">		   }</span>
<span class="hl com">	  Resolution: {300} {300} {}</span>
<span class="hl com">	  BitsPerPixel:  {3} {Normal color printing with color cartridge}</span>
<span class="hl com">	  BitsPerPixel:  {8} {Floyd-Steinberg B &amp; W printing for better greys}</span>
<span class="hl com">	  BitsPerPixel: {24} {Floyd-Steinberg Color printing (best, but slow)}</span>
<span class="hl com">	  BitsPerPixel: {32} {Sometimes provides better output than 24}</span>
<span class="hl com">	EndEntry</span>
<span class="hl com"></span>
<span class="hl com">Example of data-struct:</span>
<span class="hl com"></span>
<span class="hl com">	my %ex_printerdb_entry =</span>
<span class="hl com">	  (</span>
<span class="hl com">	   ENTRY    =&gt; &quot;DeskJet550&quot;,                               #-Human-readable name of the entry</span>
<span class="hl com">	   GSDRIVER =&gt; &quot;cdj550&quot;,                                   #-gs driver used by this printer</span>
<span class="hl com">	   DESCR    =&gt; &quot;HP DeskJet 550C/560C/6xxC series&quot;,         #-Single line description of printer</span>
<span class="hl com">	   ABOUT    =&gt; &quot;</span>
<span class="hl com">	   This driver supports the HP inkjet printers which have</span>
<span class="hl com">	   color capability using both black and color cartridges</span>
<span class="hl com">		...&quot;,                                              #-Lengthy description of printer</span>
<span class="hl com">	   RESOLUTION   =&gt; [                                       #-List of resolutions supported</span>
<span class="hl com">			    {</span>
<span class="hl com">			     XDPI    =&gt; 300,</span>
<span class="hl com">			     YDPI    =&gt; 300,</span>
<span class="hl com">			     DESCR   =&gt; &quot;commentaire&quot;,</span>
<span class="hl com">			    },</span>
<span class="hl com">			   ],</span>
<span class="hl com">	   BITSPERPIXEL =&gt; [                                       #-List of color depths supported</span>
<span class="hl com">			    {</span>
<span class="hl com">			     DEPTH =&gt; 3,</span>
<span class="hl com">			     DESCR =&gt; &quot;Normal color printing with color cartridge&quot;,</span>
<span class="hl com">			    },</span>
<span class="hl com">			   ],</span>
<span class="hl com">	  )</span>
<span class="hl com">	;</span>
<span class="hl com"></span>
<span class="hl com">=item *</span>
<span class="hl com"></span>
<span class="hl com">A printcap entry only represents a subset of possible options available</span>
<span class="hl com">Sufficient for the simple configuration we are interested in</span>
<span class="hl com">there is also some text in the template (.in) file in the spooldir</span>
<span class="hl com"></span>
<span class="hl com">	# /etc/printcap</span>
<span class="hl com">	#</span>
<span class="hl com">	# Please don&apos;t edit this file directly unless you know what you are doing</span>
<span class="hl com">	# Be warned that the control-panel printtool requires a very strict forma</span>
<span class="hl com">	# Look at the printcap(5) man page for more info.</span>
<span class="hl com">	#</span>
<span class="hl com">	# This file can be edited with the printtool in the control-panel.</span>
<span class="hl com"></span>
<span class="hl com">	##PRINTTOOL3## LOCAL uniprint NAxNA letter {} U_NECPrinwriter2X necp2x6 1</span>
<span class="hl com">	lpname:\</span>
<span class="hl com">		:sd=/var/spool/lpd/lpnamespool:\</span>
<span class="hl com">		:mx#45:\</span>
<span class="hl com">		:sh:\</span>
<span class="hl com">		:lp=/dev/device:\</span>
<span class="hl com">		:if=/var/spool/lpd/lpnamespool/filter:</span>
<span class="hl com">	##PRINTTOOL3## REMOTE st800 360x180 a4 {} EpsonStylus800 Default 1</span>
<span class="hl com">	remote:\</span>
<span class="hl com">		:sd=/var/spool/lpd/remotespool:\</span>
<span class="hl com">		:mx#47:\</span>
<span class="hl com">		:sh:\</span>
<span class="hl com">		:rm=remotehost:\</span>
<span class="hl com">		:rp=remotequeue:\</span>
<span class="hl com">		:if=/var/spool/lpd/remotespool/filter:</span>
<span class="hl com">	##PRINTTOOL3## SMB la75plus 180x180 letter {} DECLA75P Default {}</span>
<span class="hl com">	smb:\</span>
<span class="hl com">		:sd=/var/spool/lpd/smbspool:\</span>
<span class="hl com">		:mx#46:\</span>
<span class="hl com">		:sh:\</span>
<span class="hl com">		:if=/var/spool/lpd/smbspool/filter:\</span>
<span class="hl com">		:af=/var/spool/lpd/smbspool/acct:\</span>
<span class="hl com">		:lp=/dev/null:</span>
<span class="hl com">	##PRINTTOOL3## NCP ap3250 180x180 letter {} EpsonAP3250 Default {}</span>
<span class="hl com">	ncp:\</span>
<span class="hl com">		:sd=/var/spool/lpd/ncpspool:\</span>
<span class="hl com">		:mx#46:\</span>
<span class="hl com">		:sh:\</span>
<span class="hl com">		:if=/var/spool/lpd/ncpspool/filter:\</span>
<span class="hl com">		:af=/var/spool/lpd/ncpspool/acct:\</span>
<span class="hl com">		:lp=/dev/null:</span>
<span class="hl com"></span>
<span class="hl com">Example of data-struct:</span>
<span class="hl com"></span>
<span class="hl com">	my %ex_printcap_entry =</span>
<span class="hl com">	  (</span>
<span class="hl com">	   QUEUE    =&gt; &quot;lpname&quot;,                            #-Queue name, can have multi separated by &apos;|&apos;</span>
<span class="hl com"></span>
<span class="hl com">	   #-if you want something different from the default</span>
<span class="hl com">	   SPOOLDIR =&gt; &quot;/var/spool/lpd/lpnamespool/&quot;,        #-Spool directory</span>
<span class="hl com">	   IF       =&gt; &quot;/var/spool/lpd/lpnamespool/filter&quot;,  #-input filter</span>
<span class="hl com"></span>
<span class="hl com">	   #- commentaire inserer dans le printcap pour que printtool retrouve ses petits</span>
<span class="hl com">	   DBENTRY      =&gt; &quot;DeskJet670&quot;,                    #-entry in printer database for this printer</span>
<span class="hl com"></span>
<span class="hl com">	   RESOLUTION   =&gt; &quot;NAxNA&quot;,                         #-ghostscript resolution to use</span>
<span class="hl com">	   PAPERSIZE    =&gt; &quot;letter&quot;,                        #-Papersize</span>
<span class="hl com">	   BITSPERPIXEL =&gt; &quot;necp2x6&quot;,                       #-ghostscript color option</span>
<span class="hl com">	   CRLF         =&gt; 1 ,                              #-Whether or not to do CR/LF xlation</span>
<span class="hl com"></span>
<span class="hl com">	   TYPE         =&gt; &quot;LOCAL&quot;,</span>
<span class="hl com"></span>
<span class="hl com">	   #- LOCAL</span>
<span class="hl com">	   DEVICE   =&gt; &quot;/dev/device&quot;,                       #-Print device</span>
<span class="hl com"></span>
<span class="hl com">	   #- REMOTE (lpd) printers only</span>
<span class="hl com">	   REMOTEHOST   =&gt; &quot;remotehost&quot;,                     #-Remote host (not used for all entries)</span>
<span class="hl com">	   REMOTEQUEUE  =&gt; &quot;remotequeue&quot;,                    #-Queue on the remote machine</span>
<span class="hl com"></span>
<span class="hl com"></span>
<span class="hl com">	   #-SMB (LAN Manager) only</span>
<span class="hl com">	   #- in spooldir/.config</span>
<span class="hl com">	   #-share=&apos;\\hostname\printername&apos;</span>
<span class="hl com">	   #-hostip=1.2.3.4</span>
<span class="hl com">	   #-user=&apos;user&apos;</span>
<span class="hl com">	   #-password=&apos;passowrd&apos;</span>
<span class="hl com">	   #-workgroup=&apos;AS3&apos;</span>
<span class="hl com">	   SMBHOST   =&gt; &quot;hostname&quot;,                              #-Server name (NMB name, can have spaces)</span>
<span class="hl com">	   SMBHOSTIP =&gt; &quot;1.2.3.4&quot;,                               #-Can optional specify and IP address for host</span>
<span class="hl com">	   SMBSHARE  =&gt; &quot;printername&quot;,                           #-Name of share on the SMB server</span>
<span class="hl com">	   SMBUSER   =&gt; &quot;user&quot;,                                  #-User to log in as on SMB server</span>
<span class="hl com">	   SMBPASSWD =&gt; &quot;passowrd&quot;,                              #-Corresponding password</span>
<span class="hl com">	   SMBWORKGROUP =&gt; &quot;AS3&quot;,                                #-SMB workgroup name</span>
<span class="hl com">	   AF        =&gt; &quot;/var/spool/lpd/smbspool/acct&quot;,           #-accounting filter (needed for smbprint)</span>
<span class="hl com"></span>
<span class="hl com">	   #- NCP (NetWare) only</span>
<span class="hl com">	   #- in spooldir/.config</span>
<span class="hl com">	   #-server=printerservername</span>
<span class="hl com">	   #-queue=queuename</span>
<span class="hl com">	   #-user=user</span>
<span class="hl com">	   #-password=pass</span>
<span class="hl com">	   NCPHOST   =&gt; &quot;printerservername&quot;,            #-Server name (NCP name)</span>
<span class="hl com">	   NCPQUEUE  =&gt; &quot;queuename&quot;,                    #-Queue on server</span>
<span class="hl com">	   NCPUSER   =&gt; &quot;user&quot;,                         #-User to log in as on NCP server</span>
<span class="hl com">	   NCPPASSWD =&gt; &quot;pass&quot;,                         #-Corresponding password</span>
<span class="hl com"></span>
<span class="hl com">	  )</span>
<span class="hl com">	;</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Intern constant</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl slc">#-if we are in an DrakX config</span>
<span class="hl kwc">my</span> <span class="hl kwb">$prefix</span> <span class="hl opt">=</span> <span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>

<span class="hl slc">#-location of the printer database in an installed system</span>
<span class="hl kwc">my</span> <span class="hl kwb">$PRINTER_DB_FILE</span>    <span class="hl opt">=</span> <span class="hl str">&quot;/usr/lib/rhs/rhs-printfilters/printerdb&quot;</span><span class="hl opt">;</span>
<span class="hl kwc">my</span> <span class="hl kwb">$PRINTER_FILTER_DIR</span> <span class="hl opt">=</span> <span class="hl str">&quot;/usr/lib/rhs/rhs-printfilters&quot;</span><span class="hl opt">;</span>




<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Exported constant</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl kwb">%printer_type</span> <span class="hl opt">= (</span>
    __<span class="hl opt">(</span><span class="hl str">&quot;Local printer&quot;</span><span class="hl opt">)     =&gt;</span> <span class="hl str">&quot;LOCAL&quot;</span><span class="hl opt">,</span>
    __<span class="hl opt">(</span><span class="hl str">&quot;Remote lpd&quot;</span><span class="hl opt">)        =&gt;</span> <span class="hl str">&quot;REMOTE&quot;</span><span class="hl opt">,</span>
    __<span class="hl opt">(</span><span class="hl str">&quot;SMB/Windows 95/98/NT&quot;</span><span class="hl opt">) =&gt;</span> <span class="hl str">&quot;SMB&quot;</span><span class="hl opt">,</span>
    __<span class="hl opt">(</span><span class="hl str">&quot;NetWare&quot;</span><span class="hl opt">)           =&gt;</span> <span class="hl str">&quot;NCP&quot;</span><span class="hl opt">,</span>
<span class="hl opt">);</span>
<span class="hl kwb">%printer_type_inv</span> <span class="hl opt">=</span> <span class="hl kwc">reverse</span> <span class="hl kwb">%printer_type</span><span class="hl opt">;</span>
<span class="hl kwb">$printer_type_default</span> <span class="hl opt">=</span> <span class="hl str">&quot;Local printer&quot;</span><span class="hl opt">;</span>

<span class="hl kwb">%fields</span> <span class="hl opt">= (</span>
    STANDARD <span class="hl opt">=&gt; [</span><span class="hl str">qw(QUEUE SPOOLDIR IF)</span><span class="hl opt">],</span>
    SPEC     <span class="hl opt">=&gt; [</span><span class="hl str">qw(DBENTRY RESOLUTION PAPERSIZE BITSPERPIXEL CRLF)</span><span class="hl opt">],</span>
    LOCAL    <span class="hl opt">=&gt; [</span><span class="hl str">qw(DEVICE)</span><span class="hl opt">],</span>
    REMOTE   <span class="hl opt">=&gt; [</span><span class="hl str">qw(REMOTEHOST REMOTEQUEUE)</span><span class="hl opt">],</span>
    SMB      <span class="hl opt">=&gt; [</span><span class="hl str">qw(SMBHOST SMBHOSTIP SMBSHARE SMBUSER SMBPASSWD SMBWORKGROUP AF)</span><span class="hl opt">],</span>
    NCP      <span class="hl opt">=&gt; [</span><span class="hl str">qw(NCPHOST NCPQUEUE NCPUSER NCPPASSWD)</span><span class="hl opt">],</span>
<span class="hl opt">);</span>
<span class="hl kwb">&#64;papersize_type</span> <span class="hl opt">=</span> <span class="hl str">qw(letter legal ledger a3 a4)</span><span class="hl opt">;</span>
<span class="hl kwb">$spooldir</span>       <span class="hl opt">=</span> <span class="hl str">&quot;/var/spool/lpd&quot;</span><span class="hl opt">;</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl com">=head2 Functions</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>

<span class="hl slc">#-#####################################################################################</span>

<span class="hl kwa">sub</span> set_prefix<span class="hl opt">($) {</span> <span class="hl kwb">$prefix</span> <span class="hl opt">=</span> <span class="hl kwb">$_</span><span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]; }</span>
<span class="hl slc">#-*****************************************************************************</span>
<span class="hl slc">#- read function</span>
<span class="hl slc">#-*****************************************************************************</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#- Read the printer database from dbpath into memory</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwa">sub</span> read_printer_db<span class="hl opt">(;$) {</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$dbpath</span> <span class="hl opt">=</span> <span class="hl kwb">$prefix</span> <span class="hl opt">. (</span><span class="hl kwb">$_</span><span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">] ||</span> <span class="hl kwb">$PRINTER_DB_FILE</span><span class="hl opt">);</span>

    <span class="hl kwb">%thedb</span> <span class="hl kwc">and</span> <span class="hl kwa">return</span><span class="hl opt">;</span>

    <span class="hl kwc">local</span> <span class="hl kwb">$_</span><span class="hl opt">;</span> <span class="hl slc">#- use of while (&lt;...</span>
    <span class="hl kwc">local</span> <span class="hl opt">*</span>DBPATH<span class="hl opt">;</span> <span class="hl slc">#- don&apos;t have to do close ... and don&apos;t modify globals at least</span>
    <span class="hl kwc">open</span> DBPATH<span class="hl opt">,</span> <span class="hl kwb">$dbpath</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;An error has occurred on</span> <span class="hl ipl">$dbpath</span> <span class="hl str">:</span> <span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>

    <span class="hl kwa">while</span> <span class="hl opt">(&lt;</span>DBPATH<span class="hl opt">&gt;) {</span>
	<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwd">/^StartEntry:\s(\w*)/</span><span class="hl opt">) {</span>
	    <span class="hl kwc">my</span> <span class="hl kwb">$entryname</span> <span class="hl opt">=</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	    <span class="hl kwc">my</span> <span class="hl kwb">$entry</span><span class="hl opt">;</span>

	    <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>ENTRY<span class="hl opt">} =</span> <span class="hl kwb">$entryname</span><span class="hl opt">;</span>

	  WHILE <span class="hl opt">:</span>
	      <span class="hl kwa">while</span> <span class="hl opt">(&lt;</span>DBPATH<span class="hl opt">&gt;) {</span>
		SWITCH<span class="hl opt">: {</span>
		      <span class="hl kwd">/GSDriver:\s*(\w*)/</span>      <span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>
		      <span class="hl kwd">/Description:\s*{(.*)}/</span>  <span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>DESCR<span class="hl opt">}    =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>
		      <span class="hl kwd">/About:\s*{(.*)}/</span>        <span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>ABOUT<span class="hl opt">}    =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>
		      <span class="hl kwd">/About:\s*{(.*)/</span>
			<span class="hl kwc">and</span> <span class="hl kwa">do</span>
			  <span class="hl opt">{</span>
			      <span class="hl kwc">my</span> <span class="hl kwb">$string</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$1\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
			      <span class="hl kwa">while</span> <span class="hl opt">(&lt;</span>DBPATH<span class="hl opt">&gt;) {</span>
				  <span class="hl kwd">/(.*)}/</span> <span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>ABOUT<span class="hl opt">} =</span> <span class="hl kwb">$string</span><span class="hl opt">;</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>
				  <span class="hl kwb">$string</span> <span class="hl opt">.=</span> <span class="hl kwb">$_</span><span class="hl opt">;</span>
			      <span class="hl opt">}</span>
			  <span class="hl opt">};</span>
		      <span class="hl kwd">/Resolution:\s*{(.*)}\s*{(.*)}\s*{(.*)}/</span>
			<span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwc">push</span> <span class="hl opt">&#64;{</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>RESOLUTION<span class="hl opt">}}, {</span> XDPI <span class="hl opt">=&gt;</span> <span class="hl kwb">$1,</span> YDPI <span class="hl opt">=&gt;</span> <span class="hl kwb">$2,</span> DESCR <span class="hl opt">=&gt;</span> <span class="hl kwb">$3</span> <span class="hl opt">};</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>
		      <span class="hl kwd">/BitsPerPixel:\s*{(.*)}\s*{(.*)}/</span>
			<span class="hl kwc">and</span> <span class="hl kwa">do</span> <span class="hl opt">{</span> <span class="hl kwc">push</span> <span class="hl opt">&#64;{</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>BITSPERPIXEL<span class="hl opt">}}, {</span>DEPTH <span class="hl opt">=&gt;</span> <span class="hl kwb">$1,</span> DESCR <span class="hl opt">=&gt;</span> <span class="hl kwb">$2</span><span class="hl opt">};</span> <span class="hl kwa">last</span> SWITCH <span class="hl opt">};</span>

		      <span class="hl kwd">/EndEntry/</span> <span class="hl kwc">and</span> <span class="hl kwa">last</span> WHILE<span class="hl opt">;</span>
		  <span class="hl opt">}</span>
	      <span class="hl opt">}</span>
	    <span class="hl kwb">$thedb</span><span class="hl opt">{</span><span class="hl kwb">$entryname</span><span class="hl opt">} =</span> <span class="hl kwb">$entry</span><span class="hl opt">;</span>
	    <span class="hl kwb">$thedb_gsdriver</span><span class="hl opt">{</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}} =</span> <span class="hl kwb">$entry</span><span class="hl opt">;</span>
	<span class="hl opt">}</span>
    <span class="hl opt">}</span>

    <span class="hl kwb">&#64;entries_db_short</span>     <span class="hl opt">=</span> <span class="hl kwc">sort keys</span> <span class="hl kwb">%printer</span><span class="hl opt">::</span>thedb<span class="hl opt">;</span>
    <span class="hl kwb">&#64;entry_db_description</span> <span class="hl opt">=</span> <span class="hl kwc">map</span> <span class="hl opt">{</span> <span class="hl kwb">$printer</span><span class="hl opt">::</span>thedb<span class="hl opt">{</span><span class="hl kwb">$_</span><span class="hl opt">}{</span>DESCR<span class="hl opt">} }</span> <span class="hl kwb">&#64;entries_db_short</span><span class="hl opt">;</span>
    <span class="hl kwb">%descr_to_db</span>          <span class="hl opt">=</span> <span class="hl kwc">map</span> <span class="hl opt">{</span> <span class="hl kwb">$printer</span><span class="hl opt">::</span>thedb<span class="hl opt">{</span><span class="hl kwb">$_</span><span class="hl opt">}{</span>DESCR<span class="hl opt">},</span> <span class="hl kwb">$_</span> <span class="hl opt">}</span> <span class="hl kwb">&#64;entries_db_short</span><span class="hl opt">;</span>
    <span class="hl kwb">%db_to_descr</span>          <span class="hl opt">=</span> <span class="hl kwc">reverse</span> <span class="hl kwb">%descr_to_db</span><span class="hl opt">;</span>

<span class="hl opt">}</span>


<span class="hl slc">#-******************************************************************************</span>
<span class="hl slc">#- write functions</span>
<span class="hl slc">#-******************************************************************************</span>

<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#- given the path queue_path, we create all the required spool directory</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwa">sub</span> create_spool_dir<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$queue_path</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$complete_path</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix/$queue_path</span><span class="hl str">&quot;</span><span class="hl opt">;</span>

    <span class="hl kwa">unless</span> <span class="hl opt">(-</span>d <span class="hl kwb">$complete_path</span><span class="hl opt">) {</span>
	<span class="hl kwc">mkdir</span> <span class="hl str">&quot;</span><span class="hl ipl">$complete_path</span><span class="hl str">&quot;</span><span class="hl opt">,</span> <span class="hl num">0755</span>
	  <span class="hl kwc">or die</span> <span class="hl str">&quot;An error has occurred - can&apos;t create</span> <span class="hl ipl">$complete_path</span> <span class="hl str">:</span> <span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">#-redhat want that &quot;drwxr-xr-x root lp&quot;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$gid_lp</span> <span class="hl opt">= (</span><span class="hl kwc">getpwnam</span><span class="hl opt">(</span><span class="hl str">&quot;lp&quot;</span><span class="hl opt">))[</span><span class="hl num">3</span><span class="hl opt">];</span>
    <span class="hl kwc">chown</span> <span class="hl num">0</span><span class="hl opt">,</span> <span class="hl kwb">$gid_lp, $complete_path</span>
      <span class="hl kwc">or die</span> <span class="hl str">&quot;An error has occurred - can&apos;t chgrp</span> <span class="hl ipl">$complete_path</span> <span class="hl str">to lp</span> <span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
<span class="hl opt">}</span>

<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#-given the input spec file &apos;input&apos;, and the target output file &apos;output&apos;</span>
<span class="hl slc">#-we set the fields specified by fieldname to the values in fieldval</span>
<span class="hl slc">#-nval  is the number of fields to set</span>
<span class="hl slc">#-Doesnt currently catch error exec&apos;ing sed yet</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwa">sub</span> create_config_file<span class="hl opt">(</span><span class="hl kwb">$$%</span><span class="hl opt">) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$inputfile, $outputfile, %toreplace</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    template2file<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">$prefix/$inputfile</span><span class="hl str">&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix/$outputfile</span><span class="hl str">&quot;</span><span class="hl opt">,</span> <span class="hl kwb">%toreplace</span><span class="hl opt">);</span>
<span class="hl opt">}</span>


<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#-copy master filter to the spool dir</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwa">sub</span> copy_master_filter<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$queue_path</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$complete_path</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix/$queue_path/filter</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$master_filter</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix/$PRINTER_FILTER_DIR/master</span><span class="hl str">-filter&quot;</span><span class="hl opt">;</span>

    <span class="hl kwc">eval</span> <span class="hl opt">{</span> commands<span class="hl opt">::</span>cp<span class="hl opt">(</span><span class="hl str">&apos;-f&apos;</span><span class="hl opt">,</span> <span class="hl kwb">$master_filter, $complete_path</span><span class="hl opt">) };</span> <span class="hl slc">#- -f for update.</span>
    <span class="hl kwb">$&#64;</span> <span class="hl kwc">and die</span> <span class="hl str">&quot;Can&apos;t copy</span> <span class="hl ipl">$master_filter</span> <span class="hl str">to</span> <span class="hl ipl">$complete_path</span> <span class="hl str"></span><span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
<span class="hl opt">}</span>

<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#- given a PrintCap Entry, create the spool dir and special</span>
<span class="hl slc">#- rhs-printfilters related config files which are required</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwc">my</span> <span class="hl kwb">$intro_printcap_test</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span>
<span class="hl str">#</span>
<span class="hl str"># Please don&apos;t edit this file directly unless you know what you are doing!</span>
<span class="hl str"># Look at the printcap(5) man page for more info.</span>
<span class="hl str"># Be warned that the control-panel printtool requires a very strict format!</span>
<span class="hl str"># Look at the printcap(5) man page for more info.</span>
<span class="hl str">#</span>
<span class="hl str"># This file can be edited with the printtool in the control-panel.</span>
<span class="hl str">#</span>
<span class="hl str"></span>
<span class="hl str">&quot;</span><span class="hl opt">;</span>

<span class="hl kwa">sub</span> read_configured_queue<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$current</span> <span class="hl opt">=</span> <span class="hl kwc">undef</span><span class="hl opt">;</span>

    <span class="hl slc">#- read /etc/printcap file.</span>
    <span class="hl kwc">local</span> <span class="hl opt">*</span>PRINTCAP<span class="hl opt">;</span> <span class="hl kwc">open</span> PRINTCAP<span class="hl opt">,</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix/etc/printcap</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t open printcap file</span> <span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl kwa">foreach</span> <span class="hl opt">(&lt;</span>PRINTCAP<span class="hl opt">&gt;) {</span>
	<span class="hl kwc">chomp</span><span class="hl opt">;</span>
	<span class="hl kwc">my</span> <span class="hl kwb">$p</span> <span class="hl opt">=</span> <span class="hl str">&apos;(?:\{(.*?)\}|(\S+))&apos;</span><span class="hl opt">;</span>
	<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwd">/^##PRINTTOOL3##\s+$p\s+$p\s+$p\s+$p\s+$p\s+$p\s+$p(?:\s+$p)?/</span><span class="hl opt">) {</span>
	    <span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwb">$current</span><span class="hl opt">) {</span>
		add2hash<span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>configured<span class="hl opt">}{</span><span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>QUEUE<span class="hl opt">}} ||= {},</span> <span class="hl kwb">$current</span><span class="hl opt">);</span>
		<span class="hl kwb">$current</span> <span class="hl opt">=</span> <span class="hl kwc">undef</span><span class="hl opt">;</span>
	    <span class="hl opt">}</span>
	    <span class="hl kwb">$current</span> <span class="hl opt">= {</span>
			TYPE <span class="hl opt">=&gt;</span> <span class="hl kwb">$1</span> <span class="hl opt">||</span> <span class="hl kwb">$2,</span>
			GSDRIVER <span class="hl opt">=&gt;</span> <span class="hl kwb">$3</span> <span class="hl opt">||</span> <span class="hl kwb">$4,</span>
			RESOLUTION <span class="hl opt">=&gt;</span> <span class="hl kwb">$5</span> <span class="hl opt">||</span> <span class="hl kwb">$6,</span>
			PAPERSIZE <span class="hl opt">=&gt;</span> <span class="hl kwb">$7</span> <span class="hl opt">||</span> <span class="hl kwb">$8,</span>
			<span class="hl slc">#- ignored $9 || $10,</span>
			DBENTRY <span class="hl opt">=&gt;</span> <span class="hl kwb">$11</span> <span class="hl opt">||</span> <span class="hl kwb">$12,</span>
			BITSPERPIXEL <span class="hl opt">=&gt;</span> <span class="hl kwb">$13</span> <span class="hl opt">||</span> <span class="hl kwb">$14,</span>
			CRLF <span class="hl opt">=&gt;</span> <span class="hl kwb">$15</span> <span class="hl opt">||</span> <span class="hl kwb">$16,</span>
		       <span class="hl opt">};</span>
	    <span class="hl kwc">print</span> STDERR <span class="hl str">&quot;found printer</span> <span class="hl ipl">$current</span><span class="hl str">-&gt;{QUEUE} of type</span> <span class="hl ipl">$current</span><span class="hl str">-&gt;{TYPE} in</span> <span class="hl ipl">$prefix/etc/printcap\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^([^:]*):\\/</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>QUEUE<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	    <span class="hl kwc">print</span> STDERR <span class="hl str">&quot;found printer</span> <span class="hl ipl">$current</span><span class="hl str">-&gt;{QUEUE} of type</span> <span class="hl ipl">$current</span><span class="hl str">-&gt;{TYPE} in</span> <span class="hl ipl">$prefix/etc/printcap\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s+:sd=([^:]*):\\/</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>SPOOLDIR<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s+:lp=([^:]*):\\/</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>DEVICE<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s+:rm=([^:]*):\\/</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>REMOTEHOST<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s+:rp=([^:]*):\\/</span><span class="hl opt">) {</span>
	    <span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>REMOTEQUEUE<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
	<span class="hl opt">}</span>
    <span class="hl opt">}</span>
    <span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwb">$current</span><span class="hl opt">) {</span>
	add2hash<span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>configured<span class="hl opt">}{</span><span class="hl kwb">$current</span><span class="hl opt">-&gt;{</span>QUEUE<span class="hl opt">}} ||= {},</span> <span class="hl kwb">$current</span><span class="hl opt">);</span>
	<span class="hl kwb">$current</span> <span class="hl opt">=</span> <span class="hl kwc">undef</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl slc">#- get extra parameters for SMB or NCP type queue.</span>
    <span class="hl kwa">foreach</span> <span class="hl opt">(</span><span class="hl kwc">values</span> <span class="hl opt">%{</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>configured<span class="hl opt">}}) {</span>
	<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&apos;SMB&apos;</span><span class="hl opt">) {</span>
	    <span class="hl kwc">my</span> <span class="hl kwb">$config_file</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix$_</span><span class="hl str">-&gt;{SPOOLDIR}/.config&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwc">local</span> <span class="hl opt">*</span>F<span class="hl opt">;</span> <span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl str">&quot;</span><span class="hl ipl">$config_file</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t open</span> <span class="hl ipl">$config_file</span> <span class="hl str"></span><span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwa">foreach</span> <span class="hl opt">(&lt;</span>F<span class="hl opt">&gt;) {</span>
		<span class="hl kwc">chomp</span><span class="hl opt">;</span>
		<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*share=&apos;\\\\(.*?)\\(.*?)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBHOST<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBSHARE<span class="hl opt">} =</span> <span class="hl kwb">$2</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*hostip=(.*)/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBHOSTIP<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*user=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBUSER<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*password=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBPASSWD<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*workgroup=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>SMBWORKGROUP<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span>
	    <span class="hl opt">}</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&apos;NCP&apos;</span><span class="hl opt">) {</span>
	    <span class="hl kwc">my</span> <span class="hl kwb">$config_file</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix$_</span><span class="hl str">-&gt;{SPOOLDIR}/.config&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwc">local</span> <span class="hl opt">*</span>F<span class="hl opt">;</span> <span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl str">&quot;</span><span class="hl ipl">$config_file</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t open</span> <span class="hl ipl">$config_file</span> <span class="hl str"></span><span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwa">foreach</span> <span class="hl opt">(&lt;</span>F<span class="hl opt">&gt;) {</span>
		<span class="hl kwc">chomp</span><span class="hl opt">;</span>
		<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*server=(.*)/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>NCPHOST<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*user=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>NCPUSER<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*password=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>NCPPASSWD<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwd">/^\s*queue=&apos;(.*)&apos;/</span><span class="hl opt">) {</span>
		    <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>NCPQUEUE<span class="hl opt">} =</span> <span class="hl kwb">$1</span><span class="hl opt">;</span>
		<span class="hl opt">}</span>
	    <span class="hl opt">}</span>
	<span class="hl opt">}</span>
    <span class="hl opt">}</span>
<span class="hl opt">}</span>

<span class="hl kwa">sub</span> configure_queue<span class="hl opt">($) {</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>

    <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>SPOOLDIR<span class="hl opt">} ||=</span> <span class="hl str">&quot;</span><span class="hl ipl">$spooldir</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>IF<span class="hl opt">}       ||=</span> <span class="hl str">&quot;</span><span class="hl ipl">$spooldir/$entry</span><span class="hl str">-&gt;{QUEUE}/filter&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>AF<span class="hl opt">}       ||=</span> <span class="hl str">&quot;</span><span class="hl ipl">$spooldir/$entry</span><span class="hl str">-&gt;{QUEUE}/acct&quot;</span><span class="hl opt">;</span>

    <span class="hl kwc">my</span> <span class="hl kwb">$queue_path</span>      <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SPOOLDIR}&quot;</span><span class="hl opt">;</span>
    create_spool_dir<span class="hl opt">(</span><span class="hl kwb">$queue_path</span><span class="hl opt">);</span>

    <span class="hl kwc">my</span> <span class="hl kwb">$get_name_file</span> <span class="hl opt">=</span> <span class="hl kwa">sub</span> <span class="hl opt">{</span>
	<span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$name</span><span class="hl opt">) =</span> <span class="hl kwb">&#64;_</span><span class="hl opt">;</span>
	<span class="hl opt">(</span><span class="hl str">&quot;</span><span class="hl ipl">$PRINTER_FILTER_DIR/$name</span><span class="hl str">.in&quot;</span><span class="hl opt">,</span> <span class="hl str">&quot;</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SPOOLDIR}/</span><span class="hl ipl">$name</span><span class="hl str">&quot;</span><span class="hl opt">)</span>
    <span class="hl opt">};</span>
    <span class="hl kwc">my</span> <span class="hl opt">(</span><span class="hl kwb">$filein, $file</span><span class="hl opt">);</span>
    <span class="hl kwc">my</span> <span class="hl kwb">%fieldname</span> <span class="hl opt">= ();</span>
    <span class="hl kwc">my</span> <span class="hl kwb">$dbentry</span> <span class="hl opt">=</span> <span class="hl kwb">$thedb</span><span class="hl opt">{(</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>DBENTRY<span class="hl opt">})}</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;no dbentry&quot;</span><span class="hl opt">;</span>


    <span class="hl opt">(</span><span class="hl kwb">$filein, $file</span><span class="hl opt">) = &amp;</span><span class="hl kwb">$get_name_file</span><span class="hl opt">(</span><span class="hl str">&quot;general.cfg&quot;</span><span class="hl opt">);</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>ascps_trans<span class="hl opt">} = (</span><span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;POSTSCRIPT&quot;</span><span class="hl opt">)</span> ? <span class="hl str">&quot;YES&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;NO&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>desiredto<span class="hl opt">}   = (</span><span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}</span> <span class="hl kwc">ne</span> <span class="hl str">&quot;TEXT&quot;</span><span class="hl opt">)</span> ? <span class="hl str">&quot;ps&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;asc&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>papersize<span class="hl opt">}   =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>PAPERSIZE<span class="hl opt">}</span> ? <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>PAPERSIZE<span class="hl opt">} :</span> <span class="hl str">&quot;letter&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>printertype<span class="hl opt">} =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">};</span>
    create_config_file<span class="hl opt">(</span><span class="hl kwb">$filein, $file, %fieldname</span><span class="hl opt">);</span>

    <span class="hl slc">#- successfully created general.cfg, now do postscript.cfg</span>
    <span class="hl opt">(</span><span class="hl kwb">$filein, $file</span><span class="hl opt">) = &amp;</span><span class="hl kwb">$get_name_file</span><span class="hl opt">(</span><span class="hl str">&quot;postscript.cfg&quot;</span><span class="hl opt">);</span>
    <span class="hl kwb">%fieldname</span> <span class="hl opt">= ();</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>gsdevice<span class="hl opt">}       =</span> <span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">};</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>papersize<span class="hl opt">}      =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>PAPERSIZE<span class="hl opt">}</span> ? <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>PAPERSIZE<span class="hl opt">} :</span> <span class="hl str">&quot;letter&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>resolution<span class="hl opt">}     =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>RESOLUTION<span class="hl opt">};</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>color<span class="hl opt">}          =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>BITSPERPIXEL<span class="hl opt">}</span> <span class="hl kwc">ne</span> <span class="hl str">&quot;Default&quot;</span> <span class="hl opt">&amp;&amp;</span>
      <span class="hl opt">((</span><span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}</span> <span class="hl kwc">ne</span> <span class="hl str">&quot;uniprint&quot;</span> <span class="hl opt">&amp;&amp;</span> <span class="hl str">&quot;-dBitsPerPixel=&quot;</span><span class="hl opt">) .</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>BITSPERPIXEL<span class="hl opt">});</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>reversepages<span class="hl opt">}   =</span> <span class="hl str">&quot;NO&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>extragsoptions<span class="hl opt">} =</span> <span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>pssendeof<span class="hl opt">}      =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>AUTOSENDEOF<span class="hl opt">}</span> ? <span class="hl opt">(</span><span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;POSTSCRIPT&quot;</span> ? <span class="hl str">&quot;YES&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;NO&quot;</span><span class="hl opt">) :</span> <span class="hl str">&quot;NO&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>nup<span class="hl opt">}            =</span> <span class="hl str">&quot;1&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>rtlftmar<span class="hl opt">}       =</span> <span class="hl str">&quot;18&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>topbotmar<span class="hl opt">}      =</span> <span class="hl str">&quot;18&quot;</span><span class="hl opt">;</span>
    create_config_file<span class="hl opt">(</span><span class="hl kwb">$filein, $file, %fieldname</span><span class="hl opt">);</span>

    <span class="hl slc">#- finally, make textonly.cfg</span>
    <span class="hl opt">(</span><span class="hl kwb">$filein, $file</span><span class="hl opt">) = &amp;</span><span class="hl kwb">$get_name_file</span><span class="hl opt">(</span><span class="hl str">&quot;textonly.cfg&quot;</span><span class="hl opt">);</span>
    <span class="hl kwb">%fieldname</span> <span class="hl opt">= ();</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>textonlyoptions<span class="hl opt">} =</span> <span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>crlftrans<span class="hl opt">}       =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>CRLF<span class="hl opt">}</span> ? <span class="hl str">&quot;YES&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>
    <span class="hl kwb">$fieldname</span><span class="hl opt">{</span>textsendeof<span class="hl opt">}     =</span> <span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>AUTOSENDEOF<span class="hl opt">}</span> ? <span class="hl opt">(</span><span class="hl kwb">$dbentry</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;POSTSCRIPT&quot;</span> ? <span class="hl str">&quot;NO&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;YES&quot;</span><span class="hl opt">) :</span> <span class="hl str">&quot;NO&quot;</span><span class="hl opt">;</span>
    create_config_file<span class="hl opt">(</span><span class="hl kwb">$filein, $file, %fieldname</span><span class="hl opt">);</span>

    <span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;SMB&quot;</span><span class="hl opt">) {</span>
	<span class="hl slc">#- simple config file required if SMB printer</span>
	<span class="hl kwc">my</span> <span class="hl kwb">$config_file</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix$queue_path/</span><span class="hl str">.config&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">local</span> <span class="hl opt">*</span>F<span class="hl opt">;</span>
	<span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl str">&quot;&gt;</span><span class="hl ipl">$config_file</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t create</span> <span class="hl ipl">$config_file</span> <span class="hl str"></span><span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;share=&apos;</span><span class="hl esc">\\\\</span><span class="hl str"></span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBHOST}</span><span class="hl esc">\\</span><span class="hl str"></span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBSHARE}&apos;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;hostip=</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBHOSTIP}</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;user=&apos;</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBUSER}&apos;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;password=&apos;</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBPASSWD}&apos;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;workgroup=&apos;</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{SMBWORKGROUP}&apos;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;NCP&quot;</span><span class="hl opt">) {</span>
	<span class="hl slc">#- same for NCP printer</span>
	<span class="hl kwc">my</span> <span class="hl kwb">$config_file</span> <span class="hl opt">=</span> <span class="hl str">&quot;</span><span class="hl ipl">$prefix$queue_path/</span><span class="hl str">.config&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">local</span> <span class="hl opt">*</span>F<span class="hl opt">;</span>
	<span class="hl kwc">open</span> F<span class="hl opt">,</span> <span class="hl str">&quot;&gt;</span><span class="hl ipl">$config_file</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t create</span> <span class="hl ipl">$config_file</span> <span class="hl str"></span><span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;server=</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{NCPHOST}</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;queue=</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{NCPQUEUE}</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;user=</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{NCPUSER}</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> F <span class="hl str">&quot;password=</span><span class="hl ipl">$entry</span><span class="hl str">-&gt;{NCPPASSWD}</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

    copy_master_filter<span class="hl opt">(</span><span class="hl kwb">$queue_path</span><span class="hl opt">);</span>

    <span class="hl slc">#-now the printcap file, note this one contains all the printer (use configured for that).</span>
    <span class="hl kwc">local</span> <span class="hl opt">*</span>PRINTCAP<span class="hl opt">;</span>
    <span class="hl kwa">if</span> <span class="hl opt">($::</span>testing<span class="hl opt">) {</span>
	<span class="hl opt">*</span>PRINTCAP <span class="hl opt">= *</span>STDOUT<span class="hl opt">;</span>
    <span class="hl opt">}</span> <span class="hl kwa">else</span> <span class="hl opt">{</span>
	<span class="hl kwc">open</span> PRINTCAP<span class="hl opt">,</span> <span class="hl str">&quot;&gt;</span><span class="hl ipl">$prefix/etc/printcap</span><span class="hl str">&quot;</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;Can&apos;t open printcap file</span> <span class="hl ipl">$!</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>

    <span class="hl kwc">print</span> PRINTCAP <span class="hl kwb">$intro_printcap_test</span><span class="hl opt">;</span>
    <span class="hl kwa">foreach</span> <span class="hl opt">(</span><span class="hl kwc">values</span> <span class="hl opt">%{</span><span class="hl kwb">$entry</span><span class="hl opt">-&gt;{</span>configured<span class="hl opt">}}) {</span>
	<span class="hl kwc">my</span> <span class="hl kwb">$db_</span> <span class="hl opt">=</span> <span class="hl kwb">$thedb</span><span class="hl opt">{(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>DBENTRY<span class="hl opt">})}</span> <span class="hl kwc">or die</span> <span class="hl str">&quot;no dbentry&quot;</span><span class="hl opt">;</span>

	<span class="hl kwc">printf</span> PRINTCAP <span class="hl str">&quot;##PRINTTOOL3##</span>  <span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s</span> <span class="hl str"></span><span class="hl ipl">%s%s\n</span><span class="hl str">&quot;</span><span class="hl opt">,</span>
	  <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
	    <span class="hl kwb">$db_</span><span class="hl opt">-&gt;{</span>GSDRIVER<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
	      <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>RESOLUTION<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
		<span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>PAPERSIZE<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
		  <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
		    <span class="hl kwb">$db_</span><span class="hl opt">-&gt;{</span>ENTRY<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
		      <span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>BITSPERPIXEL<span class="hl opt">} ||</span> <span class="hl str">&apos;{}&apos;</span><span class="hl opt">,</span>
			<span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>CRLF<span class="hl opt">}</span> ? <span class="hl str">&quot; 1&quot;</span> <span class="hl opt">:</span> <span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>

	<span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{QUEUE}:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:sd=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{SPOOLDIR}:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:mx#0:</span><span class="hl esc">\\\n\t</span><span class="hl str">:sh:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>

	<span class="hl kwa">if</span> <span class="hl opt">(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;LOCAL&quot;</span><span class="hl opt">) {</span>
	    <span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:lp=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{DEVICE}:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">elsif</span> <span class="hl opt">(</span><span class="hl kwb">$_</span><span class="hl opt">-&gt;{</span>TYPE<span class="hl opt">}</span> <span class="hl kwc">eq</span> <span class="hl str">&quot;REMOTE&quot;</span><span class="hl opt">) {</span>
	    <span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:rm=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{REMOTEHOST}:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:rp=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{REMOTEQUEUE}:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl opt">}</span> <span class="hl kwa">else</span> <span class="hl opt">{</span>
	    <span class="hl slc">#- (pcentry-&gt;Type == (PRINTER_SMB | PRINTER_NCP))</span>
	    <span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:lp=/dev/null:</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	    <span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:af=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{SPOOLDIR}/acct</span><span class="hl esc">\\\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl opt">}</span>

	<span class="hl slc">#- cheating to get the input filter!</span>
	<span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\t</span><span class="hl str">:if=</span><span class="hl ipl">$_</span><span class="hl str">-&gt;{SPOOLDIR}/filter:</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
	<span class="hl kwc">print</span> PRINTCAP <span class="hl str">&quot;</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl opt">}</span>
<span class="hl opt">}</span>


<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#- interface function</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>

<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl slc">#- fonction de test</span>
<span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl kwa">sub</span> test <span class="hl opt">{</span>
    <span class="hl opt">$::</span>testing <span class="hl opt">=</span> <span class="hl num">1</span><span class="hl opt">;</span>
    <span class="hl kwb">$printer</span><span class="hl opt">::</span>prefix<span class="hl opt">=</span><span class="hl str">&quot;&quot;</span><span class="hl opt">;</span>

    read_printer_db<span class="hl opt">();</span>

    <span class="hl kwc">print</span> <span class="hl str">&quot;the dump</span><span class="hl esc">\n</span><span class="hl str">&quot;</span><span class="hl opt">;</span>
    <span class="hl kwc">print</span> Dumper<span class="hl opt">(</span><span class="hl kwb">%thedb</span><span class="hl opt">);</span>


    <span class="hl slc">#</span>
    <span class="hl slc">#eval { printer::create_spool_dir(&quot;/tmp/titi/&quot;, &quot;.&quot;) };</span>
    <span class="hl slc">#print $&#64;;</span>
    <span class="hl slc">#eval { printer::copy_master_filter(&quot;/tmp/titi/&quot;, &quot;.&quot;) };</span>
    <span class="hl slc">#print $&#64;;</span>
    <span class="hl slc">#</span>
    <span class="hl slc">#</span>
    <span class="hl slc">#eval { printer::create_config_file(&quot;files/postscript.cfg.in&quot;, &quot;files/postscript.cfg&quot;,&quot;./&quot;,</span>
    <span class="hl slc">#				    (</span>
    <span class="hl slc">#				     gsdevice   =&gt; &quot;titi&quot;,</span>
    <span class="hl slc">#				     resolution =&gt; &quot;tata&quot;,</span>
    <span class="hl slc">#				    ));</span>
    <span class="hl slc">#   };</span>
    <span class="hl slc">#print $&#64;;</span>
    <span class="hl slc">#</span>
    <span class="hl slc">#</span>
    <span class="hl slc">#</span>
    <span class="hl slc">#printer::configure_queue(\%printer::ex_printcap_entry, &quot;/&quot;);</span>
<span class="hl opt">}</span>

<span class="hl slc">#-######################################################################################</span>
<span class="hl slc">#- Wonderful perl :(</span>
<span class="hl slc">#-######################################################################################</span>
<span class="hl num">1</span><span class="hl opt">;</span> <span class="hl slc">#</span>

<span class="hl com">=head1 AUTHOR</span>
<span class="hl com"></span>
<span class="hl com">pad.</span>
<span class="hl com"></span>
<span class="hl com">=cut</span>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.1</a> (<a href='https://git-scm.com/'>git 2.21.0</a>) at 2025-03-11 21:34:58 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>