#! /bin/sh
# sudo sh -c "$(wget --no-check-certificate http://firmware.koolshare.cn/shell/zsh/clang-zsh.sh -O -)"
version="0.9"
shell_url="http://firmware.koolshare.cn/shell/zsh"
zsh_source="http" # http or git
clang_theme="${shell_url}/clang.zsh-theme"
zsh_http="${shell_url}/oh-my-zsh.tar.gz"
Get_Dist_Name() {
    systemPackage=''
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        systemPackage='yum'
    elif grep -Eqi "centos|red hat|redhat" /etc/issue || grep -Eqi "centos|red hat|redhat" /etc/*-release; then
        systemPackage='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        systemPackage='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        systemPackage='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        systemPackage='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        systemPackage='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        systemPackage='apt'
    elif grep -Eqi "Deepin" /etc/issue || grep -Eq "Deepin" /etc/*-release; then
        systemPackage='apt'
    else
        systemPackage='unknow'
    fi
}
check_sys() {
    local value=$1
    if [ "$value"x = "$systemPackage"x ]; then
        return 0
    else
        return 1
    fi
}
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
    Get_Dist_Name
    if [ "${systemPackage}"x = "unknow"x ]; then
        printf "${YELLOW}Error: Unable to get Linux distribution name, or do NOT support the current distribution.${NORMAL}\n"
        exit
    fi
    printf "${BLUE}+ Check ZSH Autojump git ...${NORMAL}\n"
    autojump -v >/dev/null 2>&1
    CHECK_AUTOJUMP_INSTALLED=$?
    git --version >/dev/null 2>&1
    CHECK_GIT_INSTALLED=$?
    locale -V >/dev/null 2>&1
    CHECK_LOCALE_INSTALLED=$?
    CHECK_ZSH_INSTALLED=$(grep -c /zsh$ /etc/shells)
    if [ ! $CHECK_ZSH_INSTALLED -ge 1 ] || [ $CHECK_AUTOJUMP_INSTALLED -ge 1 ] || [ $CHECK_GIT_INSTALLED -ge 1 ] || [ $CHECK_LOCALE_INSTALLED -ge 1 ];then
        printf "${BLUE}+ Install support packs...${NORMAL}\n"
        if check_sys yum; then
            ( set -x; yum install -y epel-release; yum install -y --enablerepo=base,epel zsh autojump-zsh git )
        elif check_sys apt; then
            ( set -x; apt-get update; apt-get -y install --no-install-recommends zsh autojump git locales )
        fi
    fi
    unset CHECK_ZSH_INSTALLED CHECK_AUTOJUMP_INSTALLED CHECK_GIT_INSTALLED CHECK_LOCALE_INSTALLED

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

    if [ -d "$ZSH" ]; then
        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"
        exit
    fi

    # 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

    printf "${BLUE}Cloning Oh My Zsh...${NORMAL}\n"
    hash git >/dev/null 2>&1 || {
        echo "Error: git is not installed"
        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
        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
    if [ "${zsh_source}"x = "http"x ]; then
        ( set -x; wget --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${zsh_http} -O /tmp/oh-my-zsh.tar.gz;
          cd /tmp; tar xzf /tmp/oh-my-zsh.tar.gz; mv /tmp/oh-my-zsh $ZSH; rm -f /tmp/oh-my-zsh.tar.gz; cd ~ )
    else
        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
        }
    fi

    printf "${BLUE}Looking for an existing zsh config...${NORMAL}\n"
    if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
        printf "${YELLOW}Found ~/.zshrc.${NORMAL} ${GREEN}Backing up to ~/.zshrc.pre-oh-my-zsh${NORMAL}\n";
        mv ~/.zshrc ~/.zshrc.pre-oh-my-zsh;
    fi

    printf "${BLUE}Using the Oh My Zsh template file and adding it to ~/.zshrc${NORMAL}\n"
    cp $ZSH/templates/zshrc.zsh-template ~/.zshrc
    sed "/^export ZSH=/ c\\
    export ZSH=$ZSH
    " ~/.zshrc > ~/.zshrc-omztemp
    mv -f ~/.zshrc-omztemp ~/.zshrc

    # If this user's login shell is not already "zsh", attempt to switch.
    TEST_CURRENT_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
    if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
        # If this platform provides a "chsh" command (not Cygwin), do it, man!
        if hash chsh >/dev/null 2>&1; then
        printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n"
        chsh -s $(grep /zsh$ /etc/shells | tail -1)
        # Else, suggest the user do so manually.
        else
        printf "I can't change your shell automatically because this system does not have chsh.\n"
        printf "${BLUE}Please manually change your default shell to zsh!${NORMAL}\n"
        fi
    fi
    echo ""
    printf "${BLUE}Download zsh-theme...${NORMAL}\n"
    wget --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${clang_theme} -O ~/.oh-my-zsh/custom/themes/clang.zsh-theme
    printf "${BLUE}Setting zsh ...${NORMAL}\n"
    sed -i 's/ZSH_THEME=.*/ZSH_THEME="clang"/g' $HOME/.zshrc
    if [ -f "/etc/locale.gen" ]; then
        sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
        locale-gen
    fi
    if ! locale -a | grep -qi "en_US.utf8"; then
        localedef -v -c -i en_US -f UTF-8 en_US.UTF-8 > /dev/null 2>&1
    fi
    #if ! locale -a | grep -qi "zh_CN.utf8"; then
        #localedef -v -c -i zh_CN -f UTF-8 zh_CN.UTF-8 > /dev/null 2>&1
    #fi
    if ! grep -qiE "zh_CN.utf8|en_US.utf8|C.UTF-8" $HOME/.zshrc; then
        if locale -a | grep -iq "zh_CN.utf8"; then
            echo "export LANG=zh_CN.utf8" >> $HOME/.zshrc;
        elif locale -a | grep -iq "en_US.utf8"; then
            echo "export LANG=en_US.utf8" >> $HOME/.zshrc;
        elif locale -a | grep -iq "C.UTF-8"; then
            echo "export LANG=C.UTF-8" >> $HOME/.zshrc;
        fi
    fi
    if ! grep -q "alias ls=" $HOME/.zshrc; then echo "alias ls='ls --color=auto'" >> $HOME/.zshrc; fi
    if ! grep -q "alias ll=" $HOME/.zshrc; then echo "alias ll='ls -lh'" >> $HOME/.zshrc; fi
    if ! grep -q "alias l=" $HOME/.zshrc; then echo "alias l='ls -lAh'" >> $HOME/.zshrc; fi
    if ! grep -q "alias wget=" $HOME/.zshrc; then echo "alias wget='wget --no-check-certificate'" >> $HOME/.zshrc; fi
    if ! grep -q "alias vi=" $HOME/.zshrc; then echo "alias vi='vim'" >> $HOME/.zshrc; fi
    if ! grep -q "setopt no_nomatch" $HOME/.zshrc; then echo "setopt no_nomatch" >> $HOME/.zshrc; fi
    if ! grep -q "autojump" $HOME/.zshrc && [ -s /usr/share/autojump/autojump.zsh ]; then
        echo "[[ -s /usr/share/autojump/autojump.zsh ]] && source /usr/share/autojump/autojump.zsh" >> $HOME/.zshrc
    fi

    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 ''
    printf "${NORMAL}"
    if [ "${arg1}"x = ""x ]; then
        env zsh
    fi
}
arg1=$(echo $1 | tr '[A-Z]' '[a-z]')
main
