#xibit is an app that generates image galeries for werc.
#the license the mit license, see the license file in the distribution's root directory
#until the time i change my mind
#comments and suggestions can come to http://redmine.9souldier.org/projects/xibit0/issues/new or johnny@9souldier.org

fn conf_enable_xibit {
    xibit_uri=$conf_wd
    conf_enable_app xibit
}

fn xibit_init {
    xibit_root=$sitedir$"xibit_uri
#SOMEONE PLEASE FIXME    if(! ~ $#xibit_uri 0 && test '( -d '^$"xibit_root^'/_werc/xibit ) -a ( -w '^$"xibit_root^'/_werc/xibit )' && convert -help > /dev/null) {
    if(! ~ $#xibit_uri 0 && test -d $"xibit_root/_werc/xibit && test -w $"xibit_root/_werc/xibit && which convert > /dev/null) {
        xibitdir=$xibit_root^/_werc/xibit
        xibit_uri_file=`{echo $req_path |sed 's,^.*/([^/]+)$,\1,g'}
        mkdir -p $"xibitdir/previews
        mkdir -p $"xibitdir/thumbs
        extraHeaders=($extraHeaders '<style>' `{cat apps/xibit/xibit.css} '</style>')
        if( ~ $"req_path */index */)
            handler_body_main=xibit_thumb_handler
        if not {
            xibit_img=`{echo $req_path |sed 's,^.*/([^/]+)(\.[^\.]+)?$,\1,g'}
            for(pic in $xibit_img.^$fmts){
                if(test -f $xibit_root/^$pic)
                    handler_body_main=xibit_img_handler
            }
            if ( ~ $#handler_body_main 0 )
                status='xibit cant to anything here'
        }
    }
}

fn xibit_thumb_handler {
    cd $xibit_root
    xibitdir=_werc/xibit
    rm $"xibitdir/dirstat.md5.tmp > /dev/null >[2] /dev/null
    if( ! ~ `{ls -l |md5sum |tee $xibitdir/dirstat.md5.tmp} `{cat $xibitdir/dirstat.md5}) {
        mv $xibitdir/dirstat.md5.tmp $xibitdir/dirstat.md5
        for(pic in `{ls *.^$fmts >[2] /dev/null}) {
            if( ! test -f $"xibitdir/thumbs/^$"pic )
                convert $pic -resize $thumbsize $xibitdir/thumbs/^$pic
            if( ! test -f $"xibitdir/previews/^$"pic )
                convert $pic -resize $previewsize $xibitdir/previews/^$pic
    
            xibit_pics=($xibit_pics $pic)
      }
  }
  if not
    for(pic in `{ls *.^$fmts >[2] /dev/null})
        xibit_pics=($xibit_pics $pic)
  rm $xibitdir/dirstat.md5.tmp > /dev/null >[2] /dev/null
  cd $werc_root

  if( ! ~ $req_path */details )
    template apps/xibit/thumbs.tpl
  if not
    template apps/xibit/details.tpl
}


fn xibit_img_handler {
    cd $xibit_root
    for(file in $xibit_uri_file.^$fmts) {
        if( test -f $file) {
            xibit_img=`{echo $file |sed 's,^.*/([^/]+),\1,g'}
            xibit_preview=_werc/xibit/previews/^$xibit_img
            if (test -r _werc/xibit/captions/^$xibit_img^.md)
                xibit_caption_file=$xibitdir^/captions/^$xibit_img^.md
            xibit_nextpic=`{next_pic $xibit_img}
            xibit_prevpic=`{prev_pic $xibit_img}
            dprint $xibit_nextpic $xibit_prevpic
            cd $werc_root
        }
    }
    cd $werc_root
    xibit_thumb_handler
}

fn prev_pic {
    findre='/^'^$1^'$/!h;  /^'^$1^'$/!d; /^'^$1^'$/g;'
    if( ! ls -F |sed 1q |grep -s $1 )
        ls -F |grep -v '_werc' | sed 's,\*$,,g; /\$/d; s,'',,g' | sed  $"findre^'; s,\.[^\.]+$,,'
}

fn next_pic {
    findre='/^'^$1^'$/!d; /^'^$1^'/n; p'
    if( ! ls -F |tail -1 |grep -s $1 )
        ls -F |grep -v '_werc' | sed 's,\*$,,g; /\/$/d; s,'',,g' | sed -n $"findre |sed 's,\.[^\.]+$,,'
}

