#!/bin/sh

install() {
  set \
    -o verbose \
    -o xtrace
  case "${1-}" in
  caveat*)
    shift
    {
      set \
        +o verbose \
        +o xtrace
    } 2>/dev/null
    case "${1-}" in
    environment*)
      command brew list -1 --formula | while IFS='' read -r -- formula; do
        if command brew info --formula -- "${formula-}" |
          command -p -- grep \
            -e 'print the names of formulae where there is a § Caveats' \
            -e ' Caveats' \
            >/dev/null 2>&1; then
          command -p -- printf -- '%s\n' "${formula-}"
        fi
      done
      ;;
    *)
      command brew list -1 --formula | while IFS='' read -r -- formula; do
        if command brew info --formula -- "${formula-}" |
          command -p -- grep \
            -e 'print the names of formulae where environmental-variable modification is suggested' \
            -e '[[:upper:]]=' \
            >/dev/null 2>&1; then
          command -p -- printf -- '%s\n' "${formula-}"
        fi
      done
      ;;
    esac
    ;;
  file*)
    shift
    set \
      -o noclobber \
      -o verbose \
      -o xtrace
    command brew bundle dump \
      --all \
      --cask \
      --debug \
      --describe \
      --file=- \
      --force \
      --formula \
      --mas \
      --no-restart \
      --tap \
      --verbose \
      "${@-}" |
      # move each package name onto the comment line above it, if any
      command -p -- sed \
        -e '$! N' \
        -e '/^#.*\n[^#]/ s/\n/\t/' \
        -e 'P' \
        -e 'D' |
      # swap the package and the comment
      command -p -- sed \
        -e 's/\(.*\)\t\(.*\)/\2\1/' |
      # prepend each category with a number for sorting
      command -p -- sed \
        -e 's/^\(tap\)/1\1/' \
        -e 's/^\(brew\)/2\1/' \
        -e 's/^\(cask\)/3\1/' |
      # sort output by package name
      LC_ALL='C' command -p -- sort -f | {
      command -p -- printf -- '#!/usr/bin/env ruby\n'
      command -p -- printf -- '# frozen_string_literal: true\n\n'
      command -p -- sed \
        -e '# remove the prepended numbers' \
        -e 's/^[[:digit:]]//' \
        -e '# restore each comment to a line above its package' \
        -e 's/\([^#]*\)\(#.*\)/\2\n\1/'
    } >|"${HOMEBREW_BUNDLE_FILE_GLOBAL:-${HOMEBREW_BUNDLE_FILE:-${HOME%/}/.Brewfile}}" &&
      command -p -- chmod -- 755 "${HOMEBREW_BUNDLE_FILE_GLOBAL:-${HOMEBREW_BUNDLE_FILE:-${HOME%/}/.Brewfile}}"
    {
      set \
        +o noclobber \
        +o verbose \
        +o xtrace
    } 2>/dev/null
    ;;
  HEAD*)
    shift
    {
      set \
        +o verbose \
        +o xtrace
    } 2>/dev/null
    command brew list -1 --formula | while IFS='' read -r -- formula; do
      if command brew info --formula -- "${formula-}" |
        command -p -- grep \
          -e 'print the names of formulae where --HEAD is available' \
          -e '^--HEAD$' \
          >/dev/null 2>&1; then
        command -p -- printf -- '%s\n' "${formula-}"
      fi
    done
    ;;
  search)
    shift
    brewsearch "${@-}"
    ;;
  info)
    command brew "${@-}"
    ;;
  --)
    shift
    ;;
  *)
    # @TODO! this should allow for  install --search  -- 𝑥
    #                               install --info    -- 𝑥
    #                               install           -- 𝑥
    #                  and perhaps  install --install -- 𝑥

    # Homebrew
    if command -v -- brew >/dev/null 2>&1; then
      command brew install "${@-}"
    # search brew search --debug --verbose --desc git; brew search git

    # Alpine Linux
    elif command -v -- apk >/dev/null 2>&1; then
      command apk add "${@-}"

    # Ubuntu, Debian
    elif command -v -- apt >/dev/null 2>&1; then
      command sudo -- apt install "${@-}"
    elif command -v -- apt-get >/dev/null 2>&1; then
      # https://clarkgrubb.com/package-managers#pkg-managers
      command sudo -- apt-get install -y "${@-}"

    # Arch Linux
    elif command -v -- pacman >/dev/null 2>&1; then
      # https://unix.stackexchange.com/a/34607
      # if Arch fails here, try `pacman --sync -yy`
      command pacman --sync -yy "${@-}"
    else
      command -p -- printf -- 'unable to detect system software installer\n'
      command -p -- sleep 1
      command -p -- printf -- 'aborting\n'
      return 1
    fi
    ;;
  esac
  {
    set \
      +o verbose \
      +o xtrace
  } 2>/dev/null
}
alias -- i='install'
