#!/usr/bin/env bash console_info() { if ! tput setaf &> /dev/null then echo -e "$1" else echo -e "$(tput setaf 2)$1$(tput sgr0)" fi } console_error() { if ! tput setaf &> /dev/null then echo -e "$1" 1>&2 else echo -e "$(tput setaf 1)$1$(tput sgr0)" 1>&2 fi } if (( $# > 0 )) then console_info "Installing version: $1" version=$1 else console_info "Installing latest version of bashum." version="latest" fi if (( $UID == 0 )) then console_error "Installing as root is not currently supported." exit 1 fi archive_tmp=${bashum_file_tmp:-/tmp/bashum"$RANDOM".tar} archive_remote=${bashum_file_remote:-"https://github.com/pkopriv2/bashum-versions/raw/master/bashum-$version.tar"} console_info "Attempting to download bashum file [$archive_remote] to [$archive_tmp]" if command -v curl &> /dev/null then if ! curl -L $archive_remote > $archive_tmp then console_error "Error downloading: $archive_remote. Either cannot download or cannot write to file: $bashum_file_tmp" exit 1 fi elif command -v wget &> /dev/null then if ! wget -q -O $archive_tmp $bashum_file_remote then console_error "Error downloading: $archive_remote. Either cannot download or cannot write to file: $bashum_file_tmp" exit 1 fi else console_error "This installation requires either curl or wget." exit 1 fi console_info "Cleaning up old bashum files." if [[ -d ~/.bashum ]] then rm -rf ~/.bashum fi console_info "Installing bashum." if ! tar -xf $archive_tmp -C ~ then console_error "Error unpackaging tmp file [$archive_tmp]" exit 1 fi if ! rm -f $archive_tmp then console_error "Error removing tmp file [$archive_tmp]" exit 1 fi env_files=( "~/.bashrc" "~/.bash_profile" "~/.zshrc" ) for file in "${env_files[@]}" do eval "file=$file" # expand the path correctly if [[ ! -f $file ]] then touch $file fi if grep -q "source ~/.bashum/env.sh" $file then continue fi console_info "Adding env entries to: $file" cat - >> $file <<-EOF # Generated by: bashum source ~/.bashum/env.sh EOF if (( $? )) then console_error "Error updating file [$file]" exit 1 fi done console_info "Successfully installed bashum! Please source your environment for changes to take effect. (Start a new terminal session)"