link-from-dotfiles.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. ROOT="$HOME"
  4. DOT_DIR="${ROOT}/.config/dots" # absolute path to your git folder
  5. SYSTEMD_DIR="${ROOT}/.config/systemd" # systemd needs to be specified
  6. # separately, if just normal
  7. # symlink files, they are
  8. # symlinked again if enabled and
  9. # will dissapear from their
  10. # directory
  11. CMD="ln -rsv" # works for everything except systemd services
  12. SCRIPT_NAME="${0##*/}"
  13. display_help(){
  14. echo "Usage: ${SCRIPT_NAME} <flag>"
  15. echo "Check if ❛${DOT_DIR}❜ is yours dotfiles directory path"
  16. echo "No flags == DRY RUN"
  17. echo "-d, --dry-run == link files"
  18. echo "-l, --link == link files"
  19. echo "-f, --force == link files with overwrite"
  20. exit 2
  21. }
  22. create_dirs(){
  23. file="$1"
  24. new="${file//${DOT_DIR}/${ROOT}}"
  25. newDir="${new%/*}"
  26. mkdir -p "$newDir"
  27. }
  28. dry_run(){
  29. file="$1"
  30. link="${file//${DOT_DIR}/${ROOT}}"
  31. [[ ! -f $link ]] && echo "DRY RUN: $CMD ${file} --> ${link}"
  32. }
  33. symlink_systemd_dir(){
  34. rm -rf "$SYSTEMD_DIR"
  35. $CMD -- "${DOT_DIR}/.config/systemd" "$SYSTEMD_DIR"
  36. }
  37. symlink_file(){
  38. file="$1"
  39. create_dirs "$file"
  40. $CMD -- "$file" "$new"
  41. }
  42. symlink_force(){
  43. file="$1"
  44. create_dirs "$file"
  45. $CMD -f -- "$file" "$new"
  46. }
  47. [[ ! -d $DOT_DIR || $# -ne 1 ]] && display_help
  48. while IFS= read -r file; do
  49. case "$1" in
  50. -d|--dry-run) dry_run "$file";;
  51. -l|--link) symlink_file "$file";;
  52. -f|--force) symlink_force "$file";;
  53. *) display_help;;
  54. esac
  55. done < <(find "$DOT_DIR" -not -type d -not -path "*\.git*" -not -path "*/systemd/user/*")
  56. symlink_systemd_dir
  57. # stow --dir "$DOT_DIR" --target "$ROOT"