Created
April 9, 2019 16:55
-
-
Save borekb/abf0d0e0e2d6e52e50667fd1b6dfd1ee to your computer and use it in GitHub Desktop.
Install dotfiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Installs symlinks to dotfiles into the $HOME directory. | |
# | |
# Flags: | |
# | |
# -n Dry run | |
# -f Force – overwrite files in $HOME if they exist | |
# | |
# Inspired by https://github.com/mplacona/dotfiles/blob/65329dc/bootstrap.sh but quite different in the end. | |
# ---------------- | |
# Parse flags | |
flag_force='' | |
flag_dryrun='' | |
while getopts 'nf' flag; do | |
case "${flag}" in | |
n) flag_dryrun=true ;; | |
f) flag_force=true ;; | |
*) echo 'Invalid flag'; exit 1 ;; | |
esac | |
done | |
# ---------------- | |
# Let's go! | |
cd "$(dirname "$0")" || exit 1 | |
some_files_skipped=false | |
for file in .*; do | |
# Note that .gitignore is _not_ symlinked to $HOME; it's only for the purpose of this repo | |
for ignoredFile in {.,..,.git,.DS_Store,.gitignore}; do | |
[[ $file == "$ignoredFile" ]] && continue 2 | |
done | |
target="$HOME/$file" | |
if [[ -e "$target" ]]; then | |
if [[ $flag_force = true ]]; then | |
echo "Overwriting $target" | |
rm "$target" | |
# Symlink will be created later | |
else | |
echo "File $target already exists, skipping" | |
some_files_skipped=true | |
continue | |
fi | |
fi | |
file_absolute=$(pwd)/$file | |
if [[ $flag_dryrun = true ]]; then | |
echo "Would symlink $file_absolute as $target" | |
else | |
ln -s "$file_absolute" "$target" | |
echo "Symlinked $file_absolute as $target" | |
fi | |
done | |
if [[ $some_files_skipped = true ]]; then | |
PURPLE='\033[1;35m' | |
CLEAR='\033[0m' | |
echo -e "\n${PURPLE}Run with \`-f\` to re-create the files above as symlinks.${CLEAR}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment