From 4796acdae76a294199e8d8152c3c9ed53db808a2 Mon Sep 17 00:00:00 2001
From: joey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>
Date: Mon, 13 Mar 2006 18:35:23 +0000
Subject: [PATCH] add --setup, --wrappermode allow filename param to --wrapper

---
 doc/bugs.mdwn  |  6 -----
 doc/setup.mdwn |  2 +-
 doc/todo.mdwn  |  7 +++++-
 doc/usage.mdwn | 33 ++++++++++++++++++++-----
 ikiwiki        | 66 +++++++++++++++++++++++++++++++++++++-------------
 ikiwiki.setup  | 39 +++++++++++++++++++++++++++++
 6 files changed, 122 insertions(+), 31 deletions(-)
 create mode 100644 ikiwiki.setup

diff --git a/doc/bugs.mdwn b/doc/bugs.mdwn
index 72719ba29..2c712de6d 100644
--- a/doc/bugs.mdwn
+++ b/doc/bugs.mdwn
@@ -27,9 +27,3 @@
   markdown, but that will probably be a bear. I guess the question is how
   common "[[ ]]" is, and maybe we should just provide a way to escape a
   wikilink..
-
-Fixed bugs:
-
-* RecentChanges is supposed to linkify WikiNames and it does, but only if
-  the user's page exists. It doesn't add a ?link to a noneistant page to
-  aid creating it. (Fixed. -- [[Joey]])
diff --git a/doc/setup.mdwn b/doc/setup.mdwn
index acb401cfc..541131cf9 100644
--- a/doc/setup.mdwn
+++ b/doc/setup.mdwn
@@ -74,4 +74,4 @@ optional support for commits from the web.
 
                 --historyurl='http://svn.host/trunk/[[]]?root=wiki'
 
-11. Enjoy your new wiki! Add yourself to [[IkiWikiUsers]]
\ No newline at end of file
+11. Enjoy your new wiki! Add yourself to [[IkiWikiUsers]]
diff --git a/doc/todo.mdwn b/doc/todo.mdwn
index 98b98e3f2..23ade8cbd 100644
--- a/doc/todo.mdwn
+++ b/doc/todo.mdwn
@@ -15,7 +15,7 @@ is built. (As long as all changes to all pages is ok.)
 
 * Should support mail notification of new and changed pages.
 
-  Hmm, should be easy to implement this.. it runs as a svn post-cookit hook
+  Hmm, should be easy to implement this.. it runs as a svn post-coommit hook
   already, so just look at the userdb, svnlook at what's changed, and send
   mails to people who have subscribed.
 
@@ -62,6 +62,11 @@ and that includes the date.
 What syntax do other wikis use for this? I'm considering "[[ -- ]]" (with
 spaces removed) as it has a nice nmemonic.
 
+OTOH, adding additional syntax for this would be counter to one of the
+design goals for ikiwiki: keeping as much markup as possible out of the
+wiki and not adding nonstandard markup. And it's not significantly hard to
+type "--[[Joey]]", and as to the date, we do have page history.
+
 ## recentchanges links to commit diffs
 
 Would take a bit more viewcvs integration, let the be a "[diff]" link in
diff --git a/doc/usage.mdwn b/doc/usage.mdwn
index 8f57a1593..8efb9a63d 100644
--- a/doc/usage.mdwn
+++ b/doc/usage.mdwn
@@ -6,13 +6,18 @@ ikiwiki - a wiki compiler
 
 ikiwiki [options] source templates destination
 
+ikiwiki --setup configfile
+
 # DESCRIPTION
 
-`ikiwiki` is a wiki compiler. It builds static html pages for a wiki, from `source` in the [[MarkDown]] language, using the specified html `templates` and writes it out to `destination`.
+`ikiwiki` is a wiki compiler. It builds static html pages for a wiki, from
+`source` in the [[MarkDown]] language, using the specified html `templates`
+and writes it out to `destination`.
 
 # OPTIONS
 
