usage() {
  echo "usage: git now rebase [--author|-a] [<base-branch>]"
  #echo "       git now rebase --push [<base-branch>]"
  #echo "       git now rebase --push --upstream <remote> [<base-branch>]"
  #echo "       git now rebase --push --remove <remote> [<base-branch>]"
}


cmd_default() {
  parse_args "$@"

  WORKING_BRANCH=$(git_current_branch)
  INITIAL_COMMIT=$(git_now_initial_commit)

  if [ -n "$BASE_BRANCH" ]; then
    COMMON_ANCESTOR_COMMIT=`git merge-base ${BASE_BRANCH} ${WORKING_BRANCH}`
    if flag author; then
      FIRST_NOW_COMMIT=$(git_now_my_first_commit $COMMON_ANCESTOR_COMMIT)
    else
      FIRST_NOW_COMMIT=$(git_now_first_commit $COMMON_ANCESTOR_COMMIT)
    fi

    if [ "$FIRST_NOW_COMMIT" != "$INITIAL_COMMIT" ]; then
      git rebase -i ${FIRST_NOW_COMMIT}^
    else
      git checkout ${FIRST_NOW_COMMIT}
      git commit --amend
      git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}
    fi
  else
    if flag author; then
      FIRST_NOW_COMMIT=$(git_now_my_first_commit)
    else
      FIRST_NOW_COMMIT=$(git_now_first_commit)
    fi

    if [ "$FIRST_NOW_COMMIT" != "$INITIAL_COMMIT" ]; then
      git rebase -i ${FIRST_NOW_COMMIT}^
    else
      git checkout ${FIRST_NOW_COMMIT}
      git commit --amend
      git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH}
    fi
  fi

  #if flag push; then
    #if [ -n "$UPSTREAM_REMOTE" ]; then
      #git push --set-upstream $UPSTREAM_REMOTE $WORKING_BRANCH
    #elif [ -n "$REMOVE_REMOTE" ]; then
      #git push $REMOVE_REMOTE :$WORKING_BRANCH
      #git push
    #else
      #git push
    #fi
  #fi
}

cmd_help() {
  usage
  exit 0
}

parse_args() {
  # parse options
  DEFINE_boolean 'author' false 'limit git-now commits by your name' 'a'
  #DEFINE_boolean 'push' false 'push to remote repository finally' ''
  #DEFINE_string 'remove' '' 'remote name to remove the remote branch before push' ''
  #DEFINE_string 'upstream' '' 'remote name for --set-upstream' ''
  FLAGS "$@" || exit $?
  eval set -- "${FLAGS_ARGV}"

  # read arguments into global variables
  BASE_BRANCH=$1
  #UPSTREAM_REMOTE=$FLAGS_upstream
  #REMOVE_REMOTE=$FLAGS_remove
}

