Files
dotfiles-zsh/setup.sh
Chuck Hawley (chawley/shawshank) b486a1077b Initial commit
2017-10-09 16:19:39 -04:00

168 lines
6.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#===============================================================================
#
# FILE: setup.sh
# USAGE: ./setup.sh
# DESCRIPTION: after pulling/cloning dotfiles repo, this script will check/create
# symlinks for for dotfiles. It checks for the presence of installed
# software (i.e. won't symlink a dotfile for applications that aren't
# installed) and will remove links for applications that are no longer
# installed. Safe to run after every git pull
# OPTIONS:
# REQUIREMENTS: This script replies on the 'command' command. Basically, it will run
# `command -v <dotfile>` and if there is no output, it assumes the app
# isn't installed. So this won't work for apps that aren't in your $PATH
# reference: http://www.cyberciti.biz/faq/unix-linux-shell-find-out-posixcommand-exists-or-not/
# NOTES:
# AUTHOR: C Hawley
# CREATED: 2015-10
# REVISION: Mon 09 Oct 2017 04:14:39 PM EDT
#
#===============================================================================
set -o nounset # Treat unset variables as an error
# specify where your dotfiles live
dotfiledir="$HOME/dotfiles-zsh"
# function to handle checking for existings links/files and updating
link() {
echo "symlinking ${1}"
# check for existing symlinks
if [ -h "${HOME}"/"${1}" ]; then
echo " - There's already a symlink named ${1}. Diff'ing."
# If there's an existing sylink, check if it's different than the dotfile
if [[ $(diff --brief "${HOME}"/"${1}" "${dotfiledir}"/"${1}") ]]; then
# If the links are different, ask if it should be replaced
echo " - Old link and new link are different:"
echo -n " - Replace existing symlink "${1}"? [Y/N] "
read replace
if [ "${replace}" = "Y" ] || [ "${replace}" = "y" ]; then
# If yes, unlink the old file and link the new one and exit
echo " - Unlinking old file and linking new file"
unlink "${HOME}"/"${1}"
ln -s "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
else
# If the link is the same, just exit function
echo " - Old link and new link are the same."
fi
# check for existing file/directory
elif [ -f "${HOME}"/"${1}" ] || [ -d "${HOME}"/"${1}" ]; then
echo " - There's already a file or directory named ${1}. Diff'ing."
if [[ $(diff --brief "${HOME}"/"${1}" "${dotfiledir}"/"${1}") ]]; then
echo " - Old file/dir and new link are different:"
echo -n " - Rename "${1}" and create new link? [Y/N] "
read replace
if [ "${replace}" = "Y" ] || [ "${replace}" = "y" ]; then
echo " - Renaming old file/dir and linking new file"
mv "${HOME}"/"${1}" "${HOME}"/"${1}"_original
ln -s "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
fi
else
# If there is no existing file, create one and exit
echo " - Creating link for ${1}"
ln -s "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
}
# function to handle checking for existings links/files and updating
copy() {
echo "copying ${1}"
# check for existing symlinks
if [ -h "${HOME}"/"${1}" ]; then
echo " - There's already a symlink named ${1}. Diff'ing."
# If there's an existing sylink, check if it's different than the dotfile
if [[ $(diff --brief "${HOME}"/"${1}" "${dotfiledir}"/"${1}") ]]; then
# If the links are different, ask if it should be replaced
echo " - Old link and new link are different:"
echo -n " - Replace existing symlink "${1}"? [Y/N] "
read replace
if [ "${replace}" = "Y" ] || [ "${replace}" = "y" ]; then
# If yes, unlink the old file and link the new one and exit
echo " - Unlinking old file and copying new file"
unlink "${HOME}"/"${1}"
cp -rv "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
else
# If the link is the same, just exit function
echo " - Old link and new link are the same."
fi
# check for existing file/directory
elif [ -f "${HOME}"/"${1}" ] || [ -d "${HOME}"/"${1}" ]; then
echo " - There's already a file or directory named ${1}. Diff'ing."
if [[ $(diff --brief "${HOME}"/"${1}" "${dotfiledir}"/"${1}") ]]; then
echo " - Old file/dir and new link are different:"
echo -n " - Rename "${1}" and copy newer? [Y/N] "
read replace
if [ "${replace}" = "Y" ] || [ "${replace}" = "y" ]; then
echo " - Renaming old file/dir and copying new file"
mv "${HOME}"/"${1}" "${HOME}"/"${1}"_original
cp -rv "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
fi
else
# If there is no existing file, create one and exit
echo " - Copying ${1}"
cp -rv "${dotfiledir}"/"${1}" "${HOME}"/"${1}"
fi
}
clear
echo "------------------------------------------"
echo " dotfiles repo setup script "
echo "------------------------------------------"
echo " Setting up symlinks "
echo "------------------------------------------"
echo ""
# Check/Install Oh-My-ZSH!
#-------------------------------------------------------------------------------
if [ ! -d $HOME/.oh-my-zsh ]; then
echo "OH NO! You don't have Oh-my-ZSH installed! I'll do it now!"
echo ""
echo ""
echo "When install is finished, re-run this setup script to symlink your dotfiles"
echo ""
echo "Press a key when ready"
read anykey
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
exit 0
else
echo "... nevermind - oh-my-zsh already installed!"
fi
# link .zshrc
#-------------------------------------------------------------------------------
link .zshrc
# link .zsh_aliases
#-------------------------------------------------------------------------------
link .zsh_aliases
# link .zsh_functions
#-------------------------------------------------------------------------------
link .zsh_functions
# Report on backed up files
#-------------------------------------------------------------------------------
echo "These original files were backed up as '*_original'"
ofiles=$(find "${HOME}" -maxdepth 1 -regex ".*_original$" | wc -l)
if [ "${ofiles}" -gt 0 ]; then
find "${HOME}" -maxdepth 1 -regex ".*_original$"
else
echo "<No Files Were Backed Up>"
fi
echo ""
echo "------------------------------------------"
echo "Done. "
echo "source ${HOME}/.zshrc "
echo "or "
echo "logout and login to enable .zshrc changes "
echo "------------------------------------------"