#compdef nb

# author: Andy Spiegl <nanoblogger.andy@spiegl.de>
#
# 2006-06-16: first version
# 2006-06-20: --category is respected now (thanks to Ulrich, spamt.net)

local state categories entries ret=1

# TODO:  (HELP!)
#  -c,-d,-e,-m accepts multiple ID numbers seperated by commas (e.g. 1,2,3)
#  --list and --update accepts regular expressions to match entries by date
#   ( date = YYYY-MM-DD, YYYY-MM, ^YYYY)
# --delete accepts categories OR entries but currently only entries are completed
#

# find out user specified category
integer catind=${words[(i)-c|--category]}
if (( catind )); then
  cat=$words[catind+1]
fi
#echo -e "\n DEBUG: cat=$cat\n"

# -S: no option will be completed after a `--' appearing on its own on the line
_arguments -S \
   '(--help -h *)'{--help,-h}'[show help message]' \
   '(--version -V)'{--version,-V}'[display version information]' \
   '--manual[view the manual]' \
   '(--add -a)'{--add,-a}'[create new entry, category, or weblog]' \
   '(--body -B)'{--body,-B}'[set body of entry (for add)]:body of entry:' \
   '(--blogdir -b)'{--blogdir,-b}'[weblog directory]:blogdir:_path_files -/' \
   '(--category -c)'{--category,-c}'[category <ID,cat> (for add,delete,edit,list,update)]:categories:->categories' \
   '--configure[configure weblog (for update)]' \
   '--datadir[weblogs data directory]:datadir:_path_files -/' \
   '(--delete -d)'{--delete,-d}'[delete an entry or category <ID,cat>]:entries:->entries' \
   '(--desc -D)'{--desc,-D}'[set description of entry (for add)]:description of entry:' \
   '(--edit -e)'{--edit,-e}'[edit entry/entries or category <ID,cat>]:entries:->entries' \
   '(--blogconf -f)'{--blogconf,-f}'[alternate configuration file]:blogconf:_path_files' \
   '(--list -l)'{--list,-l}'[list entries or categories]:listtype:((all\:"all entries" cat\:"defined categories" current\:"current entries" DATE\:"e.g. YYYY-MM-DD, YYYY-MM, ^YYYY" max\:max))' \
   '(--update -u)'{--update,-u}'[update of weblog]:updatetype:((all\:"all entries" current\:"current entries" DATE\:"e.g. YYYY-MM-DD, YYYY-MM, ^YYYY" main\:main))' \
   '--makepage[source and output file to create web page]:_path_files:_path_files' \
   '--template[file to load as template (for makepage)]:_path_files' \
   '--templatedir[weblogs template directory]:_path_files -/' \
   '(--move -m)'{--move,-m}'[move an entry to a specified category (for category)]:entries:->entries' \
   '(--author -n)'{--author,-n}'[set author of entry (for add)]:author of entry:' \
   '(--title -t)'{--title,-t}'[set title of entry/category/page (for add,category,makepage)]:title of entry/category/page' \
   '(--preview -p)'{--preview,-p}'[run command to preview weblog]' \
   '(--publish -P)'{--publish,-P}'[run command to publish weblog]' \
   '(--verbose -v)'{--verbose,-v}'[toggle level of verbosity]:verb:((1\:on 0\:off))'


case $state in
  categories)
    categories=( ${${(M)${(f)"$(nb --list cat 2>/dev/null)"}:# *, *}/ (#b)(*[^ ]) #, (*)/$match[1]:$match[2]})

    _describe -t categories "nanoblogger categories" categories && ret=0
  ;;

  entries)
    if [[ -z $cat ]]; then
      entries=( ${${(M)${(f)"$(nb --list 2>/dev/null)"}:# *, *}/ (#b)(*[^ ]) #, (*)/$match[1]:$match[2]})
    else
      entries=( ${${(M)${(f)"$(nb --category $cat --list 2>/dev/null)"}:# *, *}/ (#b)(*[^ ]) #, (*)/$match[1]:$match[2]})
    fi

    _describe -t entries "nanoblogger entries" entries && ret=0
  ;;
esac
