#!/usr/bin/env bash
#
# 加注释，支持#和//
#
function comment() {
  [[ ($1 != "#" && $1 != "//") || $# -lt 2 ]] && {
    echo "Usage: $0 <# or //> <pattern> <file...>"
    exit 1
  }
  local comment_str="#"
  [[ $1 == "//" ]] && comment_str="\/\/"
  local pattern=$2
  shift
  shift
  sed -i '/'$pattern'/ s/^/'$comment_str'/'  $@
}

function uncomment() {
  [[ ($1 != "#" && $1 != "//") || $# -lt 2 ]] && {
    echo "Usage: $0 <# or //> <pattern> <file...>"
    exit 1
  }
  local comment_str="#"
  [[ $1 == "//" ]] && comment_str="\/\/"
  local pattern=$2
  shift
  shift
  [[ $comment_str == "#" ]] && {
   sed -i '/'$pattern'/ s/^\s*#[ #]*//'  $@
  }
  [[ $comment_str == "\/\/" ]] && {
   sed -i '/'$pattern'/ s/^\s*\/\/[ /]*//'  $@
  }
}

comment "$@"