#!/usr/bin/bash
cd ~

# check if oh my zsh installed
if [ ! -d ~/.oh-my-zsh ]; then
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
fi

# remove Powerlevel10k theme
if [ -n $(sed -n '/Enable Powerlevel10k instant prompt/=' ~/.zshrc) ]; then
    sed -i '1,6d' ~/.zshrc
fi
if [ -n $(sed -n '/source ~\/.p10k.zsh/=' ~/.zshrc) ]; then
    sed -i -e '/# To customize prompt, run `p10k configure` or edit ~\/.p10k.zsh./d' ~/.zshrc
    sed -i -e '/^\[\[ \! -f ~\/.p10k.zsh .*/d' ~/.zshrc
fi

# install starship theme & alias plugin
if [ ! -d ~/.oh-my-zsh/custom/plugins/rainbow-alias ]; then
    mkdir -p ~/.oh-my-zsh/custom/plugins/rainbow-alias/
    curl -o ~/.oh-my-zsh/custom/plugins/rainbow-alias/rainbow-alias.plugin.zsh https://scm.i-blacknet.de/mkurz/rainbow-zsh-theme/raw/branch/master/rainbow-alias.plugin.zsh
fi
curl -o ~/.config/starship.toml https://scm.i-blacknet.de/mkurz/starship-theme/raw/branch/main/starship.toml

# install needfull plugins
if [ ! -d ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions ]; then
    git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
fi
if [ ! -d ~/.oh-my-zsh/custom/plugins/zsh-history-substring-search ]; then
    git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search
fi
if [ ! -d ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting ]; then
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
fi

# add neefull plugins
if [ -z $(sed -n '/rainbow-alias/=' ~/.zshrc) ]; then
    sed -i -e 's/\(^plugins=([^)]*\)/\1 rainbow-alias/' ~/.zshrc
fi
if [ -z $(sed -n '/zsh-autosuggestions/=' ~/.zshrc) ]; then
    sed -i -e 's/\(^plugins=([^)]*\)/\1 zsh-autosuggestions/' ~/.zshrc
fi
if [ -z $(sed -n '/zsh-history-substring-search/=' ~/.zshrc) ]; then
    sed -i -e 's/\(^plugins=([^)]*\)/\1 zsh-history-substring-search/' ~/.zshrc
fi
if [ -z $(sed -n '/zsh-syntax-highlighting/=' ~/.zshrc) ]; then
    sed -i -e 's/\(^plugins=([^)]*\)/\1 zsh-syntax-highlighting/' ~/.zshrc
fi

echo -e '\neval "$(starship init zsh)"' >> ~/.zshrc

echo "Change Shell to zsh when you want!"
echo "chsh -s $(which zsh)"