Dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.8 KiB

11 years ago
#!/bin/zsh
# .files/install
# (c) 2014-2017 MORI Taeyeon
11 years ago
# Paths & Utils
DOT="$(realpath "$(dirname "$0")")"
cd "$DOT"
11 years ago
setopt EXTENDED_GLOB
source "$DOT/lib/libzsh-utils.zsh"
11 years ago
11 years ago
# Parse commandline
# =============================
11 years ago
OVERWRITE=false
ASK=true
11 years ago
function print_usage() {
echo "Usage: $1 [--help] [--overwrite] [--dont-ask]"
11 years ago
echo
echo "Install Tae's dotfiles"
echo
echo "Parameters:"
echo " -h, --help Display this help message"
echo " --overwrite Just overwrite existing dotfiles"
echo " --dont-ask Don't ask questions. Use default values"
11 years ago
}
for arg in "$@"; do
[[ -z "$STATE" ]] && {
case "$arg" in
-h|--help) print_usage "$0"; exit 0;;
11 years ago
--overwrite) OVERWRITE=true;;
--dont-ask) ASK=false;;
*) print_usage "$0"; exit 1;;
11 years ago
esac
} || {
case "$STATE" in
# handle options with parameters
esac
STATE=
}
done
[[ -n "$STATE" ]] && err "Option $STATE is missing it's parameter!" && exit 1
11 years ago
# Helper functions
# =============================
11 years ago
function generate() {
[[ -e "$1" ]] && ! $OVERWRITE && ! grep -q "Generated by .files/install" "$1" && \
err "Custom version of $1 detected. Please delete it before running .files/install" && exit 1
10 years ago
echo "${2-#} Generated by .files/install" >"$1"
echo "${2-#} `date "+%m/%d/%Y %H:%M:%S"`" >>"$1"
11 years ago
echo >>"$1"
cat >>"$1"
}
function relink() {
[[ ! -e "$1" ]] && err "No such file or directory: '$1'" && return 1
[[ -L "$2" ]] && rm -f "$2"
[[ -e "$2" ]] && ! $OVERWRITE && err "File already exists and isn't a symbolic link: '$2'" && return 1
color 31 ln -s "$(relpath "$1" "$(dirname "$2")")" "$2"
11 years ago
}
# Prepare
# =============================
11 years ago
# Update git modules
msg "Updating git submodules..."
git submodule update --init --recursive
# Ask questions about user
$ASK && msg "Asking questions..." || err "Not asking any questions:"
11 years ago
[[ -e "$DOT/etc/user-info" ]] && source "$DOT/etc/user-info"
11 years ago
[[ -z "$REALNAME" ]] && REALNAME=`grep "^$USER:[^:]*:$UID:" /etc/passwd | cut -d: -f5 | cut -d, -f1`
if $ASK; then
ask " Real name" REALNAME
ask " E-Mail address" EMAIL
else
color 36 echo " default REALNAME=\"$REALNAME\""
color 36 echo " default EMAIL=\"$EMAIL\""
fi
11 years ago
# Write down answers
color 32 echo " Writing etc/user-info"
generate "$DOT/etc/user-info" <<EOF
11 years ago
REALNAME="$REALNAME"
EMAIL="$EMAIL"
EOF
# Write files to $HOME
# =============================
11 years ago
# Setup dotfiles
msg "Linking dotfiles..."
for file in "$DOT/dotfiles"/*; do
NAM="${file:t}"
color 32 echo " Adding .$NAM"
relink "$file" "$HOME/.$NAM"
done
# Setup .config
: ${XDG_CONFIG_HOME:=~/.config}
msg "Linking .config files... ($XDG_CONFIG_HOME)"
(
cd "$DOT/config"
for dir in $(find . -type d); do
if [ ! -d "$XDG_CONFIG_HOME/$dir" ]; then
color 32 echo " Creating ${dir:2}"
mkdir "$XDG_CONFIG_HOME/$dir"
fi
done
for file in $(find . -type f -o -type l); do
file="${file:2}"
color 32 echo " Adding $file"
if [[ -L "$file" ]]; then
cp "$file" "$XDG_CONFIG_HOME/$file"
else
relink "$DOT/config/$file" "$XDG_CONFIG_HOME/$file"
fi
done
)
11 years ago
# Setup git
# =============================
11 years ago
msg "Setting up Git configuration..."
GIT="$DOT/git"
[[ -e "$HOME/.gitconfig" ]] && ! grep -q ".files/install" "$HOME/.gitconfig" && err "Custom ~/.gitconfig already exists! Please remove it before running .files/install" && exit 1
color 32 echo " Writing .gitconfig"
11 years ago
generate "$HOME/.gitconfig" <<EOF
[include]
path = $(relpath "$GIT" "$HOME")/config
path = $(relpath "$GIT" "$HOME")/user-info
11 years ago
EOF
[ "`uname -s`" = "Darwin" ] && \
cat >>"$HOME/.gitconfig" <<EOF
path = $(relpath "$GIT" "$HOME")/osx
EOF
color 32 echo " Writing git/user-info"
11 years ago
generate "$GIT/user-info" <<EOF
[user]
name = $REALNAME
email = $EMAIL
EOF
11 years ago
# Setup ZSH/Prezto
# =============================
11 years ago
msg "Setting up ZSH configuration..."
ZSH="$DOT/zsh"
color 32 echo " Writing .zshenv"
11 years ago
generate "$HOME/.zshenv" <<EOF
export DOTFILES="\$HOME/$(relpath "$DOT" "$HOME")"
11 years ago
ZDOTDIR="\$DOTFILES/zsh"
source "\$ZDOTDIR/zshenv"
EOF
for file in zshrc zpreztorc zlogin zlogout zprofile; do
color 32 echo " Adding .$file"
11 years ago
relink "$ZSH/$file" "$ZSH/.$file"
done
# Do misc setup
# =============================
test -f "$DOTFILES/install.extra" && "$DOTFILES/install.extra"
11 years ago
# Check login shell
if getent passwd $(id -un) | grep -q 'zsh$'; then
msg "Login shell is already set to zsh, you're good to go!"
else
msg "Setting login shell to '$(which zsh)'..."
chsh -s "$(which zsh)"
fi
11 years ago
# Done.
warn "You may need to relog for all changes to take effect."
warn "If you're currently running zsh, you definitely need to at least restart the shell to apply changes."