From f818b1c7879fd22fbc335cdf45a429b58e32cd60 Mon Sep 17 00:00:00 2001 From: Jorrit Visser Date: Sat, 20 May 2017 17:19:50 +0200 Subject: [PATCH] bringup of install script Basically what it says. This is an install script that can get called via shell (and we can curl it!). A few niceties: * Error handling for various problems * Checks if other frameworks are installed, and backs them up * Changes shell to zsh, without sudo Its the oh-my-zsh installer, but very heavily modified. I wrote my own installer at first, but decided that reinventing the wheel was stupid. There is one modification: Zim's template files are no longer prepended to .zshrc and .zlogin, but instead, .zshrc and .zlogin are backed up. During the writing and testing of the script I've had the prepending command mess up and paste the template lines in the middle of other lines in my .zshrc, multiple times. You simply don't want to risk that. --- tools/zim_install | 156 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100755 tools/zim_install diff --git a/tools/zim_install b/tools/zim_install new file mode 100755 index 0000000..9c0ba04 --- /dev/null +++ b/tools/zim_install @@ -0,0 +1,156 @@ +main() { + # Use colors, but only if connected to a terminal, and that terminal + # supports them. + if which tput >/dev/null 2>&1; then + ncolors=$(tput colors); + fi; + if [ -t 1 ] && [ -n "${ncolors}" ] && [ "${ncolors}" -ge 8 ]; then + RED="$(tput setaf 1)"; + 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; + + # Only enable exit-on-error after the non-critical colorization stuff, + # which may fail on systems lacking tput or terminfo + set -e + + ZSH_INSTALLED=$(grep /zsh$ /etc/shells | wc -l) + if [ ! "${ZSH_INSTALLED}" -ge 1 ]; then + printf "${RED}Error: zsh is not installed!${BLUE} Please install zsh first!${NORMAL}\n"; + exit 1; + fi; + unset ZSH_INSTALLED + + # Set Zim directory + if [ ! -n "${ZIM_DIR}" ]; then + ZIM_DIR="${ZDOTDIR:-${HOME}}/.zim"; + fi; + + # Check if Zim is already installed + if [ -d "${ZIM_DIR}" ]; then + printf "${RED}Error: You already have Zim installed!${NORMAL}\n"; + exit 1; + fi; + + # Check if other frameworks are installed + if [ -d "${HOME}/.oh-my-zsh" ]; then + mv "${HOME}/.oh-my-zsh" "${HOME}/.oh-my-zsh_zimbackup"; + printf "${YELLOW}Oh-My-Zsh detected and backed up to \ + ${HOME}/.oh-my-zsh_zimbackup${NORMAL}\n"; + printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; + elif [ -d "${HOME}/.zplug" ]; then + mv "${HOME}/.zplug" "${HOME}/.zplug_zimbackup"; + printf "${YELLOW}Zplug detected and backed up to \ + ${HOME}/.zplug_zimbackup${NORMAL}\n"; + printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; + elif [ -d "${HOME}/.zprezto" ]; then + mv "${HOME}/.zprezto" "${HOME}/.zprezto_zimbackup"; + printf "${YELLOW}Prezto detected and backed up to \ + ${HOME}/.zprezto_zimbackup${NORMAL}\n"; + printf "${BLUE}This is done to prevent framework conflicts.${NORMAL}\n"; + else + : # Do nothing + fi; + + # Prevent the cloned repository from having insecure permissions. + 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 + if [ "${OSTYPE}" = cygwin ]; 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: Make sure the Cygwin git package is installed and is first on the path${NORMAL}\n"; + exit 1; + 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 + if [ -f "${HOME}/.zshrc" ] || [ -L "${HOME}/.zshrc" ]; then + printf "${YELLOW}Found ${HOME}/.zshrc!${NORMAL} ${BLUE}Backing up to ${HOME}/.zshrc_zimbackup.${NORMAL}\n"; + mv "${HOME}/.zshrc" "${HOME}/.zshrc_zimbackup"; + fi; + if [ -f "${HOME}/.zlogin" ] || [ -L "${HOME}/.zlogin" ]; then + printf "${YELLOW}Found ${HOME}/.zlogin!${NORMAL} ${BLUE}Backing up to ${HOME}/.zlogin_zimbackup${NORMAL}\n"; + mv "${HOME}/.zlogin" "${HOME}/.zlogin_zimbackup"; + fi; + + # Place template files in ${HOME} + cp -a "${HOME}/.zim/templates/" "${HOME}/" + + # If this user's login shell is not already "zsh", attempt to switch. + CURRENT_SHELL=$(expr "${SHELL}" : '.*/\(.*\)') + if [ "${CURRENT_SHELL}" != "zsh" ]; then + # Auto-change shell if platform provides "chsh" command + if hash chsh >/dev/null 2>&1; then + printf "${BLUE}Changing your default shell to zsh!${NORMAL}\n"; + chsh -s $(grep /zsh$ /etc/shells | tail -1); + # If not, suggest user do so manually. + else + 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"; + fi; + fi; + unset CURRENT_SHELL + + Z="${RED}Z${GREEN}" + 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|_ __ _ __ _____ _____ __| | ${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$Z|___|_| |_| |$Z$Z$Z$Z$Z|$Z| |$Z| .__/|_| \___/ \_/ \___|\__,_| ${NORMAL}\n" + printf "${GREEN} | | ${NORMAL}\n" + printf "${GREEN} |_| ${NORMAL}\n" + unset Z + + # For archival purposes + # ______ _ _____ __ __ _ + # |___ / | | |_ _|| \/ | | | + # / / ___ | |__ | | | \ / | _ __ _ __ ___ __ __ ___ __| | + # / / / __|| '_ \ | | | |\/| || '_ \ | '__|/ _ \\ \ / // _ \ / _` | + # / /__ \__ \| | | | _| |_ | | | || |_) || | | (_) |\ V /| __/| (_| | + # /_____||___/|_| |_| |_____||_| |_|| .__/ |_| \___/ \_/ \___| \__,_| + # | | + # |_| + + # Remind user to copy back old settings + if [ -f "${HOME}/.zshrc_zimbackup" ] || [ -L "${HOME}/.zshrc_zimbackup" ]; then + printf "${YELLOW}Please inspect ${HOME}/.zshrc_zimbackup and copy over${NORMAL}\n"; + printf "${YELLOW}wanted settings to ${HOME}/.zshrc${NORMAL}\n"; + fi; + if [ -f "${HOME}/.zlogin_zimbackup" ] || [ -L "${HOME}/.zlogin_zimbackup" ]; then + printf "${YELLOW}Please inspect ${HOME}/.zlogin_zimbackup and copy over${NORMAL}\n"; + printf "${YELLOW}wanted settings to ${HOME}/.zlogin${NORMAL}\n"; + fi; + + # Reload zsh + env zsh +} + +main