1
0
Fork 0
mirror of synced 2024-11-14 12:09:03 -05:00

Style fixes, git clone no longer done in script

* No longer close every line with `;`
* Reduce amount of variables by doing the magic inside `[ ]`
* Variables all in lowercase since all created inside subshell
* Attempt to install `zsh` via `homebrew` if zsh doesn't exist on the system
* Fix some typos
This commit is contained in:
Jorrit Visser 2017-05-29 17:29:16 +02:00
parent d11172d465
commit e118bab814
No known key found for this signature in database
GPG key ID: 53E37A7EA1CB82A7

View file

@ -2,131 +2,113 @@ main() {
# Use colors, but only if connected to a terminal, and that terminal # Use colors, but only if connected to a terminal, and that terminal
# supports them. # supports them.
if which tput >/dev/null 2>&1; then if which tput >/dev/null 2>&1; then
ncolors=$(tput colors); ncolors=$(tput colors)
fi; fi
if [ -t 1 ] && [ -n "${ncolors}" ] && [ "${ncolors}" -ge 8 ]; then if [ -t 1 ] && [ -n "${ncolors}" ] && [ "${ncolors}" -ge 8 ]; then
RED="$(tput setaf 1)"; red="$(tput setaf 1)"
GREEN="$(tput setaf 2)"; green="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"; yellow="$(tput setaf 3)"
BLUE="$(tput setaf 4)"; blue="$(tput setaf 4)"
BOLD="$(tput bold)"; bold="$(tput bold)"
NORMAL="$(tput sgr0)"; normal="$(tput sgr0)"
else else
RED=""; red=""
GREEN=""; green=""
YELLOW=""; yellow=""
BLUE=""; blue=""
BOLD=""; bold=""
NORMAL=""; normal=""
fi; fi
# Only enable exit-on-error after the non-critical colorization stuff, # Only enable exit-on-error after the non-critical colorization stuff,
# which may fail on systems lacking tput or terminfo # which may fail on systems lacking tput or terminfo
set -e set -e
ZSH_INSTALLED=$(grep /zsh$ /etc/shells | wc -l) # Check if zsh exists
if [ ! "${ZSH_INSTALLED}" -ge 1 ]; then if [ ! "$(grep "/zsh$" "/etc/shells" | wc -l)" = 1 ]; then
printf "${RED}Error: zsh is not installed!${BLUE} Please install zsh first!${NORMAL}\n"; # try installing from Homebrew
exit 1; if [ ! "$(which -s brew)" = 0 ]; then
fi; brew install zsh >/dev/null 2>&1
unset ZSH_INSTALLED printf "${yellow}Installing zsh via Homebrew.. ${normal}\n"
else
# Set Zim directory printf "${red}Error: zsh isn't available! ${blue}Please install zsh first!${normal}\n"
if [ ! -n "${ZIM_DIR}" ]; then exit 1
ZIM_DIR="${ZDOTDIR:-${HOME}}/.zim"; fi
fi; fi
# Check if Zim is already installed # Check if Zim is already installed
if [ -d "${ZIM_DIR}" ]; then if [ -e "${ZDOTDIR:-${HOME}}/.zimrc" ]; then
printf "${RED}Error: You already have Zim installed!${NORMAL}\n"; printf "${red}Error: You already have Zim installed!${normal}\n";
exit 1; exit 1
fi; fi
# Check if other frameworks are installed # Check if other frameworks are installed
if [ -d "${HOME}/.oh-my-zsh" ]; then if [ -d "${HOME}/.oh-my-zsh" ]; then
mv "${HOME}/.oh-my-zsh" "${HOME}/.oh-my-zsh_zimbackup"; mv "${HOME}/.oh-my-zsh" "${HOME}/.oh-my-zsh_zimbackup"
printf "${YELLOW}Oh-My-Zsh detected and backed up to \ printf "${yellow}Oh-My-Zsh detected and backed up to\
${HOME}/.oh-my-zsh_zimbackup${NORMAL}\n"; ${HOME}/.oh-my-zsh_zimbackup${normal}\n"
printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; printf "${blue}This is done to prevent framework conflicts.${normal}\n"
elif [ -d "${HOME}/.zplug" ]; then elif [ -d "${HOME}/.zplug" ]; then
mv "${HOME}/.zplug" "${HOME}/.zplug_zimbackup"; mv "${HOME}/.zplug" "${HOME}/.zplug_zimbackup"
printf "${YELLOW}Zplug detected and backed up to \ printf "${yellow}Zplug detected and backed up to\
${HOME}/.zplug_zimbackup${NORMAL}\n"; ${HOME}/.zplug_zimbackup${normal}\n"
printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; printf "${blue}This is done to prevent framework conflicts.${normal}\n"
elif [ -d "${HOME}/.zprezto" ]; then elif [ -d "${HOME}/.zprezto" ]; then
mv "${HOME}/.zprezto" "${HOME}/.zprezto_zimbackup"; mv "${HOME}/.zprezto" "${HOME}/.zprezto_zimbackup"
printf "${YELLOW}Prezto detected and backed up to \ printf "${yellow}Prezto detected and backed up to\
${HOME}/.zprezto_zimbackup${NORMAL}\n"; ${HOME}/.zprezto_zimbackup${normal}\n"
printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; printf "${blue}This is done to prevent framework conflicts.${normal}\n"
else else
: # Do nothing : # Do nothing
fi; fi
# Prevent the cloned repository from having insecure permissions. # Prevent the cloned repository from having insecure permissions.
umask g-w,o-w umask g-w,o-w
# Check if git is installed
hash git >/dev/null 2>&1 || {
printf "${RED}Error: git is not installed!${NORMAL}\n";
exit 1;
}
# The Windows (MSYS) Git is not compatible with normal use on cygwin # The Windows (MSYS) Git is not compatible with normal use on cygwin
if [ "${OSTYPE}" = cygwin ]; then if [ "${OSTYPE}" = "cygwin" ]; then
if git --version | grep msysgit > /dev/null; then if git --version | grep msysgit >/dev/null; then
printf "${RED}Error: Windows/MSYS Git is not supported on Cygwin${NORMAL}\n"; printf "${red}Error: Windows/MSYS Git is not supported on Cygwin${normal}\n"
printf "${RED}Error: Make sure the Cygwin git package is installed and is first on the path${NORMAL}\n"; printf "${red}Error: Make sure the Cygwin git package is installed and is first on the path${normal}\n"
exit 1; exit 1
fi; fi
fi; fi
ZIM_REPO="https://github.com/Eriner/zim.git"
# Clone the repo, including submodules. `-j8` allows parallel downloading
env git clone --recursive -j8 "${ZIM_REPO}" "${ZIM_DIR}" &> /dev/null || {
printf "${RED}Error: git clone of Zim repo failed${NORMAL}\n";
exit 1;
}
unset ZIM_REPO
unset ZIM_DIR
# Back-up old zsh dotfiles # Back-up old zsh dotfiles
if [ -f "${HOME}/.zshrc" ] || [ -L "${HOME}/.zshrc" ]; then if [ -f "${HOME}/.zshrc" ] || [ -L "${HOME}/.zshrc" ]; then
printf "${YELLOW}Found ${HOME}/.zshrc!${NORMAL} ${BLUE}Backing up to ${HOME}/.zshrc_zimbackup.${NORMAL}\n"; printf "${yellow}Found ${HOME}/.zshrc! ${blue}Backing up to ${HOME}/.zshrc_zimbackup.${normal}\n"
mv "${HOME}/.zshrc" "${HOME}/.zshrc_zimbackup"; mv "${HOME}/.zshrc" "${HOME}/.zshrc_zimbackup"
fi; fi
if [ -f "${HOME}/.zlogin" ] || [ -L "${HOME}/.zlogin" ]; then if [ -f "${HOME}/.zlogin" ] || [ -L "${HOME}/.zlogin" ]; then
printf "${YELLOW}Found ${HOME}/.zlogin!${NORMAL} ${BLUE}Backing up to ${HOME}/.zlogin_zimbackup${NORMAL}\n"; printf "${yellow}Found ${HOME}/.zlogin!$ ${blue}Backing up to ${HOME}/.zlogin_zimbackup${normal}\n"
mv "${HOME}/.zlogin" "${HOME}/.zlogin_zimbackup"; mv "${HOME}/.zlogin" "${HOME}/.zlogin_zimbackup"
fi; fi
# Place template files in ${HOME} # Place template files in ~/
cp -a "${HOME}/.zim/templates/" "${HOME}/" cp -a "${HOME}/.zim/templates/" "${HOME}/"
# If this user's login shell is not already "zsh", attempt to switch. # If this user's login shell is not already "zsh", attempt to switch
CURRENT_SHELL=$(expr "${SHELL}" : '.*/\(.*\)') if [ ! "$(expr "${SHELL}" : '.*/\(.*\)')" = "zsh" ]; then
if [ "${CURRENT_SHELL}" != "zsh" ]; then
# Auto-change shell if platform provides "chsh" command # Auto-change shell if platform provides "chsh" command
if hash chsh >/dev/null 2>&1; then if hash chsh >/dev/null 2>&1; then
printf "${BLUE}Changing your default shell to zsh!${NORMAL}\n"; printf "${blue}Changing your default shell to zsh!${normal}\n"
chsh -s $(grep /zsh$ /etc/shells | tail -1); chsh -s $(grep "/zsh$" "/etc/shells" | tail -1)
# If not, suggest user do so manually. # If not, suggest user do so manually.
else else
printf "${YELLOW}I can't change your shell automatically because this system does not have chsh.${NORMAL}\n"; printf "${yellow}I can't change your shell automatically because this system does not have chsh.${normal}\n"
printf "${YELLOW}Please manually change your default shell to zsh!${NORMAL}\n"; printf "${yellow}Please manually change your default shell to zsh!${normal}\n"
fi; fi
fi; fi
unset CURRENT_SHELL
Z="${RED}Z${GREEN}" Z="${red}Z${green}"
printf "${GREEN} ______ _ _____ __ __ _ ${NORMAL}\n" printf "${green} ______ _ _____ __ __ _ ${normal}\n"
printf "${GREEN} |$Z$Z$Z$Z$Z/ | | |$Z$Z$Z$Z$Z|$Z$Z\/$Z$Z| | |${NORMAL}\n" printf "${green} |$Z$Z$Z$Z$Z/ | | |$Z$Z$Z$Z$Z|$Z$Z\/$Z$Z| | |${normal}\n"
printf "${GREEN} /$Z/ ___| |__ |$Z| |$Z\\$Z$Z/$Z|_ __ _ __ _____ _____ __| | ${NORMAL}\n" printf "${green} /$Z/ ___| |__ |$Z| |$Z\\$Z$Z/$Z|_ __ _ __ _____ _____ __| | ${normal}\n"
printf "${GREEN} /$Z/ / __| '_ \ |$Z| |$Z|\/|$Z| '_ \| '__/ _ \ \ / / _ \/ _\` | ${NORMAL}\n" printf "${green} /$Z/ / __| '_ \ |$Z| |$Z|\/|$Z| '_ \| '__/ _ \ \ / / _ \/ _\` | ${normal}\n"
printf "${GREEN} /$Z/__\__ | | | | _|$Z|_|$Z| |$Z| |_) | | | (_) \ V | __| (_| | ${NORMAL}\n" printf "${green} /$Z/__\__ | | | | _|$Z|_|$Z| |$Z| |_) | | | (_) \ V | __| (_| | ${normal}\n"
printf "${GREEN} /$Z$Z$Z$Z$Z|___|_| |_| |$Z$Z$Z$Z$Z|$Z| |$Z| .__/|_| \___/ \_/ \___|\__,_| ${NORMAL}\n" printf "${green} /$Z$Z$Z$Z$Z|___|_| |_| |$Z$Z$Z$Z$Z|$Z| |$Z| .__/|_| \___/ \_/ \___|\__,_| ${normal}\n"
printf "${GREEN} | | ${NORMAL}\n" printf "${green} | | ${normal}\n"
printf "${GREEN} |_| ${NORMAL}\n" printf "${green} |_| ${normal}\n"
unset Z unset Z
# For archival purposes # For archival purposes
@ -141,15 +123,15 @@ main() {
# Remind user to copy back old settings # Remind user to copy back old settings
if [ -f "${HOME}/.zshrc_zimbackup" ] || [ -L "${HOME}/.zshrc_zimbackup" ]; then if [ -f "${HOME}/.zshrc_zimbackup" ] || [ -L "${HOME}/.zshrc_zimbackup" ]; then
printf "${YELLOW}Please inspect ${HOME}/.zshrc_zimbackup and copy over${NORMAL}\n"; printf "${yellow}Please inspect ${HOME}/.zshrc_zimbackup and copy over${normal}\n"
printf "${YELLOW}wanted settings to ${HOME}/.zshrc${NORMAL}\n"; printf "${yellow}wanted settings to ${HOME}/.zshrc${normal}\n"
fi; fi
if [ -f "${HOME}/.zlogin_zimbackup" ] || [ -L "${HOME}/.zlogin_zimbackup" ]; then if [ -f "${HOME}/.zlogin_zimbackup" ] || [ -L "${HOME}/.zlogin_zimbackup" ]; then
printf "${YELLOW}Please inspect ${HOME}/.zlogin_zimbackup and copy over${NORMAL}\n"; printf "${yellow}Please inspect ${HOME}/.zlogin_zimbackup and copy over${normal}\n"
printf "${YELLOW}wanted settings to ${HOME}/.zlogin${NORMAL}\n"; printf "${yellow}wanted settings to ${HOME}/.zlogin${normal}\n"
fi; fi
# Reload zsh # Load zsh
env zsh env zsh
} }