diff --git a/.gitattributes b/.gitattributes index 21c4a4a..f43d699 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ -home/.ssh/config filter=git-crypt diff=git-crypt +ssh/config filter=git-crypt diff=git-crypt diff --git a/.gitignore b/.gitignore index 33908e2..a41968c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ # .gitignore. .git/ -config.local +config.sh config/nvim/plugin/packer_compiled.lua config/zsh/.zcompcache config/zsh/.zcompdump diff --git a/README.md b/README.md index 57f7f44..aa193b1 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ ./unlock.sh # decrypt the SSH configuration ``` If you are not me, then you won't have the password to use in the last step. -Just `git rm unlock.sh home/.ssh/config` and add your own. +Just `git rm unlock.sh ssh/config` and add your own. ## Prerequisites diff --git a/home/.local/bin/colorchart b/bin/colorchart similarity index 100% rename from home/.local/bin/colorchart rename to bin/colorchart diff --git a/home/.local/bin/colors b/bin/colors similarity index 100% rename from home/.local/bin/colors rename to bin/colors diff --git a/home/.local/bin/colors256 b/bin/colors256 similarity index 100% rename from home/.local/bin/colors256 rename to bin/colors256 diff --git a/home/.local/bin/colortest b/bin/colortest similarity index 100% rename from home/.local/bin/colortest rename to bin/colortest diff --git a/home/.local/bin/environ b/bin/environ similarity index 100% rename from home/.local/bin/environ rename to bin/environ diff --git a/home/.local/bin/git-churn b/bin/git-churn similarity index 100% rename from home/.local/bin/git-churn rename to bin/git-churn diff --git a/home/.local/bin/heads b/bin/heads similarity index 100% rename from home/.local/bin/heads rename to bin/heads diff --git a/home/.local/bin/mpath b/bin/mpath similarity index 100% rename from home/.local/bin/mpath rename to bin/mpath diff --git a/home/.local/bin/myip b/bin/myip similarity index 100% rename from home/.local/bin/myip rename to bin/myip diff --git a/home/.local/bin/myipinfo b/bin/myipinfo similarity index 100% rename from home/.local/bin/myipinfo rename to bin/myipinfo diff --git a/home/.local/bin/noautomount b/bin/noautomount similarity index 100% rename from home/.local/bin/noautomount rename to bin/noautomount diff --git a/home/.local/bin/path b/bin/path similarity index 100% rename from home/.local/bin/path rename to bin/path diff --git a/home/.local/bin/styletest b/bin/styletest similarity index 100% rename from home/.local/bin/styletest rename to bin/styletest diff --git a/home/.local/bin/tails b/bin/tails similarity index 100% rename from home/.local/bin/tails rename to bin/tails diff --git a/home/.local/bin/themegen b/bin/themegen similarity index 100% rename from home/.local/bin/themegen rename to bin/themegen diff --git a/home/.local/bin/vboxmount b/bin/vboxmount similarity index 100% rename from home/.local/bin/vboxmount rename to bin/vboxmount diff --git a/config.def.sh b/config.def.sh new file mode 100644 index 0000000..455e34f --- /dev/null +++ b/config.def.sh @@ -0,0 +1,7 @@ +# Where to install the dotfiles, usually $HOME. +DESTDIR="$HOME" + +# Git user information. +GIT_USER="Fernando Schauenburg" +GIT_EMAIL="dev@schauenburg.me" + diff --git a/config/zsh/env.zsh b/config/zsh/env.zsh index fcafffc..4de4dba 100644 --- a/config/zsh/env.zsh +++ b/config/zsh/env.zsh @@ -51,6 +51,7 @@ done </dev/null 2>&1; then - error "Dependency \`$cmd\` not found." - exit 1 - fi - done +heading(){ + echo "${blue}===== $1 ==========$sgr0"; +} + +load_config() { + defconfig="$dotfiles/config.def.sh" + config="$dotfiles/config.sh" + { [ -r "$config" ] || cp -v "$defconfig" "$config"; } || error "can't create config.sh" + . "$config" +} + +is_dry_run() { [ "$DRY_RUN" = "yes" ]; } + +move_aside() { + backup="$1.$(date +%s)" + echo "${red}WARNING:$sgr0 moving '$1' to '$backup'" + is_dry_run || mv "$1" "$backup" } greeting() { - dry_run && { - warn "Performing dry run (use -f to actually make changes)." - warn + is_dry_run && { + echo "${yellow}Performing dry run (use -f to actually make changes).$sgr0" + echo } - info "Deploying dotfiles:" - info " Source: $cyan$DOTFILES$rst" - info " Target: $cyan$TARGET$rst" - info " Git user: $green$GIT_USER <$GIT_EMAIL>$rst" + echo "Deploying dotfiles:" + echo " Source: $cyan$dotfiles$sgr0" + echo " Destination: $cyan$DESTDIR$sgr0" + echo " Git user: $green$GIT_USER <$GIT_EMAIL>$sgr0" if [ -t 0 ] && [ -t 1 ]; then - info - info "Press ENTER to continue (CTRL-C to cancel)..." + echo + echo "Press ENTER to continue (CTRL-C to cancel)..." read -r fi } -make_dirs() { - heading 'create auxiliary directories' - while read -r item; do - dir="$TARGET/$item" - if [ ! -d "$dir" ]; then - echo "${yellow}MKDIR:$rst $dir" - dry_run || mkdir -p "$dir" - fi - done <$rst $dotfiles_config" - dry_run || ln -s "$dotfiles_config" "$link" +make_dir() { + if [ -d "$1" ]; then + echo "${green}OK:$sgr0 $1" + else + echo "${yellow}MKDIR:$sgr0 $1" + is_dry_run || mkdir -vp "$1" fi } -stow_home() { - heading 'stow home directory' - stow -v${IS_DRY_RUN:+n} --no-folding -d "$DOTFILES" -t "$TARGET" home 2>&1 \ - | sed -E \ - -e "s/^(WARNING:)/$red\1$rst/" \ - -e "s/^([^:]+:)/$yellow\1$rst/" \ - -e "s/=>/$blue=>$rst/" +make_link() { + link="$1" + target="$2" + if [ -e "$link" ] && [ "$(realpath "$link")" = "$target" ]; then + echo "${green}OK:$sgr0 $link $blue->$sgr0 $target" + else + [ -e "$link" ] && move_aside "$link" + echo "${yellow}LINK:$sgr0 $link $blue->$sgr0 $target" + is_dry_run || ln -sf "$target" "$link" + fi } -git_user_config() { - heading 'git user configuration' - config_file="$TARGET/.local/etc/git/config.user" +make_git_user_config() { + user_config="$DESTDIR/.local/etc/git/config.user" temp_git="$(mktemp)" cat >"$temp_git" </dev/null 2>&1; then - if [ -f "$config_file" ]; then action=OVERWRITE; else action=CREATE; fi - echo "$yellow$action:$rst $config_file with contents of $temp_git:" - echo "$cyan" - cat "$temp_git" - echo "$rst" - dry_run || cp -f "$temp_git" "$config_file" + if diff "$user_config" "$temp_git" >/dev/null 2>&1; then + echo "${green}OK:$sgr0 $user_config has '$GIT_USER <$GIT_EMAIL>'" + else + [ -f "$user_config" ] && move_aside "$user_config" + echo "${yellow}WRITE:$sgr0 $user_config with '$GIT_USER <$GIT_EMAIL>'" + is_dry_run || cp -f "$temp_git" "$user_config" fi } -############################################################################### -# Helper Functions -############################################################################### - -if [ -t 1 ]; then - rst="$(printf '\033[0m')" - red="$(printf '\033[31m')" - green="$(printf '\033[32m')" - yellow="$(printf '\033[33m')" - blue="$(printf '\033[34m')" - # magenta="$(printf '\033[35m')" - cyan="$(printf '\033[36m')" -else - rst='' - red='' - green='' - yellow='' - blue='' - # magenta='' - cyan='' -fi - -dry_run() { [ -n "$IS_DRY_RUN" ]; } - usage() { - echo "Usage: $(basename "$0") [-h] [-f] [-t ]" + echo "Usage: $(basename "$0") [-h] [-f]" echo "" echo " -h print this help and exit" echo " -f modify filesystem rather than dry run" - echo " -t set directory (default: \$HOME)" } -heading() { echo "${blue}===== $1 ==========${rst}"; } -info() { echo "$*"; } -warn() { echo "${yellow}$*${rst}"; } -error() { echo "${red}$*${rst}"; } +main() { + DRY_RUN=yes + while getopts 'fh' opt; do case "$opt" in + f) DRY_RUN=no;; + h) usage; exit 0;; + *) usage; exit 1;; + esac done + + load_config || error "could not load config.sh" + + [ -n "$DESTDIR" ] || error "\$DESTDIR must be set in config.sh" + [ -n "$GIT_USER" ] || error "\$GIT_USER must be set in config.sh" + [ -n "$GIT_EMAIL" ] || error "\$GIT_EMAIL must be set in config.sh" + + greeting + + heading 'create required directories' + make_dir "$DESTDIR/.ssh/" + make_dir "$DESTDIR/.local/etc/git/" + make_dir "$DESTDIR/.local/share/less/" + make_dir "$DESTDIR/.local/share/python/" + make_dir "$DESTDIR/.local/share/nvim/shada/" + make_dir "$DESTDIR/.local/share/zsh/" + + heading 'create links' + make_link "$DESTDIR/.hushlogin" "$dotfiles/home/.hushlogin" + make_link "$DESTDIR/.XCompose" "$dotfiles/home/.XCompose" + make_link "$DESTDIR/.zshenv" "$dotfiles/home/.zshenv" + make_link "$DESTDIR/.bin" "$dotfiles/bin" + make_link "$DESTDIR/.config" "$dotfiles/config" + make_link "$DESTDIR/.ssh/config" "$dotfiles/ssh/config" + + heading 'git user configuration' + make_git_user_config +} main "$@" + diff --git a/home/.ssh/config b/ssh/config similarity index 100% rename from home/.ssh/config rename to ssh/config