#!/usr/bin/env bash

# version

VERSION="0.3.0"

# bookmarks dir

DIR=~/Dropbox

# thumbs cache

THUMBS=$DIR/bookmark-thumbs

# bookmarks file

BOOKMARKS=$DIR/bookmarks

#
# Output usage info
#

usage() {
  cat <<EOF

  Usage: bm [options] [cmd]

  Commands:

    # add a bookmark with the given url, description, and optional tags
    $ bm add <url> [desc] [tag...]

    # open the first bookmark matching <query>
    $ bm open <query>
    $ bm <query>

    # search the bookmarks via full-text <query>
    $ bm search <query>

    # list bookmarks available
    $ bm list
    $ bm ls
    $ bm

    # view bookmark screenshots in your default browser
    $ vm view

    # clear all bookmarks
    $ bm clear

  Options:

     -V, --version   output bm version
     -h, --help      output this help information

EOF
}

#
# Add a bookmark
#
#   <url> [description] [tag ...] 
#

save_bookmark() {
  local url=$1
  local desc=$2
  local tags=${@:3}
  echo
  echo "  Added bookmark"
  echo
  echo "    url: $url"
  echo "    description: $desc"
  echo "    tags: $tags"
  echo
  echo "$url|$desc|$tags" >> $BOOKMARKS
}

#
# List all bookmarks
#

list_bookmarks() {
  echo
  cat $BOOKMARKS \
    | awk '
    BEGIN { FS = "|" }
    {
      printf "  \033[36m%s\033[0m\n", $1
      printf "  \033[33m%s\033[0m\n", $2
      printf "  \033[90m%s\033[0m\n\n", $3
    }' \
    | sed 's/http:\/\///'
  echo
}

#
# Search all bookmarks with <query>
#

search_bookmarks() {
  echo
  cat $BOOKMARKS \
    | grep $1 \
    | awk '
    BEGIN { FS = "|" }
    {
      printf "  \033[36m%s\033[0m\n", $1
      printf "  \033[33m%s\033[0m\n", $2
      printf "  \033[90m%s\033[0m\n\n", $3
    }' \
    | sed 's/http:\/\///'
  echo
}

#
# Open first bookmark matching <query>
#

open_bookmark() {
  cat $BOOKMARKS \
    | grep $1 \
    | cut -d '|' -f 1 \
    | xargs open
}

#
# Stylesheet
#

style() {
  cat <<EOF
<style>
  body {
    padding: 50px 0 5px 50px;
  }
  .bm {
    float: left;
    margin: 10px;
    padding: 1px;
    border: 1px solid #eee;
    border-bottom: 1px solid #ddd;
    -webkit-border-radius: 5px;
    -webkit-box-shadow: 0 0 5px #eee;
    opacity: 1;
  }
  .bm:hover {
    opacity: .75;
  }
</style>
EOF
}

#
# List urls
#

urls() {
  cat $BOOKMARKS | cut -d '|' -f 1
}

#
# Generate bookmark screenshots
#

create_bookmark_screenshots() {
  which webkit2png > /dev/null

  if test $? -ne 0; then
    echo 
    echo "  You need webkit2png(1) installed for this feature:"
    echo "  $ brew install webkit2png"
    echo 
    exit
  fi

  local urls=`urls`

  # thumbs dir
  mkdir -p $THUMBS

  # fetch thumbs
  for url in $urls; do
    local path=$THUMBS/`echo $url | md5`.png
    if ! test -f $path; then
      webkit2png -C -o out.png $url
      mv out.png-clipped.png $path
      echo " ... added $path"
    fi
  done

  rm -f out.png-{clipped,full,thumb}.png
}

#
# View bookmark screenshots.
#

view_bookmark_screenshots() {
  create_bookmark_screenshots
  local urls=`urls`

  style > /tmp/bm.html

  for url in $urls; do
    local path=$THUMBS/`echo $url | md5`.png
    echo "
      <div class=bm>
        <a href='$url'>
          <img src='$path' />
        </a>
      </div>
    " >> /tmp/bm.html
  done

  open /tmp/bm.html
}

#
# Remove all bookmarks
#

clear_bookmarks() {
  rm $BOOKMARKS
  rm -fr $THUMBS
}

# no args

if test $# -eq 0; then
  list_bookmarks
  exit
fi

# parse args

while test $# -ne 0; do
  arg=$1; shift
  case $arg in
    -V|--version) echo $VERSION; exit ;;
    -h|--help) usage; exit ;;
    ls|list) list_bookmarks; exit ;;
    search) search_bookmarks "$@"; exit ;;
    open) open_bookmark "$@"; exit ;;
    add) save_bookmark "$@"; exit ;;
    view) view_bookmark_screenshots; exit ;;
    clear) clear_bookmarks; exit ;;
    *) open_bookmark $arg; exit ;;
  esac
done
