- open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
- error("write $estdir/$cgi.tmpl: $!");
- print TEMPLATE misctemplate("search",
- "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
- close TEMPLATE;
- open(TEMPLATE, ">$estdir/$cgi.conf") ||
- error("write $estdir/$cgi.conf: $!");
+
+ my $newfile="$estdir/$cgi.tmpl.new";
+ my $cleanup = sub { unlink($newfile) };
+ open(TEMPLATE, ">:utf8", $newfile) || error("open $newfile: $!", $cleanup);
+ print TEMPLATE IkiWiki::misctemplate("search",
+ "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
+ baseurl => IkiWiki::dirname($config{cgiurl})."/") ||
+ error("write $newfile: $!", $cleanup);
+ close TEMPLATE || error("save $newfile: $!", $cleanup);
+ rename($newfile, "$estdir/$cgi.tmpl") ||
+ error("rename $newfile: $!", $cleanup);
+
+ $newfile="$estdir/$cgi.conf";
+ open(TEMPLATE, ">$newfile") || error("open $newfile: $!", $cleanup);