2012-11-09 20:44:06 -08:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-01-04 15:35:36 -08:00
|
|
|
################################################################################
|
|
|
|
#Copyright (C) 2013 Tony Grosinger. All rights reserved.
|
|
|
|
#
|
|
|
|
#Description:
|
|
|
|
# This script can be run in two modes depending on the file structure of the
|
|
|
|
# current working directory or the arguments passed to it.
|
|
|
|
#
|
|
|
|
# Mode 1: From within a cloned repository or downloaded snapshot with all files
|
|
|
|
# already present and ready to be symlinked
|
|
|
|
#
|
|
|
|
# Mode 2: Standalone or through Curl in which case all required files will be
|
|
|
|
# downloaded automatically to `~/.dotfiles` before creating symlinks.
|
|
|
|
#
|
|
|
|
#Change History:
|
|
|
|
# Date Author Description
|
|
|
|
# ---------- -------------- ------------------------------------
|
|
|
|
# 2014-01-04 agrosinger Updated to two mode operation
|
|
|
|
################################################################################
|
|
|
|
|
2012-11-09 20:44:06 -08:00
|
|
|
|
2014-01-04 15:35:36 -08:00
|
|
|
################################################################################
|
|
|
|
# Define functions
|
|
|
|
################################################################################
|
2012-11-09 20:44:06 -08:00
|
|
|
|
2014-01-03 13:37:43 -08:00
|
|
|
# Remove a file if it exists then create a symlink to the one contained in ${REPO_DIR}
|
2013-03-21 20:56:52 -07:00
|
|
|
function linkFile() {
|
2014-01-03 13:37:43 -08:00
|
|
|
if [ -e $1 ]; then rm -rf $1; fi
|
|
|
|
ln -s ${REPO_DIR}/$1 $1;
|
2013-03-21 20:56:52 -07:00
|
|
|
}
|
|
|
|
|
2014-01-03 13:37:43 -08:00
|
|
|
# Create a directory named by first parameter. Delete directory first if it already exists.
|
2013-03-21 20:56:52 -07:00
|
|
|
function createDirectory() {
|
2014-01-03 13:37:43 -08:00
|
|
|
if [ -d $1 ]; then rm -rf $1; fi
|
2013-03-21 20:56:52 -07:00
|
|
|
mkdir $1
|
|
|
|
}
|
|
|
|
|
2014-01-04 15:35:36 -08:00
|
|
|
# Perform the actual work of copying files and creating symlinks.
|
|
|
|
# Requires a home directory argument.
|
|
|
|
function performSetup() {
|
|
|
|
if [ ${#} != 1 ]; then
|
|
|
|
echo "Must pass home directory to ${FUNCNAME}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
home=${1}
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Moving to Home directory..."
|
|
|
|
pushd ${home} > /dev/null
|
2014-01-04 15:35:36 -08:00
|
|
|
|
|
|
|
echo "Adding id_rsa.pub to authorized keys if necessary"
|
2014-01-04 15:54:03 -08:00
|
|
|
if [ ! -d .ssh ]; then
|
|
|
|
mkdir .ssh
|
|
|
|
fi
|
|
|
|
if [ ! -e ".ssh/authorized_keys" ]; then
|
2014-01-04 15:35:36 -08:00
|
|
|
cp ${REPO_DIR}/.ssh/id_rsa.pub .ssh/authorized_keys
|
2014-01-04 16:08:11 -08:00
|
|
|
elif ! grep -f ${REPO_DIR}/.ssh/id_rsa.pub .ssh/authorized_keys; then
|
2014-01-04 15:35:36 -08:00
|
|
|
cat ${REPO_DIR}/.ssh/id_rsa.pub >> .ssh/authorized_keys
|
|
|
|
fi
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Linking shell configs..."
|
2014-01-04 15:35:36 -08:00
|
|
|
linkFile ".bashrc"
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Linking vim..."
|
2014-01-04 15:35:36 -08:00
|
|
|
linkFile ".vimrc"
|
|
|
|
createDirectory ".vim"
|
|
|
|
createDirectory ".vim/swaps"
|
|
|
|
createDirectory ".vim/backups"
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Linking Git..."
|
2014-01-04 15:35:36 -08:00
|
|
|
linkFile ".gitconfig"
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Linking tmux..."
|
2014-01-04 15:35:36 -08:00
|
|
|
linkFile ".tmux.conf"
|
|
|
|
|
|
|
|
echo "Linking inputrc..."
|
|
|
|
linkFile ".inputrc"
|
|
|
|
|
2014-01-04 16:08:11 -08:00
|
|
|
popd > /dev/null
|
2014-01-04 15:35:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Initialize (Determine mode)
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Determine which mode we are running in. (Is the file in the current directory?)
|
|
|
|
if [ -f install.sh ]; then
|
|
|
|
echo "Running in Mode 1: Already cloned repository"
|
|
|
|
REPO_DIR="$( cd "$( dirname "$0" )" && pwd )"
|
2014-01-04 15:50:07 -08:00
|
|
|
|
|
|
|
echo "This will create symlinks and destroy any conflicting configs already in place.";
|
|
|
|
read -p "Continue? [y/N] " choice
|
|
|
|
|
|
|
|
# Perform the logic
|
|
|
|
case "$choice" in
|
|
|
|
Y|y|yes )
|
|
|
|
performSetup ${HOME}
|
|
|
|
echo "Done! Restart your shell to see changes"
|
|
|
|
;;
|
|
|
|
* ) echo "Aborted!";;
|
|
|
|
esac
|
2014-01-04 15:35:36 -08:00
|
|
|
else
|
|
|
|
echo "Running in Mode 2: Direct from Curl"
|
|
|
|
|
|
|
|
# Make sure git is installed. Exit if it isn't.
|
|
|
|
if ! which git; then
|
|
|
|
echo "Please install git before continuing."
|
|
|
|
echo "Alternatively, download the files and run manually from https://github.com/tgrosinger/dotfiles/archive/master.zip"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Clone the repository into current location using readonly url.
|
2014-01-04 15:50:07 -08:00
|
|
|
if [ -d ${HOME}/.dotfiles ]; then
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Updating dotfiles repository in ${HOME}/.dotfiles"
|
|
|
|
pushd ${HOME}/.dotfiles > /dev/null
|
|
|
|
git pull > /dev/null
|
|
|
|
popd > /dev/null
|
2014-01-04 15:50:07 -08:00
|
|
|
else
|
2014-01-04 16:08:11 -08:00
|
|
|
echo "Cloning dotfiles repository to ${HOME}/.dotfiles"
|
2014-01-04 15:50:07 -08:00
|
|
|
git clone https://github.com/tgrosinger/dotfiles.git ${HOME}/.dotfiles
|
|
|
|
fi
|
|
|
|
REPO_DIR="${HOME}/.dotfiles"
|
|
|
|
performSetup ${HOME}
|
|
|
|
echo "Done! Restart your shell to see changes"
|
2014-01-04 15:35:36 -08:00
|
|
|
fi
|