aboutsummaryrefslogtreecommitdiff
path: root/ikiwiki-update-wikilist
blob: 56d6e07319094dcc77d8fea283bcdf4b0b0b734d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -t
# Add a user to the system wide wikilist.
# This script can safely be made suid or put in /etc/sudoers.
use warnings;
use strict;
use English;

my $remove=(@ARGV && $ARGV[0] eq '-r');

my $username=getpwuid($REAL_USER_ID);
if (! defined $username || ! length $username) {
	die "unable to determine user name for UID $REAL_USER_ID\n";
}

my $wikilist="/etc/ikiwiki/wikilist";
if (! -e $wikilist) {
	die "$wikilist does not exist\n";
}

my $changed=0;
my $seen=0;
my @lines;
open (my $list, "<$wikilist") || die "read $wikilist: $!";
while (<$list>) {
	chomp;
	if (/^\s*([^\s]+)\s*$/) {
		my $user=$1;
		if ($user eq $username) {
			if (! $remove) {
				$seen=1;
				push @lines, $_;
			}
			else {
				$changed=1;
			}
		}
		else {
			push @lines, $_;
		}
	}
	else {
		push @lines, $_;
	}
}
if (! $seen && ! $remove) {
	push @lines, $username;
	$changed=1;
}
if ($changed) {
	close $list || die "ikiwiki-update-wikilist: error reading $list: $!\n";
	open ($list, ">$wikilist") || die "ikiwiki-update-wikilist: cannot write to $wikilist\n";
	foreach (@lines) {
		print $list "$_\n";
	}
	if ($remove) {
		print "ikiwiki-update-wikilist: removed user $username from $wikilist\n";
	}
	else {
		print "ikiwiki-update-wikilist: added user $username to $wikilist\n";
	}
	close $list || die "ikiwiki-update-wikilist: error writing $wikilist: $!\n";
}
else {
	print "ikiwiki-update-wikilist: no changes need to be made\n";
}