You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6 KiB

9 years ago
  1. #!/usr/bin/env bash
  2. # Tools for the scripts
  3. # Make a backup of the existing file if it exists
  4. # Then link to our supplied dotfile
  5. link() {
  6. # Only make a backup if an existing file is there and is not a link
  7. if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then
  8. cp "$1/$2" "$1/$2.old"
  9. echo "Backed up original $2 to $1/$2.old"
  10. fi
  11. ln -sf "$3" "$1/$2"
  12. }
  13. copy() {
  14. # Only make a backup if an existing directory is there and is not a link
  15. # Allow passing true as third param to do backups
  16. backup=true
  17. if [ ! -z "$4" ]; then
  18. backup=$4
  19. fi
  20. overwrite=false
  21. if [ ! -z "$5" ]; then
  22. overwrite=$5
  23. fi
  24. if [ -f "$1/$2" ] || [ -L "$1/$2" ]; then
  25. if $backup; then
  26. mv "$1/$2" "$1/$2.old"
  27. echo "Backed up original $2 to $1/$2.old"
  28. else
  29. if $overwrite; then
  30. rm -r "$1/$2"
  31. echo "removed original $2"
  32. fi
  33. fi
  34. fi
  35. # Should probably only do this is the source is newer
  36. # Remove the old and copy the new
  37. if $overwrite; then
  38. cp -r "$3" "$1/$2"
  39. fi
  40. }
  41. link_dir() {
  42. # Only make a backup if an existing directory is there and is not a link
  43. if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then
  44. mv "$1/$2" "$1/$2.old"
  45. echo "Backed up original $2 to $1/$2.old"
  46. fi
  47. # Don't overwrite links
  48. if [ ! -L "$1/$2" ]; then
  49. echo "ln -sf $3 $1/$2"
  50. ln -sf "$3" "$1/$2"
  51. fi
  52. }
  53. copy_dir() {
  54. # Only make a backup if an existing directory is there and is not a link
  55. if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then
  56. # Allow passing true as third param to do backups
  57. backup=true
  58. if [ ! -z "$3" ]; then
  59. backup=$4
  60. fi
  61. if $backup; then
  62. mv "$1/$2" "$1/$2.old"
  63. echo "Backed up original $2 to $1/$2.old"
  64. else
  65. rm -r "$1/$2"
  66. echo "removed original $2"
  67. fi
  68. fi
  69. # Should probably only do this is the source is newer
  70. # Remove the old and copy the new
  71. cp -r "$3" "$1/$2"
  72. }
  73. # Escape the path so we can use it in sed
  74. escape_path() {
  75. local safe="$(echo $1 | sed 's/\//\\\//g')"
  76. echo $safe
  77. }
  78. # Clone a git repository into a location
  79. # Or update an existing repository
  80. clone_or_update_git_repo() {
  81. local target=$1
  82. local repo=$2
  83. if [ ! -d "$target" ]; then
  84. mkdir -p "$target"
  85. echo "Cloning [$repo] into [$target]"
  86. git clone "$repo" "$target"
  87. else
  88. # Update the repo otherwise
  89. echo "Updating [$repo]"
  90. cur_dir=$PWD
  91. cd "$target"
  92. git checkout .
  93. git pull
  94. cd $cur_dir
  95. fi
  96. }