# How to use:
# ($ = normal user prompt, # = root prompt)
#
# 1. get converter code
#  $ mkdir ~/bottoms; cd ~/bottoms
#  $ git clone https://github.com/pflanze/ml2json.git
#
# 2a. get dependencies available in Debian:
#  # apt-get install libhtml-parser-perl liburi-perl libhtml-tree-perl libtext-markdown-perl
#
# 2b. get dependencies available in CPAN:
#  # cpan
#  cpan> install Function::Parameters
#
# 2c. get dependencies available from my Github account:
#  # cd /opt
#  # mkdir chj
#  # cd chj
#  # git clone https://github.com/pflanze/chj-perllib.git perllib
#
# 3. get wiki:
#  $ cd ~/bottoms
#  $ git clone https://github.com/pflanze/bottoms.wiki.git
#
# 4. run converter:
#  $ mkdir -p ~/tmp/bottoms.wiki.out/
#  $ cd ml2json
# -check that bottoms-cfg.pl and logo.pl do not contain evil code!- then:
#  $ html/gen ../bottoms.wiki/bottoms-cfg.pl ../bottoms.wiki/ ~/tmp/bottoms.wiki.out/
#
#  Then open your browser on ~/tmp/bottoms.wiki.out/
#
#  I'm using my loop-filechange script (*) to run the conversion
#  whenever the wiki directory changes its modification time:
#  $ loop-filechange 'html/gen ../bottoms.wiki/bottoms-cfg.pl ../bottoms.wiki/ ~/tmp/bottoms.wiki.out/' ../bottoms.wiki/
#
#  (*) install it with:
#  # cd /opt/chj/; 
#  # git clone https://github.com/pflanze/chj-bin.git bin
#  $ PATH="$PATH:/opt/chj/bin" # put into .bash_profile for persistence


use strict; use warnings FATAL => 'uninitialized';
use Function::Parameters qw(:strict);

# XX move to lib
use Chj::xperlfunc 'dirname';
sub require_relative {
    my ($path)=@_;
    if ($path=~ m|^/|) {
	require $path
    } else {
	my ($package, $cpath, $line) = caller;
	my $rpath= dirname($cpath)."/".$path;
	require $rpath;
    }
}
sub path_append2 ($$) {
    my ($a,$b)=@_;
    length $a or die;
    $a . ($a=~ m|/$| ? "" :
	  $b=~ m|^/| ? "" : "/") . $b
}


my $cfg= require_relative "logo.pl";

fun stripmd ($path0) {
    $path0=~ s|\.md$|| or die;
    $path0=~ m|^[\w/-]+\z| or die "contains stuff possibly messing up URLs: '$path0'";
    $path0
}

+{
  downcaps=> 0,
  warn_hint=> 0,
  indexpath0P=> fun ($path0) {
      $path0 eq "Home.md"
  },
  path0_handlers=> {},
  title=> fun ($filetitle) {
      ($filetitle, " - Bottoms")
  },
  head=> fun ($path0) {
      # HTML before navigation
      $$cfg{logo}
  },
  belownav=>
  `hostname`=~ /^servi/ ?
  fun ($path0) {
      # HTML between navigation and page content
      my $loc= path_append2($$cfg{homeurl}, xsuffix_md2html ($path0,0)); # XXX safe?
      my $base= "https://github.com/pflanze/bottoms/wiki/".stripmd($path0);
      FORM ({action=> "http://bottoms.christianjaeger.ch/cgi-bin/wiki-sync",
	     method=> "POST",
	     align=> "right",
	     class=> "belownav"
	    },
	    INPUT ({type=> "hidden", name=> "key", value=> "tpch24x4qsfhecs24a2"}),
	    INPUT ({type=> "hidden", name=> "target", value=> $loc}),
	    A({href=> "$base/_history"},
	      "history"),
	    " | ",
	    A({href=> "$base/_edit"},
	      "edit"),
	    " | ",
	    BUTTON ({type=> "submit", name=> "do", value=> "do", class=> "belownav"},
		    "sync"))
  } :
  # don't add history/edit/sync on local previews
  fun ($path0) {
      undef
  },
  sortorder=>
  [qw(
Home
News
Format
Aims
VM
S3C
GC
VML
Objects
Macros
Languages
Parser
Exercises
Tasks
Questions
Reading
More_links
Name
Licensing
Style
Mailing_lists
IRC
Meet-ups
Wiki
    )],
  # ^hm feature to  put some files at the end? would be nice.
 }
