Skip to content

Instantly share code, notes, and snippets.

@tanapoln
Last active September 14, 2023 08:25
Show Gist options
  • Save tanapoln/a905cccda65f7e87a0cce9e884258a64 to your computer and use it in GitHub Desktop.
Save tanapoln/a905cccda65f7e87a0cce9e884258a64 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
DOWNLOAD_LINK=$1
FILENAME=$2
if [[ -z "$DOWNLOAD_LINK" || -z "$FILENAME" ]]; then
echo "Error: missing DOWNLOAD_LINK or FILENAME"
echo "Usage: install-bin.sh [DOWNLOAD_LINK] [FILENAME]"
exit 1
fi
if [[ ! "$DOWNLOAD_LINK" =~ ^https?://.+$ ]]; then
echo "Error: invalid download link format"
exit 1
fi
echo "Download link: $DOWNLOAD_LINK"
echo "Filename: $FILENAME"
TMPDIR=$(mktemp -d)
OUTPUT_FILE="${TMPDIR}/${FILENAME}"
mkdir -p ~/.local/bin
TARGET_DIR=~/.local/bin
echo "Downloading: $DOWNLOAD_LINK"
curl --netrc -Z --fail -# "$DOWNLOAD_LINK" -o "$OUTPUT_FILE"
if [[ $? -ne 0 ]]; then
echo -e "\n"
echo "Error download file. If you've got 401 error, please setup ~/.netrc file as following:"
domain=$(echo "$DOWNLOAD_LINK" | sed -E "s/^https?:\/\/([^/]*)\/?.*$/\1/")
echo -e "machine ${domain}\n\tlogin your_username\n\tpassword your_password"
exit 1
fi
chmod +x $OUTPUT_FILE
TARGET_FILE="${TARGET_DIR}/${FILENAME}"
mv "$OUTPUT_FILE" "$TARGET_FILE"
echo "File is installed at $TARGET_FILE"
if [[ ! $PATH =~ "$TARGET_DIR" ]]; then
echo -e "\nPlease check if $TARGET_DIR is in \$PATH."
bashloc="~/.bashrc"
if [[ $(uname -s) == "Darwin" ]]; then
bashloc="~/.zshrc"
fi
echo -e "To put $TARGET_DIR in your \$PATH, you can run:\n\n\techo 'export PATH=\"$TARGET_DIR:\$PATH\"' >> $bashloc\n\tsource $bashloc\n\n"
fi
if [[ $(read -p "Remove temp folder $TMPDIR? (y/n): ") =~ ^[Yy]$ ]]; then
rm -rf "$TMPDIR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment