sh.tforgione.fr/tforgione.sh

322 lines
7.8 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 -yqq $1
return
fi
test_command pacman
if [ $? -eq 0 ]; then
sudo pacman -S $1
return
fi
error "distribution not recognized, can't install $1."
}
install_python3() {
test_command apt
if [ $? -eq 0 ]; then
sudo apt install -yqq python3 python3-pip
return
fi
test_command pacman
if [ $? -eq 0 ]; then
sudo pacman -S python python-pip
return
fi
error "distribution not recognized, can't install python3."
}
install_python_neovim() {
test_command pip3
if [ $? -eq 0 ]; then
sudo pip3 install neovim
return
fi
test_command pip
if [ $? -eq 0 ]; then
sudo pip install neovim
return
fi
error "couldn't find pip, can't install python-neovim."
}
install_neovim() {
test_command apt
if [ $? -eq 0 ]; then
test_command add-apt-repository
if [ $? -ne 0 ]; then
sudo apt update -qq && sudo apt install -yqq software-properties-common
fi
sudo add-apt-repository -y ppa:neovim-ppa/stable
sudo apt update -yqq
sudo apt install -yqq neovim
return
fi
test_command pacman
if [ $? -eq 0 ]; then
sudo pacman -S neovim
return
fi
error "distribution not recognized, can't install neovim."
}
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
}
clone_dotfiles() {
if [ ! -d $HOME/.config/dotfiles ]; then
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
git_clone https://gitea.tforgione.fr/tforgione/dotfiles/ $HOME/.config/dotfiles
echo "# Checks that the update is done weekly, if its not done, prints a nice message" > $HOME/.config/dotfiles/zsh/extraconfig.zsh
echo "export UPDATE_CHECK=weekly" >> $HOME/.config/dotfiles/zsh/extraconfig.zsh
mkdir -p $HOME/.config/dotfiles/.data
if [ $sudo_available -ne 0 ]; then
touch $HOME/.config/dotfiles/.data/noroot
fi
fi
}
configure_dotfiles() {
if [ ! -d $HOME/.config/dotfiles ]; then
yes_no_ask "do you wish to configure the dotfiles ?"
if [ $? -ne 0 ]; then
return
fi
clone_dotfiles
fi
}
configure_shell() {
yes_no_ask "do you wish to install and configure zsh ?"
if [ $? -ne 0 ]; then
yes_no_ask "you don't want zsh ? Ok... but can I at least give you a nice bashrc ?"
if [ $? -ne 0 ]; then
info "ok :'( I guess I'm not doing anything then"
return
fi
clone_dotfiles
rm -f $HOME/.bashrc
ln -s `realpath $HOME/.config/dotfiles/bashrc` `realpath $HOME/.bashrc`
mkdir -p $HOME/.config/dotfiles/bash
echo "# Checks that the update is done weekly, if its not done, prints a nice message" > $HOME/.config/dotfiles/bash/extraconfig.bash
echo "export UPDATE_CHECK=weekly" >> $HOME/.config/dotfiles/bash/extraconfig.bash
info "bashrc configured successfully"
return
fi
clone_dotfiles
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 [ -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 ?"
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 >> $HOME/.bashrc
echo "exec zsh" >> $HOME/.bashrc
echo >> $HOME/.bashrc
else
info "not doing anything"
fi
fi
fi
}
configure_neovim() {
test_command nvim
if [ $? -ne 0 ] && [ $sudo_available -ne 0 ]; then
warn "you don't have neither sudo nor neovim, skipping neovim configuration"
return
fi
yes_no_ask "do you wish to install and configure neovim ?"
if [ $? -ne 0 ]; then
info "not installing neovim"
return
fi
clone_dotfiles
test_command nvim
if [ $? -ne 0 ]; then
info "installing neovim"
install_neovim
fi
# Try and install python3 and python-neovim
test_command python3
if [ $? -ne 0 ]; then
install_python3
fi
install_python_neovim
# Create config files an directories
mkdir -p $HOME/.config/nvim $HOME/.nvim/backups $HOME/.nvim/swp $HOME/.nvim/undo
rm -f $HOME/.config/nvim/init.vim
ln -s $HOME/.config/dotfiles/init.vim $HOME/.config/nvim/init.vim
# Install vim plug
curl -fLo $HOME/.local/share/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
# Install plugins
nvim +PlugInstall +qa
}
main() {
echo_green "=== WELCOME TO TFORGIONE'S CONFIG INSTALLER ==="
info "before anything, i need to know if your user can use sudo"
yes_no_ask "can your user use sudo ?"
sudo_available=$?
configure_dotfiles
configure_shell
configure_neovim
}
main < /dev/tty