dotfiles/install.sh

236 lines
7.7 KiB
Bash
Executable File

#!/bin/bash
## brethil's dotfiles installation script
## 20 June 2016
# Get the location for the dotfiles
DOTFILES=$PWD
# Install packages
# vim, zsh
# antigen: zsh plugin manager
# grc: generic colourizer, colors output of any command
# ccze: similar to the above
# byobu: configuration/wrapper for tmux
# bmon: bandwidth monitor, monitors bandwith usage, shows graph
# mtr: mytraceroute, traceroute tool
# pv: pipe view, monitor the progress of data through a pipe
# byobu: tmux wrapper
# htop: like top, but bettah
# bat: a cat clone on wings
PACKAGES="vim zsh antigen git grc ccze bmon mtr pv tmux byobu htop bat"
function setup_git {
# set up some git stuff
git config --global pull.ff only
git config --global merge.ff only
}
function install_vimrc {
# Install vimrc from github.com/amix/vimrc
git clone "https://github.com/amix/vimrc.git" "$HOME/.vim_runtime" && sh "$HOME/.vim_runtime/install_awesome_vimrc.sh" || return 1
}
function setup_vim {
ln -s "$DOTFILES/my_configs.vim" "$HOME/.vim_runtime/my_configs.vim"
mkdir -p ~/.vim_runtime/my_plugins/twilight/colors
ln -s "$DOTFILES/twilight256.vim" ~/.vim_runtime/my_plugins/twilight/colors/
(cd ~/.vim_runtime/my_plugins && git add -f * && git commit -m "bretello dotfiles")
}
# Add an ssh config file with:
# - Connection multiplexer for faster multiple connections
# - Keep connections alive (avoid timeout disconnections)
function create_ssh_config {
echo "# Configuring ssh..."
ssh_config="$HOME/.ssh/config"
if [ ! -f $HOME/.ssh/id_rsa ]; then
echo "Creating ssh key (4096bit)..."
echo "Enter ssh-key comment (leave empty for default: user@host)"
read comment
if [[ $comment ]]; then
ssh-keygen -t rsa -b 4096 -C $comment
else
ssh-keygen -t rsa -b 4096
fi
# fix permissions
chmod 0700 "$HOME/.ssh"
fi
if [[ -f $ssh_config ]]; then
until [[ $modifyssh == "y" || $modifyssh == "n" ]]; do
echo "Do you want to modify the existing ssh config? (New values will be appended) (y/n)"
read modifyssh
done
fi
exec 3>&1 # save stdout file descriptor
# add the ssh rc that symlinks the used SSH_AUTH_SOCK. This will be executed on every ssh login.
# The idea is that if the user is logging in using ssh -A, the symlink will point to the correct
# location of the ssh auth socket and the remote ssh agent will be used.
# SSH_AUTH_SOCK is declared in brethil_dotfile.sh
exec 1>>~/.ssh/rc
echo '#!/bin/bash'
echo 'if test "$SSH_AUTH_SOCK" ; then'
echo ' ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock'
echo 'fi '
chmod 755 ~/.ssh/rc
exec 1>>$ssh_config # redirect everything below to the $ssh_config file
if [[ "$modifyssh" == "n" ]]; then
echo "Did not modify $ssh_config."
exec 1>&3 # restore stdout
return
fi
echo "# brethil's dotfiles setup start"
echo 'TCPKeepAlive=yes'
echo 'ServerAliveCountMax=6'
echo
echo "## Uncomment to enable compression for all ssh sessions"
echo '#Compression=yes'
echo
echo '## Uncomment the following to enable ssh ControlMaster and ssh session persistence'
echo '#ControlMaster auto'
echo '#ControlPath ~/.ssh/%r@%h:%p'
echo '#ControlPersist yes'
echo
echo 'Host *'
echo ' ServerAliveInterval 300'
echo
echo '## Enable the following if you want to use the rmate textmate remote'
echo "#Host *"
echo "# RemoteForward 52698 localhost:52698"
echo
echo '## Enable the following if you want to use a reverse ssh tunnel to use mecp command on remote hosts'
echo "#Host *"
echo "# Remoteforward 2222 localhost:22"
echo
echo '# end of brethil dotfiles setup #'
exec 1>&3 # restore stdout
echo ".ssh/ssh_config configured. Edit it to enable custom options:"
echo "- Compression"
echo "- Remote forwarding remote:2222->localhost:22 (revere tunnel to scp back to the original host using mecp)"
echo "# End of ssh config."
}
function fix_annoyances {
## Fix scrolling in byobu
if [[ $(uname) == "Darwin" ]]; then
sed -i '' 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux
else
sed -i 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux
fi
# remove ls from the grc.zsh config: # TODO: fix this on MacOS
sed 's|ls \\|#ls \\|' /etc/grc.zsh
# TODO: add iptables, docker to grc.zsh
}
# First setup
function brethil_dotfiles_setup {
bin="$HOME/bin"
projects="$HOME/projects"
git="$HOME/git"
mkdir -p "$bin" "$projects" "$git" && echo "Created dirs $bin, $projects, $git"
# Check if running as root, if try using sudo to install packages.
# If sudo is not installed, launch a shell to try and install it
if [[ $(id -u) != 0 ]]; then
if [[ ! -f $(which sudo) ]] ; then
unset yn
until [[ $yn == "y" || $yn == "n" ]]; do
echo "Cannot install new packages without root access. Do you want to try to manually install sudo? (yn) "
read yn
done
if [[ $yn == "y" ]]; then
echo "Launching an interactive shell. Type exit after installing sudo."
# Launch bash
$(which bash)
else
echo "Quitting."
exit 0
fi
elif [[ -f $(which sudo) ]]; then
sudo="sudo "
fi
fi
if [[ $(which apt-get ) ]]; then
PACKAGES=${PACKAGES/antigen/zsh-antigen/}
antigen_path=/usr/share/zsh-antigen/antigen.zsh
install_command="${sudo}apt-get install"
elif [[ $(which pacman) ]]; then
pacman -Sy yay # also install the yay aur package managery
antigen_path=/usr/share/zsh/share/antigen.zsh
grc_path=/etc/grc.zsh
install_command="${sudo}yay -Sy"
elif [[ $(which yum) ]]; then
antigen_path=/usr/share/zsh/share/antigen.zsh
install_command="${sudo}yum"
elif [[ $(which brew) ]]; then
antigen_path=/usr/local/share/antigen/antigen.zsh
install_command="${sudo}brew install"
else
echo "I do not know how to install the required packages. Quitting."
exit 0
fi
if [[ ! -f $antigen_path ]]; then
echo "antigen.zsh not found, please set up manually in ~/.zshrc" 1>&2
exit 0
fi
# actually install the packages
$install_command $PACKAGES
# prepare .zshrc
exec 3>&1 # save stdout
exec 1>$HOME/.zshrc
echo "source $antigen_path"
echo -e "\n\n# brethil's dotfiles:"
# Source antigen
echo "source $antigen_path"
echo "DOTFILES=$DOTFILES"
echo "source \$DOTFILES/brethil_dotfile.sh"
echo -e "# End of brethil's dotfiles\n\n"
exec 1>&3 # restore stdout
fix_annoyances
# Install vim awesomerc (git amix/vimrc)
install_vimrc && echo "Installed vim awesome rc" || echo "Failed to install vim awesome rc"
setup_vim
# Create ssh config
create_ssh_config
# git config
setup_git
# pdbpp rc
setup_pdbprc
# Symlink brethil.zsh-theme
ln -s $DOTFILES/brethil.zsh-theme $HOME/.antigen/bundles/robbyrussell/oh-my-zsh/themes/ # TODO: improve this
# Symlink brethil-minimal.zsh-theme
ln -s $DOTFILES/brethil-minimal.zsh-theme $HOME/.antigen/bundles/robbyrussell/oh-my-zsh/themes/
# TODO: powerline?
}
# Setup
brethil_dotfiles_setup
echo "Functions definitions in $DOTFILES/functions.sh (you can add your own functions in ~/.dotfiles_functions)"
echo "Aliases definitions in $DOTFILES/aliases.sh (you can add your own aliases in ~/.dotfiles_aliases)"
echo "Colors are defined in $DOTFILES/colors.sh"
echo "Type . ~/.zshrc to source the new dotfiles, or simply launch another shell. )"