mirror of https://github.com/nirenjan/dotfiles.git
				
				
				
			
		
			
				
	
	
		
			241 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			241 lines
		
	
	
		
			6.1 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
 | |
| }
 | |
| 
 | |
| #######################################################################
 | |
| # Verify command line switches
 | |
| #######################################################################
 | |
| while getopts "h" OPTION
 | |
| do
 | |
|     case "$OPTION" in
 | |
|         h)
 | |
|             echo '
 | |
| This script installs the dotfiles into various locations using symbolic
 | |
| links.
 | |
| 
 | |
| Options:
 | |
| --------
 | |
|     -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 git configuration
 | |
| #######################################################################
 | |
| if [[ ! -e ~/.git-commit-template ]]
 | |
| then
 | |
|     echo "Installing Git commit template"
 | |
|     cp -v gitcommit ~/.git-commit-template
 | |
| fi
 | |
| 
 | |
| 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"
 | |
| else
 | |
|     echo "Applying gitconfig"
 | |
|     ./scripts/applygitconfig gitconfig
 | |
| fi
 | |
| 
 | |
| # Install git prompt functions
 | |
| if [[ ! -e ~/.git_prompt.sh ]]
 | |
| then
 | |
|     echo "Installing Git prompt functionality..."
 | |
|     curl -Sso ~/.git_prompt.sh \
 | |
|     https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
 | |
| fi
 | |
| 
 | |
| #######################################################################
 | |
| # Install scripts
 | |
| #######################################################################
 | |
| 
 | |
| # Create ~/bin folder
 | |
| mkfolder ~/bin
 | |
| 
 | |
| # Install scripts
 | |
| lnfile scripts/stardate ~/bin/
 | |
| lnfile scripts/settitle ~/bin/
 | |
| lnfile scripts/smartwd ~/bin/
 | |
| 
 | |
| #######################################################################
 | |
| # Install vimrc & plugins
 | |
| #######################################################################
 | |
| 
 | |
| # Create VIM folders
 | |
| mkfolder ~/.vim
 | |
| 
 | |
| # Update the Git repository at ~/.vim, if it exists, clone otherwise
 | |
| pushd ~/.vim &>/dev/null
 | |
| if [[ -d .git ]]
 | |
| then
 | |
|     git pull
 | |
| else
 | |
|     git clone my:vimfiles.git .
 | |
| fi
 | |
| 
 | |
| git submodule init
 | |
| git submodule update
 | |
| 
 | |
| popd &>/dev/null
 | |
| 
 | |
| # Install the basic vimrc file from this repo
 | |
| lnfile vimrc ~/.vimrc
 | |
| 
 | |
| #######################################################################
 | |
| # 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.d ~/.bashrc.d
 | |
| lnfile dircolors ~/.dircolors
 | |
| 
 | |
| if [[ $PRINT_INST == 1 ]]
 | |
| then
 | |
|     echo "Add the following line to your ~/.bashrc file"
 | |
|     echo "    source ~/.bashrc.common"
 | |
| fi
 | |
| 
 | |
| #######################################################################
 | |
| # Install zsh files
 | |
| #######################################################################
 | |
| lnfile zshrc ~/.zshrc
 | |
| mkfolder ~/.zshrc.d
 | |
| mkfolder ~/.zshrc.d/functions
 | |
| for plugin in zsh/*.zsh
 | |
| do
 | |
|     lnfile $plugin ~/.zshrc.d/
 | |
| done
 | |
| 
 |