#!/bin/bash ## Author: crunchie ## Description: Script to automatically link up certain files in home-directory to pre-built versions. ## Backups of currently installed versions are made in a .dtbak-file. ## Links the .zshrc, .zshrc-theme and .aliases file to different files depending on whether a linux or mac platform is in use. ## install routine checks whether .oh-my-zsh exists in users home-folder - and install .oh-my-zsh unattended if not. ## if .oh-my-zsh is installed the version will be updated to the current version. # function to check if mac or linux check_os () { which sw_vers > /dev/null 2>&1 if [ $? == 0 ]; then os=mac else os=linux fi } # function to unlink symlink or mv original file to dtbak unlink_mv_file () { local file=$1 if [ -e $file ] || [ -L $file ]; then if [ -L $file ]; then unlink $file fi if [ -f $file ]; then mv -f $file{,.dtbak} fi fi } # function to check if the file exists in the home directory # return parameters: 2 = file exists, 3 = file does not exist check_exist () { local file=$1 if [ -e ~/$file ] || [ -L ~/$file ]; then return 2 else return 3 fi } # function to check whether .oh-my-zsh is installed. # installation is checked by looking if ~/.oh-my-zsh exists. check_oh-my-zsh () { if [ -d ~/.oh-my-zsh ]; then return 2 else return 3 fi } ## EXECUTE begins here # Check whether linux or macos is used check_os # Backup dotfiles and link to local versions for file in $(find ~/dotfiles -maxdepth 1 -name ".*" ! -name ".aliases*" ! -name ".zshrc*" ! -name ".zcompdum*" ! -name ".*history*" -type f -printf "%f\n" ); do check_exist $file stat_dot=$? if [ $stat_dot == 2 ]; then unlink_mv_file ~/$file ln -s ~/dotfiles/$file ~/$file elif [ $stat_dot == 3 ]; then ln -s ~/dotfiles/$file ~/$file fi done # Unlink or move existing .zshrc config or .zshrc-theme or .aliases file for file in .zshrc .zshrc-theme .aliases do check_exist $file if [ $? == 2 ]; then unlink_mv_file ~/$file fi done # check if .oh-my-zsh is installed - install if not, update if it is installed. check_oh-my-zsh inst=$? if [ $inst == 3 ]; then sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended else git -C ~/.oh-my-zsh/ pull fi # link the .zshrc-file in home-directory depending on the underlying os if [ $os == linux ];then ln -s ~/dotfiles/.zshrc-linux ~/.zshrc ln -s ~/dotfiles/.zshrc-linux-agnoster ~/.zshrc-theme ln -s ~/dotfiles/.aliases-linux ~/.aliases echo ".zshrc, .zshrc-theme and .aliases linked for linux." elif [ $os == mac ];then ln -s ~/dotfiles/.zshrc-mac ~/.zshrc ln -s ~/dotfiles/.zshrc-mac-agnoster ~/.zshrc-theme ln -s ~/dotfiles/.aliases-mac ~/.aliases echo ".zshrc, .zshrc-theme and .aliases linked for mac" else echo "Unknown OS - no .zshrc, .zshrc-theme or .aliases linked." fi echo "Installation done." touch ~/dotfiles/version.installed exit 0