fn conf_enable_ico {
    ico_uri=$conf_wd
    ico_dirs=$*
    if(~ $#ico_dirs 0)
        ico_dirs=( . )
    conf_enable_app ico

    if(~ $"conf_ico_editors '')
        conf_ico_editors=ico-editors

    if(~ $"conf_max_posts_per_page '')
        conf_max_posts_per_page=32

    if(~ $"conf_ico_categories '')
        conf_ico_categories=(general)

    if(~ $"conf_ico_max_image_size '')
        conf_ico_max_image_size=5242880
}

fn ico_init {
    if(~ $#ico_dirs 0 && ~ $req_path */[iI]co/*) {
        ico_uri=`{echo $req_path | sed 's,(/[iI]co/).*,\1,'}
        ico_dirs=( . )
    }

    if(! ~ $#ico_dirs 0) {
        ico_url=$base_url^$ico_uri
        ico_root=$sitedir^$ico_uri
        if(check_user $conf_ico_editors) {
            editor_mode=on
            if(~ $"post_arg_date '')
                post_date=`{/bin/date -I|sed 's,-,/,g'}
            if not
                post_date=$post_arg_date
            ll_add handlers_foot_body template `{get_lib_file ico/foot.tpl apps/ico/foot.tpl}
        }

        if(~ $req_path $ico_uri) {
            handler_body_main=ico_body
            u=$ico_uri'index'
            extraHeaders=$"extraHeaders ^ \
'<link rel="alternate" type="application/atom+xml" title="ATOM" href="'$"u'.atom" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="'$"u'.rss" />'
        }
        if not if(~ $req_path $ico_uri^category/*) {
            category=`{echo $req_path | sed 's!.*/!!; s/_/ /g'}
            handler_body_main=ico_category_body
        }
        if not if(~ $req_path $ico_uri^index.atom)
            ico_setup_feed_handlers atom.tpl 'application/atom+xml'
        if not if(~ $req_path $ico_uri^index.rss)
            ico_setup_feed_handlers rss20.tpl 'application/rss+xml'
        if not if(~ $req_path $ico_uri^new_post && ! ~ $#editor_mode 0) {
            handler_body_main=( tpl_handler `{get_lib_file ico/new_post.tpl apps/ico/new_post.tpl} )
            if(~ $REQUEST_METHOD POST) {
                if(mkicopost $"post_arg_body $"post_date $"post_arg_title $"post_arg_categories $"post_arg_loc $"post_arg_loc_link $"post_arg_link $"post_arg_short_desc $"post_arg_poster $"post_arg_edate)
                    post_redirect $ico_uri
                if not
                    notify_errors=$status
            }
        }
    }
}

fn ico_category_body {
    template apps/ico/event_list_cat.tpl
}

fn ico_body {
    template apps/ico/event_list_all.tpl
}

fn get_post_list {
    ls -F $*^/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/ >[2]/dev/null | 
    sed -n '/'^$forbidden_uri_chars^'/d; /\/$/p' | 
    sort -r | 
    sed -e $conf_max_posts_per_page^'q'
}

fn mkicopost {
    icotext=$1
    icodate=$2
    icotitle=$3
    icocategories=$4
    icolocation=$5
    icolocationlink=$6
    icoeventlink=$7
    icoshortdesc=$8
    icoposterpath=$9
    icoexpireddate=$10
    _status=()
    postdate=`{/bin/date -I|sed 's,-,/,g'}
    if(~ $"icotext '')
        _status=($_status 'You need to provide a post body.')
    if(! ~ $"postdate [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9])
        _status=($_status 'Invalid date: '''^$"postdate^'''')

    if(~ $#_status 0) {
        umask 022
        ddir=$ico_root^$postdate^'/'
        n=`{ls $ddir >[2]/dev/null | wc -l}
        mkdir -p $ddir^$n

        post_link='/'$postdate^'/'$n

        category_links=''
        for(cat in `{echo $icocategories}) {
            if(! ~ $cat '') {
                cat_link=`{echo -n $cat | sed 's/ /_/g'}
                category_links=$category_links'<div>[<a href='$ico_uri'category/'$cat_link'>'$cat'</a>]</div> '
            }
        }

        icopostlink='/'^$postdate^'/'^$n^'/'
        
        template apps/ico/post.tpl > $ddir^$n^'/index.html' 

        # Create the main categories directory
        categories_dir=$ico_root^'categories'
        mkdir -p $categories_dir

        # Update category files
        for(cat in `{echo $icocategories}) {
            if(! ~ $cat '') {
                cat_file=$categories_dir^'/'^`{echo -n $cat | sed 's/ /_/g'}^'.txt'
                echo $post_link >> $cat_file
            }
        }
    }
    status=$_status
}

fn get_post_by_category {
    cat_dir=$ico_root^'categories'
    cat_file=$cat_dir^'/'^`{echo -n $1 | sed 's/ /_/g'}^'.txt'
    if(test -f $cat_file) {
        cat $cat_file | sort -r | sed $conf_max_posts_per_page^'q'
    }
}

fn list_categories {
    ls $ico_root^'categories/'*.txt | sed 's!.*/!!; s!\.txt$!!; s!_! !g'
}
