#!/bin/sh
#
# git-ddg - searches DuckDuckGo - git ddg <term>

set -eu

# see: https://gist.github.com/cdown/1163649#file-gistfile1-sh
url_encode() {
  awk 'BEGIN {
     for (n = 0; n < 125; n++) {
         m[sprintf("%c", n)] = n
      }
      n = 1
      while (1) {
         s = substr(ARGV[1], n, 1)
         if (s == "") {
            break
         }
         t = s ~ /[[:alnum:]_.!~*\47()-]/ ? t s : t sprintf("%%%02X", m[s])
         n++
      }
      print t
   }' "$1"
}

query=$(url_encode "git $(printf '%b' "$@") tutorial")
open "https://duckduckgo.com/?q=${query}"
