add TAILS
[ikiwiki.git] / ikiwiki-update-wikilist
bloba6425f011c6a7e04c281cdfe8f70094d302818e5
1 #!/usr/bin/perl -t
2 # Add a user to the system wide wikilist.
3 # This script can safely be made suid.
4 use warnings;
5 use strict;
6 use English;
8 my $remove=(@ARGV && $ARGV[0] eq '-r');
10 my $username=getpwuid($REAL_USER_ID);
11 if (! defined $username || ! length $username) {
12 die "unable to determine user name for UID $REAL_USER_ID\n";
15 my $wikilist="/etc/ikiwiki/wikilist";
16 if (! -e $wikilist) {
17 die "$wikilist does not exist\n";
20 my $changed=0;
21 my $seen=0;
22 my @lines;
23 open (my $list, "<$wikilist") || die "read $wikilist: $!";
24 while (<$list>) {
25 chomp;
26 if (/^\s*([^\s]+)\s*$/) {
27 my $user=$1;
28 if ($user eq $username) {
29 if (! $remove) {
30 $seen=1;
31 push @lines, $_;
34 else {
35 push @lines, $_;
38 else {
39 push @lines, $_;
42 if (! $seen && ! $remove) {
43 push @lines, $username;
44 $changed=1;
46 if ($changed) {
47 close $list || die "ikiwiki-update-wikilist: error reading $list: $!\n";
48 open ($list, ">$wikilist") || die "ikiwiki-update-wikilist: cannot write to $wikilist\n";
49 foreach (@lines) {
50 print $list "$_\n";
52 if ($remove) {
53 print "ikiwiki-update-wikilist: removed user $username from $wikilist\n";
55 else {
56 print "ikiwiki-update-wikilist: added user $username to $wikilist\n";
58 close $list || die "ikiwiki-update-wikilist: error writing $wikilist: $!\n";
60 else {
61 print "ikiwiki-update-wikilist: no changes need to be made\n";