#!/usr/bin/perl

# Adds/removes the PDQ panic button to/from the KDE desktops of all users
# (~/Desktop directory)
# (C) 2001 Till Kamppeter, GPL (www.gnu.org)

use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");
GetOptions("add"      => \$opt_add,   # Add the panic button
           "remove"   => \$opt_rm);   # Remove it

if (!$opt_add && !$opt_rm) { # HEPLP!
    print "Usage: panicbutton --add
       panicbutton --remove
";
    exit;
}

open PASSWD, "< /etc/passwd" || die "Cannot read /etc/passwd!\n";
while (<PASSWD>) {
    chomp;
    if ($_ =~ /^([^:]+):[^:]*:([^:]+):([^:]+):[^:]*:([^:]+):[^:]*$/) {
	my ($username, $uid, $gid, $homedir) = ($1, $2, $3, $4);
	if ($uid >= 500) {
	    if ($opt_add) {
		if (!-d "$homedir/Desktop") {mkdir("$homedir/Desktop/");}
		if (open PANICBUTTON, "> $homedir/Desktop/PDQ_Panic.desktop") {
		    print PANICBUTTON "[Desktop Entry]
Comment[C]=
Exec=/usr/bin/killpdq
Icon=error
MimeType=
Name[C]=Stop Printer!
Path=
ServiceTypes=
SwallowExec=
SwallowTitle=
Terminal=false
TerminalOptions=
Type=Application
X-KDE-SubstituteUID=false
X-KDE-Username=
";
		    close PANICBUTTON;
		    chown($uid, $gid, "$homedir/Desktop/PDQ_Panic.desktop");
		} else {
		    warn "Cannot install panic button for the user \"$username\"!\n";
		}
	    } elsif ($opt_rm) {
		if (-f "$homedir/Desktop/PDQ_Panic.desktop") {
		    unlink("$homedir/Desktop/PDQ_Panic.desktop") ||
			warn "Cannot remove panic button of the user \"$username\"!\n";
		}
	    }
	}
    }
}
close PASSWD;
