/script/install.sh

https://github.com/jphenow/dotfiles · Shell · 144 lines · 112 code · 18 blank · 14 comment · 24 complexity · 9d88d2f81b6815a08e50b8c64275fe0a MD5 · raw file

  1. #!/bin/sh
  2. #
  3. # This script should be run via curl:
  4. # sh -c "$(curl -fsSL https://raw.githubusercontent.com/jphenow/dotfiles/master/script/install.sh)"
  5. # or wget:
  6. # sh -c "$(wget -qO- https://raw.githubusercontent.com/jphenow/dotfiles/master/script/install.sh)"
  7. #
  8. # As an alternative, you can first download the install script and run it afterwards:
  9. # wget https://raw.githubusercontent.com/jphenow/dotfiles/master/script/install.sh
  10. # sh install.sh
  11. set -e
  12. # Default settings
  13. ZSH=${ZSH:-~/.oh-my-zsh}
  14. command_exists() {
  15. command -v "$@" >/dev/null 2>&1
  16. }
  17. error() {
  18. echo ${RED}"Error: $@"${RESET} >&2
  19. }
  20. setup_color() {
  21. # Only use colors if connected to a terminal
  22. if [ -t 1 ]; then
  23. RED=$(printf '\033[31m')
  24. GREEN=$(printf '\033[32m')
  25. YELLOW=$(printf '\033[33m')
  26. BLUE=$(printf '\033[34m')
  27. BOLD=$(printf '\033[1m')
  28. RESET=$(printf '\033[m')
  29. else
  30. RED=""
  31. GREEN=""
  32. YELLOW=""
  33. BLUE=""
  34. BOLD=""
  35. RESET=""
  36. fi
  37. }
  38. install_dependencies() {
  39. if [[ "$OSTYPE" = darwin* ]] && git --version | grep -q msysgit; then
  40. command_exists brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  41. command_exists zsh || brew install zsh
  42. command_exists git || brew install git
  43. command_exists reattach-to-user-namespace || brew install reattach-to-user-namespace
  44. command_exists ag || brew install the_silver_searcher
  45. command_exists fpp || brew install fpp # https://facebook.github.io/PathPicker/
  46. command_exists urlview || brew install urlview # https://packages.debian.org/stable/misc/urlview
  47. else
  48. command_exists curl || sudo apt install -y curl
  49. command_exists zsh || sudo apt install -y zsh
  50. command_exists git || sudo apt install -y git
  51. fi
  52. if [ "$(echo $SHELL)" != "$(which zsh)" ]; then
  53. echo "Switch shell from $SHELL to $(which zsh)..."
  54. chsh -s $(which zsh)
  55. fi
  56. if [ -d "$HOME/.dotfiles" ]; then
  57. echo "${BOLD}~/.dotfiles${RESET} already exists, attempting to pull updates..."
  58. cd $HOME/.dotfiles
  59. git pull
  60. else
  61. git clone https://github.com/jphenow/dotfiles.git $HOME/.dotfiles
  62. cd $HOME/.dotfiles
  63. fi
  64. rm -rf "$HOME/.oh-my-zsh"
  65. set +e
  66. export RUNZSH=no
  67. sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  68. if [[ $? -ne 0 ]]; then
  69. exit 1
  70. fi
  71. set -e
  72. }
  73. install_modules() {
  74. cd ~/.dotfiles
  75. for INSTALL_SCRIPT in $(ls */install.sh)
  76. do
  77. if [ "$INSTALL_SCRIPT" != "script/install.sh" ]; then
  78. bash $INSTALL_SCRIPT
  79. fi
  80. done
  81. }
  82. setup_dotfiles() {
  83. cd ~/.dotfiles
  84. dir=$(pwd)
  85. for DOTFILE in $(ls -lrt -d -1 */*.symlink)
  86. do
  87. target_name="$HOME/$(echo $DOTFILE | sed 's/\.symlink//' | sed 's/\//\/./' | sed 's/.*\///')"
  88. source_name="$dir/$DOTFILE"
  89. echo "Linking $source_name to $target_name..."
  90. set +e
  91. ln -s $source_name $target_name
  92. if [ $? -eq 1 ]; then
  93. echo "Backing up existing $target_name to $target_name.bak..."
  94. mv $target_name $target_name.bak
  95. set -e
  96. ln -s $source_name $target_name
  97. fi
  98. set -e
  99. done
  100. }
  101. main() {
  102. # Run as unattended if stdin is closed
  103. if [ ! -t 0 ]; then
  104. RUNZSH=no
  105. CHSH=no
  106. fi
  107. # Parse arguments
  108. while [ $# -gt 0 ]; do
  109. case $1 in
  110. --unattended) RUNZSH=no; CHSH=no ;;
  111. --skip-chsh) CHSH=no ;;
  112. --keep-zshrc) KEEP_ZSHRC=yes ;;
  113. esac
  114. shift
  115. done
  116. setup_color
  117. install_dependencies
  118. install_modules
  119. setup_dotfiles
  120. printf "$GREEN"
  121. echo "TODO"
  122. echo " * cp ~/.dotfiles/git/gitconfig.symlink.example ~/.gitconfig"
  123. echo " * vim ~/.gitconfig"
  124. echo "Please open a new terminal!"
  125. }
  126. main "$@"