#!/usr/bin/env bash # # download and setup zsh, oh-my-zsh and various zsh plugins # # author: david@socialnerds.org # license: MIT # git: https://socialg.it/david/scripts.git # version 0.1 # **** vars **** command="$0" root="$HOME/.dotfiles" plugins="$root/zsh" timestamp=$(date +"%Y%m%d%H%M") debug=1 remove=".dotfiles .dotfiles.old .myzsh .oh-my-zsh .zshrc .zshrc-pre-oh-my-zsh" dependencies="awk tee ls git wget curl rm mv sleep date" if [ $(uname) == "Darwin" ]; then alias sed="gsed" #TODO: check if gsed is there first fi # **** functions **** message() { if [ $1 == "e" ]; then level="\033[31m✗\033[0m" elif [ $1 == "i" ]; then level="\033[32m✓\033[0m" elif [ $1 == "w" ]; then level="\033[33m!\033[0m" elif [ $1 == "d" ]; then if [ $debug -eq 1 ]; then level="\033[2md\033[0m" else return 0 fi else message e "Unknown message level[$1]." return 1 fi echo -e [$level] ${@:2} sleep 0.1 } failsafe() { if [ $1 -ne 0 ]; then message e "Installer died horribly!" if [ $2 ]; then message d "Dumping last output to stdout." echo ${@:2} fi exit 1 fi } cleanup() { # Get rid of preexisting data at destination destination=$1 if [ -a "$destination.old" ]; then message e "A previous backup exists. Move elsewhere or delete first[$destination.old]." exit 1 else if [ -h $destination ]; then link=$(ls -la $destination | awk '{print $9" "$10" "$11}') # It should be safe to just remove symbolic links because no data is actually deleted. message d "Symbolic link exists, attempting to remove it[$link]." output=$(rm $destination 2>&1); failsafe $? $output message i "Removed symbolic link[$link]." elif [ -a $destination ]; then # Rename data to $destination.old message d "Data exists at install destination, attempting to create a backup[$destination.old]." output="", output=$(mv $destination $destination.old 2>&1); failsafe $? $output message i "Created a backup of a preexisting data[$destination.old]." fi fi } # **** start of script **** message d "Warming up installer." #message d "Initiating preflight checks." # check dependencies missing="" for item in $dependencies; do command -v $item > /dev/null if [ $? -ne 0 ]; then missing="$missing $item" fi done if [ -n "$missing" ]; then message e "dependencies missing: $missing" exit 1 fi message i "all dependencies found" # remove everything in 5 seconds: ~/.dotfiles ~/.dotfiles.old ~/.myzsh ~/.oh-my-zsh ~/.zshrc message w "your old configs will be deleted in 5 seconds [terminate deletion with Ctrl-C]" sleep 5 for item in $remove; do if [ -d $item ]; then rm -rf $HOME/$item message w "removed folder: ~/$item" elif [ -h $item ]; then rm $HOME/$item message w "removed link: ~/$item" elif [ -f $item ]; then rm $HOME/$item message w "removed file: ~/$item" fi done # invoke oh-my-zh installer if [ -w $HOME ]; then curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash message i "oh-my-zsh installed" else message e "oh-my-zsh could not be installed. $HOME is not writeable" exit 1 fi # clone plugins cd $HOME/.oh-my-zsh/custom/plugins/ > /dev/null git clone https://github.com/sindresorhus/pure.git pure git clone https://github.com/mafredri/zsh-async.git async git clone https://socialg.it/david/zsh-david.git zsh-david git clone https://github.com/zsh-users/zsh-syntax-highlighting.git zsh-syntax-highlighting git clone https://github.com/skx/sysadmin-util.git sysadmin-util git clone https://github.com/TamCore/autoupdate-oh-my-zsh-plugins.git autoupdate git clone https://github.com/zsh-users/zsh-autosuggestions.git zsh-autosuggestions cd - > /dev/null message i "all plugins cloned" # set $ZSH_THEME="" in .zshrc sed -i "s/^ZSH_THEME=\"robbyrussell\"/ZSH_THEME=\"\"/g" $HOME/.zshrc # set $plugins in .zshrc plugins="git zsh-autosuggestions autoupdate sysadmin-util zsh-syntax-highlighting zsh-david async pure" sed -i "/plugins=(/,/)/d; s/source \$ZSH\/oh-my-zsh.sh/plugins=($plugins);; source \$ZSH\/oh-my-zsh.sh/g" $HOME/.zshrc sed -i $'s/;; /\\\n/g' $HOME/.zshrc message i "tweaked ~/.zshrc" message d "Installer finished." # **** end of script ****