start 1.7 KB

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