#!/bin/zsh # .files/installer # (c) 2014 MORI Taeyeon # Paths & Utils DOT="$(realpath "$(dirname "$0")")" cd "$DOT" setopt EXTENDED_GLOB source "$DOT/lib/libzsh-utils.zsh" # Parse commandline OVERWRITE=false ASK=true function print_usage() { echo "Usage: $1 [--help] [--overwrite] [--dont-ask]" 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" } for arg in "$@"; do [[ -z "$STATE" ]] && { case "$arg" in -h|--help) print_usage "$0"; exit 0;; --overwrite) OVERWRITE=true;; --dont-ask) ASK=false;; *) print_usage "$0"; exit 1;; esac } || { case "$STATE" in # handle options with parameters esac STATE= } done [[ -n "$STATE" ]] && err "Option $STATE is missing it's parameter!" && exit 1 # Helper functions 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 echo "${2-#} Generated by .files/install" >"$1" echo "${2-#} `date "+%m/%d/%Y %H:%M:%S"`" >>"$1" 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" } # 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:" [[ -e "$DOT/etc/user-info" ]] && source "$DOT/etc/user-info" [[ -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 generate "$DOT/etc/user-info" <>"$HOME/.gitconfig" <