#
# Bookmark script for zsh
# by Michael Neumann (neumann@s-direktnet.de)
#

dirs=(
  /usr/pkgsrc
)

if [[ $# == 0 ]] ; then
  cnt=1
  for i in $dirs ; do
    echo "[$cnt]\t$i"
    cnt=`expr $cnt + 1`
  done 
else
  # reload array dirs
  if [[ $1 == -r ]] ; then
    unfunction $0
    autoload $0
  else
    nr=`expr $1 : '\([0-9]*\)$'`

    if [[ $nr != '' ]] ; then          # it's a number
      if [[ $nr -le $#dirs && $nr -gt 0 ]] ; then 
        cd $dirs[$nr]
      else
        echo "False bookmark number"
      fi
    else
      cnt=1
      match_dirs=()
      for i in $dirs ; do
        if [[ `expr $i : '.*'$1` -gt 0 ]] ; then  # matches entry
          match_dirs[$cnt]=$i
          cnt=`expr $cnt + 1`
        fi
      done

      if [[ $#match_dirs == 0 ]] ; then
        echo "Nothing found"
      elif [[ $#match_dirs == 1 ]] ; then 
        cd $match_dirs[1]
        pwd
      else
        cnt=1
        for i in $match_dirs ; do 
          echo "[$cnt]\t$i"
          cnt=`expr $cnt + 1`
        done
        echo -n "? "; read i
        cd $match_dirs[$i] 
      fi   
    fi
  fi
fi  
