# usage: sidebar siteroot currentpage
#
# e.g.: % sidebar /home/lplt/sites/src.lplt.net /home/lplt/sites/src.lplt.net/swb/navtree.md

fn lst {
  dir=$1

  echo $2^'<ul>'

  for (f in $dir/*) {
    basef=`{basename $f}

    if (test -d $f && ! ~ $basef 'pub') {
      if (~ $current $f/*) {
        echo $2^'<li><a href="'^`{trim_chroot $chroot $f}^'/" class="thisPage">&rsaquo; <i>'^$basef^'/</i></a></li>'
        newindent=$2^'    '
        echo $2^'<li>'
        lst $f $newindent
        echo $2^'</li>'
      }
      if not {
        echo $2^'<li><a href="'^`{trim_chroot $chroot $f}^'/">&rsaquo; '^$basef^'/</a></li>'
      }
    }

    if not {
      if (~ $f *.md && ! ~ $basef index.md) {
        pagepath=`{trim_chroot $chroot $f |sed 's/\.md$//g'}
        pagename=`{basename $pagepath}
        if (~ $f $current)
          echo $2^'<li><a href="'^$pagepath^'" class="thisPage">&rsaquo;<i> '^$pagename^'</i></a></li>'
        if not
          echo $2^'<li><a href="'^$pagepath^'">&rsaquo; '^$pagename^'</a></li>'
      }
    }
  }

  echo $2^'</ul>'
}

fn trim_chroot {
  # trim $1 prefix from path $2
  echo $2 |sed 's%^'$1'%%g'
}

current=$2
chroot=$1

lst $1 ''



