# Rename existing tools
alias _cat=`which cat`
alias _curl=`which curl`
alias _ls="/bin/ls"
alias _grep="/usr/bin/grep"
alias _diff="/usr/bin/diff"
alias _du=`which du`
alias _df=`which df`
alias _find=`which find`
alias _top=`which top`
alias _ps="/bin/ps"
alias _dig=`which dig`
alias _readlink='/usr/bin/readlink'
alias _sed='/usr/bin/sed'
alias _date='/bin/date'
alias _base64='/usr/bin/base64'
alias _git=`which git`
if command -v $(brew --prefix)/bin/git &>/dev/null; then
  alias _git=$(brew --prefix)/bin/git
fi

# Aliases
alias cat='bat '
alias curl='curlie'
alias ls='eza --group-directories-first --icons --header --git --ignore-glob=.git'
alias grep='rg'
alias diff='delta'
alias du='dust'
alias df='duf -hide special'
alias find='fd'
alias find-empty-dirs='fd --type empty --type directory'
alias bandwhich='sudo bandwhich'
alias top='btm'
alias ps='procs'
alias dig='doggo'
alias kubectx='switch'
alias kctx='switch'
alias machine-info='macchina -t Boron --bar'
alias watch='watch '
alias tmp='cd $(mktemp -d)'

# Ensure GNU version of tools are used by default (symlink so they are picked up in scripts also)
which greadlink &>/dev/null && ln -sfn `which greadlink` /usr/local/bin/readlink
which gsed &>/dev/null && ln -sfn `which gsed` /usr/local/bin/sed
which gdate &>/dev/null && ln -sfn `which gdate` /usr/local/bin/date
which gbase64 &>/dev/null && ln -sfn `which gbase64` /usr/local/bin/base64

lt() {
  DEPTH=$(echo $1 | grep "^[0-9]*$")
  if [ "$DEPTH" = "" ]; then
    DEPTH=2
  else
    shift
  fi
  ls -l --tree -L $DEPTH -I ".git|cache|log|logs|node_modules|vendor" $@
}

git() {
  if [ "$1" = "take" ]; then # Git clone then cd into new directory
    if [ $# -gt 2 ]; then
      _git clone ${@:2}
      cd $3
    else
      ORG=$(echo $2 | sed -e 's|https://||' -e 's|/| |g' -e 's|:| |g' | cut -f 2 -d ' ')
      REPO=$(echo $2 | sed -e 's|https://||' -e 's|/| |g' -e 's|:| |g' | cut -f 3 -d ' ' | xargs basename -s .git)

      DST="${HOME}/Code/${ORG}/${REPO}"
      mkdir -p ${DST}

      _git clone $2 ${DST}
      cd ${DST}
    fi
  elif [ "$1" = "commit" ]; then # Sign all commits
    shift
    _git commit -s $@
  elif [ "$1" = "co" ]; then # Create a new branch
    shift
    _git checkout -b $@
  elif [ "$1" = "push" ]; then # Guard against pushing to certain orgs
    shift
    if [ $# -eq 0 ]; then
      remote_url=$(_git remote get-url $(git current-remote))
      if [[ $remote_url == *"kubernetes/"* ]] || [[ $remote_url == *"kubernetes-sigs/"* ]]; then
        echo "⚠️  Don't push directly to Kubernetes"
        return 1
      fi
    fi
    _git push $@
  elif [ "$1" = "release" ]; then # Create a new tag
    shift
    SEMVER=$1

    CURRENT_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
    VERSION_PARTS=($(echo $CURRENT_TAG | tr "." "\n"))
    VERSION_MAJOR=${VERSION_PARTS[1]}
    VERSION_MINOR=${VERSION_PARTS[2]}
    VERSION_PATCH=${VERSION_PARTS[3]}

    case ${SEMVER} in
      patch)
        VERSION_PATCH=$((VERSION_PATCH+1))
        ;;

      minor)
        VERSION_MINOR=$((VERSION_MINOR+1))
        VERSION_PATCH=0
        ;;

      major)
        if [[ ${VERSION_MAJOR:0:1} == "v" ]]; then
          VERSION_MAJOR="v$((VERSION_MAJOR+1))"
        else
          VERSION_MAJOR=$((VERSION_MAJOR+1))
        fi
        VERSION_MINOR=0
        VERSION_PATCH=0
        ;;

      *)
        echo "Unknown Semver level provided"
        exit 1
        ;;
    esac

    NEW_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
    echo ""
    echo "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ "
    echo "Current version  ${CURRENT_TAG}"
    echo "    New version  ${NEW_VERSION}"
    echo "✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ ✨ "
    echo ""

    printf "Confirm? (y/n): "
    read CONFIRM

    if [ "${CONFIRM}" = "y" ]; then
      git tag -a -m "${NEW_VERSION}" ${NEW_VERSION}
      echo "New tag created, don't forget to push to remote"
    else
      echo "Aborting..."
      exit 1
    fi
  else
    _git $@
  fi
}