-Note that most options can be shortened to single letters, and boolean flags such as --verbose can be negated with --no-verbose.
+Note that most options can be shortened to single letters, and boolean
+flags such as --verbose can be negated with --no-verbose.
 
 * --wikiname
 
@@ -26,13 +31,22 @@ Note that most options can be shortened to single letters, and boolean flags suc
 
   Force a rebuild of all pages.
 
-* --wrapper
+* --wrapper [file]
+
+  Generate a [[wrapper]] binary that is hardcoded to do action specified by
+  the other options, using the specified input files and `destination`
+  directory. The filename to use for the wrapper is optional.
+
+  The wrapper is designed to be safely made suid and be run by untrusted
+  users, as a [[Subversion]] [[post-commit]] hook, or as a [[CGI]].
 
-  Generate a [[wrapper]] binary that is hardcoded to do action specified by the other options, using the specified input files and `destination` directory.
+  Note that the generated wrapper will ignore all command line parameters
+  except for --params, which will make it print out the parameters it would
+  run ikiwiki with.
 
-  The wrapper is designed to be safely made suid and be run by untrusted users, as a [[Subversion]] [[post-commit]] hook, or as a [[CGI]].
+* --wrappermode mode
 
-  Note that the generated wrapper will ignore all command line parameters except for --params, which will make it print out the parameters it would run ikiwiki with.
+  Specify a mode to chmod the wrapper to after creating it.
 
 * --svn, --nosvn
 
@@ -71,6 +85,13 @@ Note that most options can be shortened to single letters, and boolean flags suc
   Specifies a rexexp of source files to exclude from processing.
   May be specified multiple times to add to exclude list.
 
+* --setup configfile
+
+  In setup mode, ikiwiki reads the config file, which is really a perl
+  program that can call ikiwiki internal functions. Uses of this are
+  various; one is to automatically generate wrappers for a wiki based on
+  data in a config file.
+
 # AUTHOR
 
 Joey Hess <joey@kitenet.net>
diff --git a/ikiwiki b/ikiwiki
index 361c75c07..4e64e4031 100755
--- a/ikiwiki
+++ b/ikiwiki
@@ -18,23 +18,27 @@ my %config=( #{{{
 	wikiname => "wiki",
 	default_pageext => ".mdwn",
 	cgi => 0,
-	url => "",
-	cgiurl => "",
-	historyurl => "",
 	svn => 1,
+	url => '',
+	cgiurl => '',
+	historyurl => '',
 	anonok => 0,
 	rebuild => 0,
-	wrapper => 0,
+	wrapper => undef,
+	wrappermode => undef,
 	srcdir => undef,
 	destdir => undef,
 	templatedir => undef,
+	setup => undef,
 ); #}}}
 
 GetOptions( #{{{
+	"setup=s" => \$config{setup},
 	"wikiname=s" => \$config{wikiname},
 	"verbose|v!" => \$config{verbose},
 	"rebuild!" => \$config{rebuild},
-	"wrapper!" => \$config{wrapper},
+	"wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
+	"wrappermode=i" => \$config{wrappermode},
 	"svn!" => \$config{svn},
 	"anonok!" => \$config{anonok},
 	"cgi!" => \$config{cgi},
@@ -46,13 +50,16 @@ GetOptions( #{{{
 	},
 ) || usage();
 
-usage() unless @ARGV == 3;
-$config{srcdir} = possibly_foolish_untaint(shift);
-$config{templatedir} = possibly_foolish_untaint(shift);
-$config{destdir} = possibly_foolish_untaint(shift);
-if ($config{cgi} && ! length $config{url}) {
-	error("Must specify url to wiki with --url when using --cgi");
-} #}}}
+if (! $config{setup}) {
+	usage() unless @ARGV == 3;
+	$config{srcdir} = possibly_foolish_untaint(shift);
+	$config{templatedir} = possibly_foolish_untaint(shift);
+	$config{destdir} = possibly_foolish_untaint(shift);
+	if ($config{cgi} && ! length $config{url}) {
+		error("Must specify url to wiki with --url when using --cgi");
+	}
+}
+#}}}
 
 sub usage { #{{{
 	die "usage: ikiwiki [options] source templates dest\n";
@@ -659,7 +666,8 @@ FILE:		foreach my $file (@files) {
 	}
 } #}}}
 
