link-to-dotfiles.sh 959 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. dotfiles_dir="${DOTFILES:-${HOME}/.config/dots}"
  4. for file in "$@"; do
  5. [[ -L "$file" ]] && echo "${file} is a symlink already" && continue
  6. file="$(realpath "$file")"
  7. file_wo_home="${file##${HOME}/}"
  8. file_dirname="$(dirname "$file")"
  9. file_dirname_wo_home="${file_dirname##${HOME}}"
  10. # # Remove leading slash from file_dirname_wo_home if present
  11. # file_dirname_wo_home="${file_dirname_wo_home#/}"
  12. # for files and directories
  13. mkdir -pv "${dotfiles_dir}/${file_dirname_wo_home}"
  14. mv -v "$file" "${dotfiles_dir}/${file_dirname_wo_home}"
  15. ln -sv "${dotfiles_dir}/${file_wo_home}" "$file_dirname"
  16. # dry run
  17. # echo "You want to create directory: ${dotfiles_dir}/${file_dirname_wo_home}"
  18. # echo "You want to move: ${file} >> ${dotfiles_dir}/${file_dirname_wo_home}"
  19. # echo "You want to link: ${dotfiles_dir}/${file_wo_home} >> ${file_dirname}"
  20. done