#!/bin/bash
# This symlinks all the dotfiles (and .atom/) to ~/
# It also symlinks ~/bin for easy updating
# This is safe to run multiple times and will prompt you about anything unclear
#
# Utils
#
answer_is_yes( ) {
[ [ " $REPLY " = ~ ^[ Yy] $ ] ] \
&& return 0 \
|| return 1
}
ask( ) {
print_question " $1 "
read
}
ask_for_confirmation( ) {
print_question " $1 (y/n) "
read -n 1
printf "\n"
}
ask_for_sudo( ) {
# Ask for the administrator password upfront
sudo -v
# Update existing `sudo` time stamp until this script has finished
# https://gist.github.com/cowboy/3118588
while true; do
sudo -n true
sleep 60
kill -0 " $$ " || exit
done & > /dev/null &
}
cmd_exists( ) {
[ -x " $( command -v " $1 " ) " ] \
&& printf 0 \
|| printf 1
}
execute( ) {
$1 & > /dev/null
print_result $? " ${ 2 :- $1 } "
}
get_answer( ) {
printf " $REPLY "
}
get_os( ) {
declare -r OS_NAME = " $( uname -s) "
local os = ""
if [ " $OS_NAME " = = "Darwin" ] ; then
os = "osx"
elif [ " $OS_NAME " = = "Linux" ] && [ -e "/etc/lsb-release" ] ; then
os = "ubuntu"
fi
printf "%s" " $os "
}
is_git_repository( ) {
[ " $( git rev-parse & >/dev/null; printf $? ) " -eq 0 ] \
&& return 0 \
|| return 1
}
mkd( ) {
if [ -n " $1 " ] ; then
if [ -e " $1 " ] ; then
if [ ! -d " $1 " ] ; then
print_error " $1 - a file with the same name already exists! "
else
print_success " $1 "
fi
else
execute " mkdir -p $1 " " $1 "
fi
fi
}
print_error( ) {
# Print output in red
printf " \e[0;31m [✖] $1 $2 \e[0m\n "
}
print_info( ) {
# Print output in purple
printf " \n\e[0;35m $1 \e[0m\n\n "
}
print_question( ) {
# Print output in yellow
printf " \e[0;33m [?] $1 \e[0m "
}
print_result( ) {
[ $1 -eq 0 ] \
&& print_success " $2 " \
|| print_error " $2 "
[ " $3 " = = "true" ] && [ $1 -ne 0 ] \
&& exit
}
print_success( ) {
# Print output in green
printf " \e[0;32m [✔] $1 \e[0m\n "
}
dir = ~/dotfiles # dotfiles directory
dir_backup = ~/dotfiles_old # old dotfiles backup directory
# Get current dir (so run this script from anywhere)
export DOTFILES_DIR
DOTFILES_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
# Create dotfiles_old in homedir
echo -n " Creating $dir_backup for backup of any existing dotfiles in ~... "
mkdir -p $dir_backup
echo "done"
# Change to the dotfiles directory
echo -n " Changing to the $dir directory... "
cd $dir
echo "done"
#
# Actual symlink stuff
#
# Atom editor settings
echo -n "Copying Atom settings.."
mv -f ~/.atom ~/dotfiles_old/
ln -s $HOME /dotfiles/atom ~/.atom
echo "done"
declare -a FILES_TO_SYMLINK = (
'shell/shell_aliases'
'shell/shell_config'
'shell/shell_exports'
'shell/shell_functions'
'shell/bash_profile'
'shell/bash_prompt'
'shell/bashrc'
'shell/zshrc'
'shell/ackrc'
'shell/curlrc'
'shell/gemrc'
'shell/inputrc'
'shell/screenrc'
'git/gitattributes'
'git/gitconfig'
'git/gitignore'
)
# FILES_TO_SYMLINK="$FILES_TO_SYMLINK .vim bin" # add in vim and the binaries
# Move any existing dotfiles in homedir to dotfiles_old directory, then create symlinks from the homedir to any files in the ~/dotfiles directory specified in $files
for i in ${ FILES_TO_SYMLINK [@] } ; do
echo " Moving any existing dotfiles from ~ to $dir_backup "
mv ~/.${ i ##*/ } ~/dotfiles_old/
done
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main( ) {
local i = ''
local sourceFile = ''
local targetFile = ''
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for i in ${ FILES_TO_SYMLINK [@] } ; do
sourceFile = " $( pwd ) / $i "
targetFile = " $HOME /. $( printf "%s" " $i " | sed "s/.*\/\(.*\)/\1/g" ) "
if [ ! -e " $targetFile " ] ; then
execute " ln -fs $sourceFile $targetFile " " $targetFile → $sourceFile "
elif [ " $( readlink " $targetFile " ) " = = " $sourceFile " ] ; then
print_success " $targetFile → $sourceFile "
else
ask_for_confirmation " ' $targetFile ' already exists, do you want to overwrite it? "
if answer_is_yes; then
rm -rf " $targetFile "
execute " ln -fs $sourceFile $targetFile " " $targetFile → $sourceFile "
else
print_error " $targetFile → $sourceFile "
fi
fi
done
# Copy batcharge
ln -fs $HOME /dotfiles/bin $HOME
chmod +rwx $HOME /bin/batcharge.py
# Symlink online-check.sh
ln -fs $HOME /dotfiles/lib/online-check.sh $HOME /online-check.sh
# Write out current crontab
crontab -l > mycron
# Echo new cron into cron file
echo "* * * * * ~/online-check.sh" >> mycron
# Install new cron file
crontab mycron
rm mycron
}
install_zsh ( ) {
# Test to see if zshell is installed. If it is:
if [ -f /bin/zsh -o -f /usr/bin/zsh ] ; then
# Install Oh My Zsh if it isn't already present
if [ [ ! -d $dir /oh-my-zsh/ ] ] ; then
sh -c " $( curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh) "
fi
# Set the default shell to zsh if it isn't currently set to zsh
if [ [ ! $( echo $SHELL ) = = $( which zsh) ] ] ; then
chsh -s $( which zsh)
fi
else
# If zsh isn't installed, get the platform of the current machine
platform = $( uname) ;
# If the platform is Linux, try an apt-get to install zsh and then recurse
if [ [ $platform = = 'Linux' ] ] ; then
if [ [ -f /etc/redhat-release ] ] ; then
sudo yum install zsh
install_zsh
fi
if [ [ -f /etc/debian_version ] ] ; then
sudo apt-get install zsh
install_zsh
fi
# If the platform is OS X, tell the user to install zsh :)
elif [ [ $platform = = 'Darwin' ] ] ; then
echo "We'll install zsh, then re-run this script!"
brew install zsh
exit
fi
fi
}
# Package managers & packages
# . "$DOTFILES_DIR/install/brew.sh"
# . "$DOTFILES_DIR/install/npm.sh"
# if [ "$(uname)" == "Darwin" ]; then
# . "$DOTFILES_DIR/install/brew-cask.sh"
# fi
main
# install_zsh
###############################################################################
# Atom #
###############################################################################
# Copy over Atom configs
#cp -r atom/packages.list $HOME/.atom
# Install community packages
#apm list --installed --bare - get a list of installed packages
#apm install --packages-file $HOME/.atom/packages.list
###############################################################################
# Zsh #
###############################################################################
# Install Zsh settings
ln -s ~/dotfiles/zsh/themes/nick.zsh-theme $HOME /.oh-my-zsh/themes
###############################################################################
# Terminal & iTerm 2 #
###############################################################################
# Only use UTF-8 in Terminal.app
defaults write com.apple.terminal StringEncodings -array 4
# Install the Solarized Dark theme for iTerm
open " ${ HOME } /dotfiles/iterm/themes/Solarized Dark.itermcolors "
# Don’t display the annoying prompt when quitting iTerm
defaults write com.googlecode.iterm2 PromptOnQuit -bool false
# Reload zsh settings
source ~/.zshrc