#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
}

fn xibit_init {
    xibit_root=$sitedir$"xibit_uri
    if(!  ~ $#xibit_uri 0 && test -d $xibit_root^/_werc/xibit -w
    $xibit_root^/_werc/xibit && convert -help > /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
	if (!  ~ $#xibit_check_user 0 && ! check_user) # XXX Perm checking should
	be done in config file instead.
	    handler_body_main=(echo '<a href="/_users/login">please log
	    in</a>')
	if not {
	    extraHeaders=($extraHeaders '<style>' `{cat
	    apps/xibit/xibit.css} '</style>')
	    if( ~ $req_path */index */)
		handler_body_main=xibit_thumb_handler
	    if not {
		handler_body_main=xibit_img_handler
		ll_add handlers_body_foot xibit_thumb_handler
	    }
	}
    }
}

fn xibit_thumb_handler {
    cd $xibit_root
    for(pic in `{ls *.^$fmts >[2] /dev/null}){
	if(!  test -f _werc/xibit/thumbs/^$pic)
	    convert $pic -resize $thumbsize _werc/xibit/thumbs/^$pic
	if(!  test -f _werc/xibit/previews/^$pic)
	    convert $pic -resize $previewsize _werc/xibit/previews/^$pic

	xibit_pics=($xibit_pics $pic)
    }
    cd $werc_root

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


fn xibit_img_handler {
    for(f in $xibit_root/^$xibit_uri_file.^$fmts) {
	if(test -f $file) {
	    xibit_img=`{echo $f | sed 's,^.*/([^/]+),\1,g'}
	    xibit_preview=_werc/xibit/previews/^$xibit_img
	    if(test -r $xibitdir^/captions/^$xibit_img^.md)
	    xibit_caption_file=$xibitdir^/captions/^$xibit_img^.md
	    template apps/xibit/image.tpl
	}
    }
}