summaryrefslogtreecommitdiff
path: root/ikiwiki-update-wikilist
blob: a6425f011c6a7e04c281cdfe8f70094d302818e5 (plain)
  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;
  7. my $remove=(@ARGV && $ARGV[0] eq '-r');
  8. my $username=getpwuid($REAL_USER_ID);
  9. if (! defined $username || ! length $username) {
  10. die "unable to determine user name for UID $REAL_USER_ID\n";
  11. }
  12. my $wikilist="/etc/ikiwiki/wikilist";
  13. if (! -e $wikilist) {
  14. die "$wikilist does not exist\n";
  15. }
  16. my $changed=0;
  17. my $seen=0;
  18. my @lines;
  19. open (my $list, "<$wikilist") || die "read $wikilist: $!";
  20. while (<$list>) {
  21. chomp;
  22. if (/^\s*([^\s]+)\s*$/) {
  23. my $user=$1;
  24. if ($user eq $username) {
  25. if (! $remove) {
  26. $seen=1;
  27. push @lines, $_;
  28. }
  29. }
  30. else {
  31. push @lines, $_;
  32. }
  33. }
  34. else {
  35. push @lines, $_;
  36. }
  37. }
  38. if (! $seen && ! $remove) {
  39. push @lines, $username;
  40. $changed=1;
  41. }
  42. if ($changed) {
  43. close $list || die "ikiwiki-update-wikilist: error reading $list: $!\n";
  44. open ($list, ">$wikilist") || die "ikiwiki-update-wikilist: cannot write to $wikilist\n";
  45. foreach (@lines) {
  46. print $list "$_\n";
  47. }
  48. if ($remove) {
  49. print "ikiwiki-update-wikilist: removed user $username from $wikilist\n";
  50. }
  51. else {
  52. print "ikiwiki-update-wikilist: added user $username to $wikilist\n";
  53. }
  54. close $list || die "ikiwiki-update-wikilist: error writing $wikilist: $!\n";
  55. }
  56. else {
  57. print "ikiwiki-update-wikilist: no changes need to be made\n";
  58. }