start 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 checkout master
  29. git submodule update
  30. if [ ! -z "$1" ]; then
  31. real_path=$(realpath $HOME/$1)
  32. to_copy=${real_path#*$HOME}
  33. cp -R $real_path $dots_dir/$to_copy
  34. rm -rf $real_path
  35. echo "${to_copy/\//}" >> ../to_sync
  36. fi
  37. while read p; do
  38. file="$dots_dir/$p"
  39. git add $file
  40. done < $script_dir/to_sync
  41. git add start to_sync
  42. if git commit -am "$(date)"; then
  43. git remote add origin $repo || true
  44. git push -u origin master
  45. fi
  46. while read p; do
  47. destination="$HOME/$p"
  48. rm -rf $destination
  49. ln -s "$dots_dir/$p" $destination
  50. done < $script_dir/to_sync
  51. }
  52. subcommand=$1
  53. case $subcommand in
  54. "" | "-h" | "--help")
  55. sub_help
  56. ;;
  57. *)
  58. shift
  59. sub_${subcommand} $@
  60. if [ $? = 127 ]; then
  61. echo "Error: '$subcommand' is not a known subcommand." >&2
  62. echo " Run '$ProgName --help' for a list of known subcommands." >&2
  63. exit 1
  64. fi
  65. ;;
  66. esac