dotfiles/install

336 lines
9.7 KiB
Bash
Executable File

#!/bin/bash
#######################################################################
# Default settings
#######################################################################
# Use git by default
BUNDLE_TRANSPORT="git"
#######################################################################
# Helper functions
#######################################################################
# Make a folder (throw errors if it's already there but not a folder)
mkfolder()
{
if [[ -e $1 ]]
then
if [[ ! -d $1 ]]
then
echo "$1 is not a directory!"
echo "Backing up $1 to $1.bak"
mv -v $1 $1.bak
mkdir -p $1
fi
else
mkdir -p $1
fi
}
# Symlink file to destination folder
lnfile()
{
# See if we are linking to a directory
if [[ $2 =~ /$ ]]
then
FILE_PATH=$2
FILE_NAME=$(basename $1)
DST_FILE="${FILE_PATH}${FILE_NAME}"
else
FILE_PATH=$(dirname $2)
FILE_NAME=$(basename $2)
DST_FILE="$FILE_PATH/$FILE_NAME"
fi
SRC_FILE="$DF_PATH/$1"
WRITE_LINK=0
# See if the target exists
if [[ -e $DST_FILE ]]
then
# Is the target a symbolic link?
if [[ -L $DST_FILE ]]
then
POINTS_TO=$(readlink $DST_FILE)
# Does it point to our file?
if [[ "$POINTS_TO" != "$SRC_FILE" ]]
then
echo "Target $DST_FILE points to a different file - $POINTS_TO"
echo -n "Replace link to point to $SRC_FILE? [y/n] "
read -n1
echo
if [[ $REPLY == y || $REPLY == Y ]]
then
WRITE_LINK=1
# Remove the old symbolic link
rm -f $DST_FILE
fi
#else
# It already points to our file. No point in rewriting it.
fi
else
# No, it's not a symbolic link
echo "Target $DST_FILE is not a symbolic link!"
echo "Backing up to $DST_FILE.bak"
mv -v $DST_FILE ${DST_FILE}.bak
WRITE_LINK=1
fi
else
WRITE_LINK=1
fi
if [[ ! -z $DEBUG ]]
then
echo -n "Linking "
[[ ! -z $WRITE_LINK ]] && echo -n "(NOT) "
echo "'$DST_FILE' -> '$SRC_FILE'"
fi
if [[ $WRITE_LINK == 1 ]]
then
echo "Installing $1"
ln -sf "$SRC_FILE" "$DST_FILE"
fi
}
# Install/update vim bundles
vim_update_bundle()
{
BUNDLE=$(echo $1 | sed 's:^.*/::' | sed 's:\.git$::')
# If the bundle already exists, update it (if possible)
if [[ -d "$HOME/.vim/bundle/$BUNDLE" ]]
then
# Don't update the bundle if BUNDLE_NO_UPDATE is set
if [[ -d "$HOME/.vim/bundle/$BUNDLE/.git" ]]
then
if [[ -z $BUNDLE_NO_UPDATE ]]; then
echo "Updating bundle $BUNDLE..."
pushd "$HOME/.vim/bundle/$BUNDLE" >/dev/null
git pull
popd >/dev/null
echo
fi
else
echo "$BUNDLE is not a Git repository. Skipping update."
fi
else
echo "Installing bundle $BUNDLE..."
pushd "$HOME/.vim/bundle/" >/dev/null
git clone $1
popd >/dev/null
echo
fi
}
# Wrapper to vim_update_bundle
# Let's you specify the bundle repo in a short form if it resides on GitHub
# (and perhaps one of the repos in vim-scripts). You can still specify a full
# URL if necessary. However, that will not allow you to dynamically choose a
# transport layer at runtime
Bundle()
{
# Get the prefix ready
if [[ "$BUNDLE_TRANSPORT" == "ssh" ]]; then
# If the user specifies to use ssh as the transport
URL="git@github.com:"
elif [[ "$BUNDLE_TRANSPORT" == "https" ]]; then
# Use https as the transport
URL="https://github.com/"
elif [[ "$BUNDLE_TRANSPORT" == "git" ]]; then
# Use git as the transport
URL="git://github.com/"
else
echo "Invalid transport layer! Aborting!!!"
exit 1
fi
# Check the bundle format
if [[ $(echo $1 | grep "^[A-Za-z0-9-]\+\/[A-Za-z0-9._-]\+$") == $1 ]]; then
# Username/repo combo
URL="${URL}${1}.git"
elif [[ $(echo $1 | grep "^[A-Za-z0-9._-]\+$") == $1 ]]; then
# Bare repo name - must be from the vim-scripts user
URL="${URL}vim-scripts/${1}.git"
else
# I don't care, I'm specifying the URL directly
URL=$1
fi
vim_update_bundle "$URL"
}
#######################################################################
# Verify command line switches
#######################################################################
while getopts "nht:" OPTION
do
case "$OPTION" in
n)
echo "Skipping bundle updates"
BUNDLE_NO_UPDATE=1
;;
t)
echo "Using transport $OPTARG"
BUNDLE_TRANSPORT=$OPTARG
;;
h)
echo '
This script installs the dotfiles into various locations using symbolic
links.
Options:
--------
-n Will not update existing bundles, but will install new
bundles.
-t<option> Specify the transport for downloading bundles. Can be
one of git, https or ssh. ssh transport requires you to
have set up the necessary keys at GitHub
-h Displays this help message
'
exit 0
;;
\?)
exit 1
break
;;
esac
done
#######################################################################
# Install dotfiles
#######################################################################
# Install scripts/config files to corresponding destination folders
cd $(dirname $0)
DF_PATH=$(pwd)
PRINT_PATH=$(echo $DF_PATH | sed "s:^$HOME:~:")
echo Installing dotfiles from $PRINT_PATH
#######################################################################
# Install scripts
#######################################################################
# Create ~/bin folder
mkfolder ~/bin
# Install scripts
lnfile scripts/beep ~/bin/
lnfile scripts/mlog ~/bin/
lnfile scripts/stardate ~/bin/
lnfile scripts/settitle ~/bin/
lnfile scripts/smartwd ~/bin/
# Don't bother installing the note script on a Mac, it doesn't work that well
if [[ "$(uname)" != *"Darwin"* ]]
then
lnfile scripts/note ~/bin/
lnfile scripts/note ~/bin/n
fi
#######################################################################
# Install git configuration
#######################################################################
if [[ ! -e ~/.gitconfig ]]
then
echo "Installing gitconfig"
cp -v gitconfig ~/.gitconfig
echo -n "What is your default Git username? "
read $GIT_USER
echo -n "What is your default Git e-mail address? "
read $GIT_EMAIL
git config --global user.name $GIT_USER
git config --global user.email $GIT_EMAIL
fi
# Install git prompt functions
if [[ ! -e ~/.git_prompt.sh ]]
then
echo "Installing Git prompt functionality..."
curl -Sso ~/.git_prompt.sh \
https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh
fi
#######################################################################
# Install vimrc & plugins
#######################################################################
# Create VIM folders
mkfolder ~/.vim
mkfolder ~/.vim/autoload
mkfolder ~/.vim/bundle
mkfolder ~/.vim/colors
mkfolder ~/.vim/ftdetect
mkfolder ~/.vim/ftplugin
mkfolder ~/.vim/plugin
mkfolder ~/.vim/syntax
# Install vim files
lnfile vimrc ~/.vimrc
# Install my plugins
lnfile vim/plugin/long-lines.vim ~/.vim/plugin/
lnfile vim/plugin/match-brackets.vim ~/.vim/plugin/
lnfile vim/plugin/airline-settings.vim ~/.vim/plugin/
# Install my ftdetect plugins
lnfile vim/ftdetect/swig.vim ~/.vim/ftdetect/
lnfile vim/ftdetect/markdown.vim ~/.vim/ftdetect/
# Install my filetype plugins
lnfile vim/ftplugin/make.vim ~/.vim/ftplugin/
lnfile vim/ftplugin/perl.vim ~/.vim/ftplugin/
lnfile vim/ftplugin/ruby.vim ~/.vim/ftplugin/
lnfile vim/ftplugin/changelog.vim ~/.vim/ftplugin/
lnfile vim/ftplugin/markdown.vim ~/.vim/ftplugin/
#######################################################################
# Install/update Vim bundles
#######################################################################
Bundle tpope/vim-pathogen # pathogen
Bundle tpope/vim-fugitive # A Git wrapper so awesome, it
# should be illegal!
Bundle zaiste/tmux.vim # tmux config file syntax
Bundle altercation/vim-colors-solarized # Solarized
Bundle vimwiki # Vimwiki
Bundle msanders/snipmate.vim # snipMate
Bundle tomtom/tcomment_vim # tComment
Bundle bling/vim-airline # Airline is awesome!
#######################################################################
# Install tmux & screen config files
#######################################################################
# Install tmux configuration file
lnfile tmux.conf ~/.tmux.conf
# Install screen configuration file
lnfile screenrc ~/.screenrc
#######################################################################
# Install bashrc files
#######################################################################
if [[ -L ~/.bashrc.common ]]
then
PRINT_INST=0
else
PRINT_INST=1
fi
lnfile bash/bashrc.common ~/.bashrc.common
lnfile bash/bashrc.lscolors ~/.bashrc.lscolors
lnfile bash/bashrc.aliases ~/.bashrc.aliases
lnfile dircolors ~/.dir_colors
if [[ $PRINT_INST == 1 ]]
then
echo "Add the following line to your ~/.bashrc file"
echo " source ~/.bashrc.common"
fi