start 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. dot_dir=$HOME/.dofiles
  3. repo="https://git.capella.pro/capella/dotfiles.git"
  4. set -ex
  5. trap popd EXIT
  6. mkdir -pv $dot_dir
  7. pushd $dot_dir
  8. sub_help(){
  9. echo "Usage: $ProgName <subcommand> [options]\n"
  10. echo "Subcommands:"
  11. echo " sync <dot_file_to_add> add a dotfyle to sync"
  12. echo ""
  13. echo "For help with each subcommand run:"
  14. echo "$ProgName <subcommand> -h|--help"
  15. echo ""
  16. }
  17. sub_sync(){
  18. if [[ ! -d $dot_dir/dotfiles/.git ]]; then
  19. git clone $repo
  20. fi
  21. mkdir -p dotfiles/dots
  22. cd dotfiles/dots
  23. git pull
  24. if [ ! -z "$1" ]; then
  25. real_path=$(realpath $1)
  26. to_copy=${real_path#*$HOME}
  27. cp -R $real_path $(pwd)$to_copy
  28. rm -rf $real_path
  29. echo "${to_copy/\//}" >> ../to_sync
  30. fi
  31. cd ..
  32. git add .
  33. if git commit -am "$(date)"; then
  34. git remote add origin $repo || true
  35. git push -u origin master
  36. fi
  37. while read p; do
  38. destination="$HOME/$p"
  39. rm -rf $destination
  40. ln -s "$dot_dir/dotfiles/dots/$p" $(dirname $destination)
  41. done <to_sync
  42. }
  43. subcommand=$1
  44. case $subcommand in
  45. "" | "-h" | "--help")
  46. sub_help
  47. ;;
  48. *)
  49. shift
  50. sub_${subcommand} $@
  51. if [ $? = 127 ]; then
  52. echo "Error: '$subcommand' is not a known subcommand." >&2
  53. echo " Run '$ProgName --help' for a list of known subcommands." >&2
  54. exit 1
  55. fi
  56. ;;
  57. esac