Last active
August 24, 2019 18:47
-
-
Save naoki-mizuno/f4e27e3d8e302aa1f2bd495b6b369206 to your computer and use it in GitHub Desktop.
CLion Updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
KEEP_TAR='false' | |
TAR_FILE='' | |
while getopts 'hkt:' flag; do | |
case "$flag" in | |
k) | |
KEEP_TAR='true' | |
;; | |
t) | |
TAR_FILE="$OPTARG" | |
;; | |
h) | |
echo "Usage: $0 [-h] [-k] [-t tar file path]" | |
echo " -k Keep the downloaded tar file" | |
echo " -t Use an already downloaded tar file" | |
exit 0 | |
esac | |
done | |
shift $(( $OPTIND - 1 )) | |
DEST_DIR="${1:-$HOME/usr/share/clion}" | |
download_in_temp() { | |
local tar_dir="$( mktemp -d )" | |
builtin cd "$tar_dir" | |
curl -L \ | |
'https://data.services.jetbrains.com/products/download?code=CL&platform=linux' \ | |
-o clion.tar.gz | |
echo "$tar_dir/clion.tar.gz" | |
} | |
extract_and_replace() { | |
local tar_file="$1" | |
local dest_dir="$2" | |
local latest_version_dir | |
builtin cd "$( dirname $dest_dir )" | |
if [[ -s $dest_dir ]]; then | |
rm -rf $dest_dir | |
elif [[ -d $dest_dir ]]; then | |
read -p "$dest_dir exists. Remove? [Y/n] " ans | |
if [[ $ans == 'n' ]]; then | |
echo "Renaming to ${dest_dir}_old" | |
mv $dest_dir ${dest_dir}_old | |
else | |
rm -rf $dest_dir | |
fi | |
fi | |
echo 'Unpacking tar file' | |
tar xzf "$tar_file" | |
latest_version_dir="$( find . -maxdepth 1 -name 'clion-*' -type d | sort -r | head -n 1 )" | |
ln -sf $latest_version_dir ./clion | |
if $KEEP_TAR; then | |
echo "Saved tar file at $tar_file" | |
else | |
echo 'Removing tar file' | |
rm -rf $tar_file | |
fi | |
} | |
if [[ -z $TAR_FILE ]]; then | |
echo 'Downloading latest CLion' | |
TAR_FILE="$( download_in_temp )" | |
fi | |
extract_and_replace "$TAR_FILE" "$DEST_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment