sh.tforgione.fr/tforgione.sh

183 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env sh
# This script installs the default things for having a stylish zsh
user_shell=`getent passwd $USER | cut -f7 -d:`
info() {
echo -e '\033[1m'info:'\033[0m' $@
}
info_n() {
echo -en '\033[1m'info:'\033[0m' $@
}
warn() {
echo -e '\033[33;1m'warning:'\033[0m' $@
}
warn_n() {
echo -en '\033[33;1m'warning:'\033[0m' $@
}
error() {
echo -e '\033[31;1m'error:'\033[0m' $@
}
error_n() {
echo -en '\033[31;1m'error:'\033[0m' $@
}
echo_green() {
echo -e '\033[32;1m'$@'\033[0m'
}
test_command() {
command -v $1 > /dev/null 2>&1
}
yes_no_ask_required() {
yes_no_ask $@ "(required)"
if [ $? -ne 0 ]; then
error "refused to perform required task, can't continue"
exit 1
fi
}
yes_no_ask() {
info_n $@ "[Y/n]"
read answer
if ! [[ "$answer" == "N" ]] && ! [[ "$answer" == "n" ]]; then
return 0
else
return 1
fi
}
install() {
test_command apt
if [ $? -eq 0 ]; then
sudo apt install -y $1
return
fi
test_command pacman
if [ $? -eq 0 ]; then
sudo pacman -S $1
return
fi
error "distribution not recognized, can't install $1."
}
git_clone() {
info_n "cloning $1..."
git clone $1 $2 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo " OK!"
else
echo
error "clone failed!"
fi
}
configure_zsh() {
yes_no_ask "do you wish to install and configure zsh ?"
if [ $? -ne 0 ]; then
info "exiting"
return
fi
test_command git
if [ $? -ne 0 ]; then
if [ $sudo_available -ne 0 ]; then
error "git is needed, but you don't have git neither sudo"
return 1
else
yes_no_ask_required "you'll need git for this, do you wish to install git ?"
install git
fi
fi
test_command zsh
if [ $? -ne 0 ]; then
if [ $sudo_available -ne 0 ]; then
error "zsh is needed, but you don't have zsh neither sudo"
return 1
else
yes_no_ask_required "this magnificent prompt is based on zsh, do you wish to install zsh ?"
install zsh
fi
fi
mkdir -p $HOME/.config
if [ ! -d $HOME/.config/oh-my-zsh ]; then
git_clone https://github.com/robbyrussell/oh-my-zsh/ $HOME/.config/oh-my-zsh
fi
if [ ! -d $HOME/.config/dotfiles ]; then
git_clone https://gitea.tforgione.fr/tforgione/dotfiles/ $HOME/.config/dotfiles
fi
if [ -f $HOME/.zshrc ]; then
path1=`realpath $HOME/.zshrc`
path2=`realpath $HOME/.config/dotfiles/zshrc`
if [ "$path1" == "$path2" ]; then
info "it seems that your zshrc is already a good link."
else
if `realpath $HOME/.zshrc`; then
warn "you already have a zshrc, it will be moved to ~/.zshrc.bak"
mv $HOME/.zshrc $HOME/.zshrc.bak
fi
info "linking zshrc"
ln -s $HOME/.config/dotfiles/zshrc $HOME/.zshrc
fi
else
info "linking zshrc"
ln -s $HOME/.config/dotfiles/zshrc $HOME/.zshrc
fi
if [ "$user_shell" == "/bin/zsh" ]; then
info "it seems that you already use zsh."
else
info "it seems your shell is not zsh..."
if [ $sudo_available -eq 0 ]; then
# Sudo is available, run chsh
yes_no_ask "do you want to change your shell ? (you'll need root) ?"
if [ $? -eq 0 ]; then
info "changing your shell"
sudo chsh $user -s /bin/zsh
else
info "not doing anything"
fi
else
info "you don't have root, but you can run 'exec zsh' at the end of your bashrc"
yes_no_ask "do you wish to do that ?"
if [ $? -eq 0 ]; then
echo "\nexec zsh\n" >> $HOME/.bashrc
else
info "not doing anything"
fi
fi
fi
}
main() {
echo_green "=== WELCOME TO TFORGIONE'S CONFIG INSTALLER ==="
configure_zsh
}
sudoresult=$(sudo -nv 2>&1)
if [ $? -eq 0 ]; then
sudo_available=0
elif echo $sudoresult | grep -q '^sudo:'; then
sudo_available=0
else
sudo_available=1
fi
main < /dev/tty