-sub gen_wrapper () { #{{{
+sub gen_wrapper (@) { #{{{
+	my %config=(@_);
 	eval q{use Cwd 'abs_path'};
 	$config{srcdir}=abs_path($config{srcdir});
 	$config{destdir}=abs_path($config{destdir});
@@ -668,6 +676,10 @@ sub gen_wrapper () { #{{{
 		error("$this doesn't seem to be executable");
 	}
 
+	if ($config{setup}) {
+		error("cannot create a wrapper that uses a setup file");
+	}
+	
 	my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
 		"--wikiname=$config{wikiname}");
 	push @params, "--verbose" if $config{verbose};
@@ -729,12 +741,15 @@ $envsave
 }
 EOF
 	close OUT;
-	if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
+	if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
 		error("failed to compile ikiwiki-wrap.c");
 	}
 	unlink("ikiwiki-wrap.c");
-	print "successfully generated ikiwiki-wrap\n";
-	exit 0;
+	if (defined $config{wrappermode} &&
+	    ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
+		error("chmod $config{wrapper}: $!");
+	}
+	print "successfully generated $config{wrapper}\n";
 } #}}}
 		
 sub misctemplate ($$) { #{{{
@@ -1133,8 +1148,25 @@ sub cgi () { #{{{
 	}
 } #}}}
 
+sub setup () { # {{{
+	my $setup=possibly_foolish_untaint($config{setup});
+	open (IN, $setup) || error("read $setup: $!\n");
+	local $/=undef;
+	my $code=<IN>;
+	($code)=$code=~/(.*)/s;
+	close IN;
+	eval $code;
+	error($@) if $@;
+	print "ikiwiki setup complete\n";
+	exit;
+} #}}}
+
 # main {{{
-gen_wrapper() if $config{wrapper};
+setup() if $config{setup};
+if ($config{wrapper}) {
+	gen_wrapper(%config);
+	exit;
+}
 memoize('pagename');
 memoize('bestlink');
 loadindex() unless $config{rebuild};
diff --git a/ikiwiki.setup b/ikiwiki.setup
new file mode 100644
index 000000000..44c4f3106
--- /dev/null
+++ b/ikiwiki.setup
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+# Configuration file for ikiwiki.
+# Passing this to ikiwiki --setup will make ikiwiki generate tw
+# wrapper programs, one for cgi and one for a subversion post-commit hook.
+#
+# Remember to re-run ikiwiki --setup any time you edit this file.
+
+my %common=(
+	wikiname => "MyWiki",
+
+	# Be sure to customise these..
+	srcdir => "/path/to/source",
+	templatedir => "/path/to/templates",
+	destdir => "/var/www/wiki",
+	url => "http://myhost/wiki",
+	cgiurl => "http://myhost/ikiwiki.cgi",
+	historyurl => "$webdir/cgi-bin/viewcvs?[[]]"
+	
+	# Whether to integrate with svn.
+	svn => 1,
+	svnrepo => "/svn/wiki",
+	
+	# Can anonymous web users edit pages?
+	#anonok => 1,
+);	
+
+gen_wrapper(
+	%common,
+	cgi => 1,
+	wrapper => "$common{destdir}/ikiwiki.cgi",
+	wrappermode => 06755,
+);
+gen_wrapper(
+	%common,
+	# Note that this will overwrite any exsting post-commit hoo
+	# script, which may not be what you want.
+	wrapper => "$common{svnrepo}/hooks/post-commit",
+	wrappermode => 04755,
+) if $common{$svn};
-- 
2.39.5