install.sh 3.95 KB
Newer Older
Brian J Brennan's avatar
Brian J Brennan committed
1
function main() {
2
3
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
4
5
6
7
tput=$(which tput)
if [ -n "$tput" ]; then
    ncolors=$($tput colors)
fi
8
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
9
  RED="$(tput setaf 1)"
10
11
12
13
14
15
16
17
18
19
20
21
22
  GREEN="$(tput setaf 2)"
  YELLOW="$(tput setaf 3)"
  BLUE="$(tput setaf 4)"
  BOLD="$(tput bold)"
  NORMAL="$(tput sgr0)"
else
  RED=""
  GREEN=""
  YELLOW=""
  BLUE=""
  BOLD=""
  NORMAL=""
fi
23
24
25
26
27

# Only enable exit-on-error after the non-critical colorization stuff,
# which may fail on systems lacking tput or terminfo
set -e

28
29
CHECK_ZSH_INSTALLED=$(grep /zsh$ /etc/shells | wc -l)
if [ ! $CHECK_ZSH_INSTALLED -ge 1 ]; then
30
  printf "${YELLOW}Zsh is not installed!${NORMAL} Please install zsh first!\n"
31
32
33
  exit
fi
unset CHECK_ZSH_INSTALLED
34

35
if [ ! -n "$ZSH" ]; then
36
37
38
  ZSH=~/.oh-my-zsh
fi

39
if [ -d "$ZSH" ]; then
40
41
  printf "${YELLOW}You already have Oh My Zsh installed.${NORMAL}\n"
  printf "You'll need to remove $ZSH if you want to re-install.\n"
Robby Russell's avatar
Robby Russell committed
42
43
44
  exit
fi

45
46
47
48
49
50
51
# Prevent the cloned repository from having insecure permissions. Failing to do
# so causes compinit() calls to fail with "command not found: compdef" errors
# for users with insecure umasks (e.g., "002", allowing group writability). Note
# that this will be ignored under Cygwin by default, as Windows ACLs take
# precedence over umasks except for filesystems mounted with option "noacl".
umask g-w,o-w

52
printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n"
53
54
55
56
hash git >/dev/null 2>&1 || {
  echo "Error: git is not installed"
  exit 1
}
57
58
59
env git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git $ZSH || {
  printf "Error: git clone of oh-my-zsh repo failed\n"
  exit 1
Renan Cakirerk's avatar
Renan Cakirerk committed
60
}
Toon Claes's avatar
Toon Claes committed
61

62
63
64
65
66
67
68
69
# The Windows (MSYS) Git is not compatible with normal use on cygwin
if [ "$OSTYPE" = cygwin ]; then
  if git --version | grep msysgit > /dev/null; then
    echo "Error: Windows/MSYS Git is not supported on Cygwin"
    echo "Error: Make sure the Cygwin git package is installed and is first on the path"
    exit 1
  fi
fi
Toon Claes's avatar
Toon Claes committed
70

71
printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n"
72
if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
73
  printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n";
74
  mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh;
Robby Russell's avatar
Robby Russell committed
75
76
fi

77
printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n"
78
cp $ZSH/templates/zshrc.zsh-template ~/.zshrc
79
sed "/^export ZSH=/ c\\
80
export ZSH=$ZSH
81
82
" ~/.zshrc > ~/.zshrc-omztemp
mv -f ~/.zshrc-omztemp ~/.zshrc
Robby Russell's avatar
Robby Russell committed
83

84
printf "${BLUE}Copying your current PATH and adding it to the end of ~/.zshrc for you.${NORMAL}\n"
85
sed "/export PATH=/ c\\
86
export PATH=\"$PATH\"
87
88
" ~/.zshrc > ~/.zshrc-omztemp
mv -f ~/.zshrc-omztemp ~/.zshrc
89

90
# If this user's login shell is not already "zsh", attempt to switch.
91
92
TEST_CURRENT_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
93
94
  # If this platform provides a "chsh" command (not Cygwin), do it, man!
  if hash chsh >/dev/null 2>&1; then
95
    printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n"
96
    chsh -s $(grep /zsh$ /etc/shells | tail -1)
97
98
  # Else, suggest the user do so manually.
  else
99
    printf "I can't change your shell automatically because this system does not have chsh.\n"
100
    printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n"
101
  fi
102
fi
Robby Russell's avatar
Robby Russell committed
103

104
105
106
107
108
109
110
111
112
113
114
printf "${GREEN}"
echo '         __                                     __   '
echo '  ____  / /_     ____ ___  __  __   ____  _____/ /_  '
echo ' / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \ '
echo '/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / / '
echo '\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/  '
echo '                        /____/                       ....is now installed!'
echo ''
echo ''
echo 'Please look over the ~/.zshrc file to select plugins, themes, and options.'
echo ''
115
echo 'p.s. Follow us at https://twitter.com/ohmyzsh.'
116
117
118
echo ''
echo 'p.p.s. Get stickers and t-shirts at http://shop.planetargon.com.'
echo ''
119
printf "${NORMAL}"
120
env zsh
Brian J Brennan's avatar
Brian J Brennan committed
121
122
123
}

main