#!/usr/bin/env bash
set -o nounset
set -o pipefail
TAG="CMD"
LOG_PATH="/tmp/shell/logs"
LOG_FILE="${LOG_PATH}/install_zsh_$(date +"%Y%m%d").log"
HIDE_LOG=true
user_name="$(whoami)"
function log() {
[ ! -d "${LOG_PATH}" ] && mkdir -p "${LOG_PATH}"
if [ $HIDE_LOG ]; then
echo -e "[$(date +"%Y/%m/%d:%H:%M:%S %z")] [$(whoami)] [$TAG]" "${@}" >> "${LOG_FILE}"
else
echo "[$(date +"%Y/%m/%d:%H:%M:%S %z")] [$(whoami)] [$TAG]" "${@}" | tee -a "${LOG_FILE}"
fi
}
function script_trap_err() {
local exit_code=1
trap - ERR
set +o errexit
set +o pipefail
log "[E] ERROR" "${@}"
status_closure clear_zsh_env
exit "$exit_code"
}
function script_trap_exit() {
log "[I] shell exec done."
}
function print_color () {
case $1 in
red) echo -e "\033[31m$2 \033[0m" ;;
green) echo -e "\033[32m$2 \033[0m" ;;
yellow) echo -e "\033[33m$2 \033[0m" ;;
blue) echo -e "\033[34m$2 \033[0m" ;;
*) echo -e "\033[31m[Color Error]$2 \033[0m" ;;
esac
}
function status_closure () {
print_color "green" "${1}"
eval "${*}"
print_color "green" "${1} executed successfully"
}
function help() {
echo "Usage: ./install_ohmyzsh.sh [-h -v -c -r]"
echo " -h : display this help and exit"
echo " -v : print zsh version and exit"
echo " -c : config zsh env and exit"
echo " -r : remove zsh env and exit"
exit 0
}
function get_user_shell() {
grep "${user_name}" /etc/passwd | awk -F: '{print $NF }'
}
function install_ohmyzsh() {
echo y | sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
}
function check_zsh_install() {
isInstall=$(grep -c zsh /etc/shells)
if [ "${isInstall}" -eq 0 ]
then
print_color "green" "start install zsh"
sudo apt-get -y install zsh
status_closure set_default_shell "/bin/zsh"
status_closure install_ohmyzsh
else
print_color "green" "zsh has been installed"
print_color "blue" "version: $(zsh --version)"
fi
}
function print_zsh_version() {
if [ "$(which zsh)" ]
then
zsh --version
else
print_color "red" "zsh has not been installed yet"
print_color "red" "please install zsh first!"
fi
}
function set_default_shell() {
print_color "green" "change ${user_name} default shell"
if [ "$(get_user_shell)" != "${1}" ];then sudo usermod -s "${1}" vagrant;fi
print_color "blue" "current shell: $(get_user_shell)"
print_color "yellow" "shell change succeeded. please login again"
print_color "green" "${user_name} default shell is ${1}"
}
function config_oh_my_zsh() {
local ZSH_CUSTOM
ZSH_CUSTOM="${HOME}/.oh-my-zsh/custom"
if [ ! -d "${ZSH_CUSTOM}/themes/spaceship-prompt" ]
then
print_color "green" "install zsh spaceship theme"
git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"
else
print_color "green" "zsh spaceship-prompt theme installed"
fi
if [ ! -d "${ZSH_CUSTOM}/plugins/zsh-autosuggestions" ]
then
print_color "green" "install zsh zsh-autosuggestions plugin"
git clone https://github.com/zsh-users/zsh-autosuggestions "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}"/plugins/zsh-autosuggestions
else
print_color "green" "zsh zsh-autosuggestions plugin installed"
fi
if [ ! -d "${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting" ]
then
print_color "green" "install zsh zsh-syntax-highlighting plugin"
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}"/plugins/zsh-syntax-highlighting
else
print_color "green" "zsh zsh-syntax-highlighting plugin installed"
fi
print_color "green" "config .zshrc"
if [ -f "${HOME}/.zshrc" ]; then rm -rf "${HOME}/.zshrc"; fi
wget https://gitea.com/neet11/config-dev-env/raw/branch/main/.zshrc -P "${HOME}/" && chmod 644 "${HOME}/.zshrc"
if [ -f "${HOME}/.spacevimrc.zsh" ]; then rm -rf "${HOME}/.spacevimrc.zsh"; fi
wget https://gitea.com/neet11/config-dev-env/raw/branch/main/.spaceshiprc.zsh -P "${HOME}/" && chmod 644 "${HOME}/.spaceshiprc.zsh"
print_color "green" "zsh configfile .zshrc has been updated"
print_color "yellow" "reload .zshrc use source ${HOME}/.zshrc"
}
function clear_zsh_env() {
local uninstall_oh_my_zsh
uninstall_oh_my_zsh="${HOME}/.oh-my-zsh/tools/uninstall.sh"
print_color "green" "remove the oh my zsh"
if [ -f "${uninstall_oh_my_zsh}" ]
then
echo y | bash "${uninstall_oh_my_zsh}"
fi
if [ -f "${HOME}/.zshrc" ]
then
sudo rm -rf "${HOME}"/.zshrc*
fi
print_color "green" "remove the installed zsh"
sudo apt-get -y --purge autoremove zsh > /dev/null
print_color "green" "change default shell to bash"
if [ "$(get_user_shell)" != "/bin/bash" ]
then
status_closure set_default_shell "/bin/bash"
else
print_color "blue" "current shell: $(get_user_shell)"
print_color "yellow" "clear zsh succeeded. please login again"
fi
}
function run_install_zsh() {
status_closure check_zsh_install
}
function main() {
trap script_trap_err INT TERM QUIT HUP ERR
trap script_trap_exit EXIT
log "[I] shell start"
if [ "${#}" -ne 0 ]
then
case $1 in
-h|help)
help
;;
-v|version)
print_zsh_version
;;
-r|remove)
status_closure clear_zsh_env
;;
-c|config)
status_closure config_oh_my_zsh
;;
*)
print_color "red" "unknown parameter" && help
;;
esac
else
status_closure run_install_zsh
fi
}
main "${@}"