#! /bin/sh
set -eu

if [ -n "${DEBUG:-}" ]; then
  set -x
fi

cat << EOF
 ____  _     _   _ _ _
|  _ \(_)___| |_(_) | | ___ _ __ _   _
| | | | / __| __| | | |/ _ \ '__| | | |
| |_| | \__ \ |_| | | |  __/ |  | |_| |
|____/|_|___/\__|_|_|_|\___|_|   \__, |
                                 |___/

install binaries as fast and as easy as possible. like homebrew but with less fizz.

if you found yourself somehow directly on this page and not programmatically, install using this one-liner:

  curl --proto '=https' --tlsv1.2 -LsSf https://get.dist.sh | sh

for more information checkout the README and our documentation at https://dist.sh

source code -> https://github.com/ekristen/distillery

Please file an issue if you encounter any problems!

===============================================================================
ATTENTION WINDOWS USERS
===============================================================================
If you are looking to install on windows use the following command:

  iwr https://get.dist.sh/install.ps1 -useb | iex

===============================================================================

EOF

VERSION=${VERSION:-latest}

DISTILLERY_CONFIG="${DISTILLERY_CONFIG:-}"
DISTILLERY_GITHUB_TOKEN="${DISTILLERY_GITHUB_TOKEN:-}"
DISTILLERY_GITLAB_TOKEN="${DISTILLERY_GITLAB_TOKEN:-}"

# Create a temporary directory and set up a trap to clean up
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXIT

__distillery_install_binary(){
  echo "Fetching installer.sh for version: $VERSION"
  URL=https://github.com/ekristen/distillery/releases/latest/download/installer.sh

  if [ "$VERSION" != "latest" ]; then
    URL="https://github.com/ekristen/distillery/releases/download/$VERSION/installer.sh"
  fi

  if ! curl --proto '=https' --tlsv1.2 -LsSf "$URL" -o "$TEMP_DIR/installer.sh"; then
    echo "Error: Failed to fetch installer.sh"
    exit 1
  fi

  if [ "$VERSION" = "latest" ]; then
    VERSION=""
  fi

  chmod +x "$TEMP_DIR/installer.sh"

  if ! VERSION=$VERSION DISTILLERY_CONFIG=$DISTILLERY_CONFIG DISTILLERY_GITHUB_TOKEN=$DISTILLERY_GITHUB_TOKEN DISTILLERY_GITLAB_TOKEN=$DISTILLERY_GITLAB_TOKEN sh "$TEMP_DIR/installer.sh" "$@"; then
    echo "Error: Failed to execute installer.sh"
    exit 1
  fi
}

if ! command -v curl > /dev/null; then
    echo "curl not installed. Please install curl."
    exit
fi

__distillery_install_binary "$@"

echo "Installation Complete!"
echo 
echo "You still have to export the distillery bin directory to your PATH"
echo "For example, add the following to your .bashrc or .zshrc file:"
echo
echo "For bash:"
echo
echo "  echo \"export PATH=\$HOME/.distillery/bin:\$PATH\" >> \$HOME/.bashrc"
echo
echo "For zsh:"
echo 
echo "  echo \"export PATH=\$HOME/.distillery/bin:\$PATH\" >> \$HOME/.zshrc"
echo
