sh.tforgione.fr/tforgione.sh

512 lines
15 KiB
Bash
Executable File

#!/usr/bin/env bash
user=$USER
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() {
for i in $@; do
command -v $i > /dev/null 2>&1
res=$?
if [ $res -ne 0 ]; then
return $res
fi
done
}
yes_no_ask() {
info_n $@ "[Y/n]"
read answer
if ! [[ "$answer" == "N" ]] && ! [[ "$answer" == "n" ]]; then
return 0
else
return 1
fi
}
has_sudo=0
has_apt=0
has_pacman=0
should_install_git=0
should_install_zsh=0
should_clone_dotfiles=0
should_install_dotfiles=0
should_install_update_remainder=0
should_install_neovim=0
should_configure_neovim=0
should_install_rust=0
should_install_nodejs=0
should_configure_nodejs=0
should_install_python=0
should_configure_python=0
should_install_hyprland=0
find_os() {
apt --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
has_apt=1
return
fi
pacman --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
has_pacman=1
return
fi
error_n "os not supported"
exit 1
}
ask_for_dotfiles() {
test_command zsh
if [ $? -eq 0 ]; then
has_zsh=1
else
has_zsh=0
fi
test_command git
if [ $? -eq 0 ]; then
has_git=1
else
has_git=0
fi
if [ $has_sudo -eq 0 ]; then
if [ $has_zsh -eq 0 ] || [ $has_git -eq 0 ]; then
warn "can't install dotfiles without sudo, or zsh and git"
return
fi
fi
yes_no_ask "do you wish to install and configure dotfiles?"
if [ $? -eq 0 ]; then
if [ $has_zsh -eq 0 ]; then
should_install_zsh=1
fi
if [ $has_git -eq 0 ]; then
should_install_git=1
fi
should_clone_dotfiles=1
should_install_dotfiles=1
yes_no_ask "do you want a weekly remainder to do your system's update?"
if [ $? -eq 0 ]; then
should_install_update_remainder=1
fi
fi
}
ask_for_neovim() {
test_command nvim
if [ $? -eq 0 ]; then
has_neovim=1
else
has_neovim=0
fi
if [ $has_neovim -eq 0 ] && [ $has_sudo -eq 0 ]; then
warn "can't install neovim without sudo"
return
fi
if [ $has_neovim -eq 1 ]; then
yes_no_ask "do you wish to configure neovim?"
if [ $? -eq 0 ]; then
should_clone_dotfiles=1
should_configure_neovim=1
fi
else
yes_no_ask "do you wish to install and configure neovim?"
if [ $? -eq 0 ]; then
should_clone_dotfiles=1
should_install_neovim=1
should_configure_neovim=1
fi
fi
}
ask_for_rust() {
yes_no_ask "do you wish to install and configure rust?"
if [ $? -ne 0 ]; then
return
fi
test_command rustc
if [ $? -ne 0 ]; then
should_install_rust=1
fi
}
ask_for_python() {
test_command python
if [ $? -eq 0 ]; then
has_python=1
else
has_python=0
fi
if [ $has_python -eq 0 ] && [ $has_sudo -eq 0 ]; then
warn "can't install python without sudo"
return
fi
if [ $has_python -eq 1 ]; then
yes_no_ask "do you wish to configure python?"
if [ $? -eq 0 ]; then
should_configure_python=1
fi
else
yes_no_ask "do you wish to install and configure python?"
if [ $? -eq 0 ]; then
should_install_python=1
should_configure_python=1
fi
fi
}
ask_for_nodejs() {
test_command node
if [ $? -eq 0 ]; then
has_node=1
else
has_node=0
fi
if [ $has_node -eq 0 ] && [ $has_sudo -eq 0 ]; then
warn "can't install node without sudo"
return
fi
if [ $has_node -eq 1 ]; then
yes_no_ask "do you wish to configure nodejs?"
if [ $? -eq 0 ]; then
should_configure_node=1
fi
else
yes_no_ask "do you wish to install and configure nodejs?"
if [ $? -eq 0 ]; then
should_install_nodejs=1
should_configure_nodejs=1
fi
fi
}
ask_for_hyprland() {
if [ $has_sudo -eq 0 ]; then
return
fi
if [ $has_pacman -ne 1 ]; then
warn "can't install hyprland on this OS"
return
fi
yes_no_ask "do you wish to install and configure hyprland?"
if [ $? -ne 0 ]; then
return
fi
should_clone_dotfiles=1
should_install_hyprland=1
}
debug() {
if [ $should_install_git -eq 1 ]; then
info "should install git"
fi
if [ $should_install_zsh -eq 1 ]; then
info "should install zsh"
fi
if [ $should_install_dotfiles -eq 1 ]; then
info "should install dotfiles"
fi
if [ $should_install_neovim -eq 1 ]; then
info "should install neovim"
fi
if [ $should_configure_neovim -eq 1 ]; then
info "should configure neovim"
fi
if [ $should_install_rust -eq 1 ]; then
info "should install rust"
fi
if [ $should_install_python -eq 1 ]; then
info "should install python"
fi
if [ $should_configure_python -eq 1 ]; then
info "should configure python"
fi
if [ $should_install_nodejs -eq 1 ]; then
info "should install nodejs"
fi
if [ $should_configure_nodejs -eq 1 ]; then
info "should configure nodejs"
fi
if [ $should_install_hyprland -eq 1 ]; then
info "should install hyprland"
fi
}
run() {
packages=()
python_packages=()
if [ $should_install_git -eq 1 ]; then
packages+=(git)
fi
if [ $should_install_zsh -eq 1 ]; then
packages+=(zsh)
fi
if [ $should_install_neovim -eq 1 ]; then
if [ $has_pacman -eq 1 ]; then
packages+=(neovim)
fi
fi
if [ $should_install_nodejs -eq 1 ]; then
packages+=(nodejs)
if [ $has_pacman -eq 1 ]; then
packages+=(npm)
fi
fi
if [ $should_install_python ]; then
# Install python
if [ $has_apt -eq 1 ]; then
packages+=(python3)
packages+=(python-is-python3)
packages+=(python3-pip)
packages+=(python3-venv)
elif [ $has_pacman -eq 1 ]; then
packages+=(python)
fi
# Install some python plugins that work well with neovim if necessary
if [ $should_configure_neovim -eq 1 ]; then
python_packages+=(neovim)
python_packages+=(pycodestyle)
python_packages+=("python-lsp-server[all]")
fi
fi
if [ $should_install_hyprland -eq 1 ]; then
packages+=(hyprland)
packages+=(hyprpaper)
packages+=(hyprpicker)
packages+=(egl-wayland)
packages+=(pavucontrol)
packages+=(waybar)
packages+=(slurp)
packages+=(wl-clipboard)
packages+=(brightnessctl)
python_packages+=(psutil)
fi
# Add apt repository for recent nodejs for debian
if [ $should_install_nodejs -eq 1 ] && [ $has_apt -eq 1 ]; then
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo bash
fi
if [ $has_sudo -eq 1 ]; then
if [ $has_apt -eq 1 ]; then
sudo apt install -y ${packages[@]}
if [ $should_install_neovim ]; then
# Install neovim from github to get latest version
curl -L https://github.com/neovim/neovim/releases/download/stable/nvim-linux-x86_64.tar.gz -o tmp.tar.gz
sudo mkdir -p /opt/nvim
sudo tar xf tmp.tar.gz -C /opt/nvim --strip-components=1
sudo ln -s /opt/nvim/bin/nvim /usr/bin/nvim
rm tmp.tar.gz
fi
elif [ $has_pacman -eq 1 ]; then
sudo pacman -Sy ${packages[@]} --noconfirm --needed
fi
if [ $should_configure_dotfiles ]; then
sudo chsh $USER -s /usr/bin/zsh
fi
fi
# Prepare venv for python
if [ $should_configure_python -eq 1 ]; then
python -m venv $HOME/.venv
VIRTUAL_ENV=$HOME/.venv $HOME/.venv/bin/pip install ${python_packages[@]}
fi
# Configure dotfiles
if [ $should_clone_dotfiles -eq 1 ]; then
if [ ! -d $HOME/.config/dotfiles ]; then
git clone https://gitea.tforgione.fr/tforgione/dotfiles $HOME/.config/dotfiles
fi
fi
if [ $should_install_dotfiles -eq 1 ]; then
if [ ! -d $HOME/.config/ohmyzsh ]; then
git clone https://github.com/ohmyzsh/ohmyzsh $HOME/.config/ohmyzsh
fi
ln -s $HOME/.config/dotfiles/zshrc $HOME/.zshrc
if [ $should_install_update_remainder -eq 1 ]; then
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
echo "export UPDATE_CHECK_TYPE=sliding" >> $HOME/.config/dotfiles/zsh/extraconfig.zsh
export UPDATE_CHECK=weekly
export UPDATE_CHECK_TYPE=sliding
$HOME/.config/dotfiles/bin/update postpone
if [ $has_sudo -eq 0 ]; then
mkdir -p $HOME/.config/dotfiles/.data
touch $HOME/.config/dotfiles/.data/noroot
fi
fi
fi
# Configure hyprland
if [ $should_install_hyprland -eq 1 ]; then
ln -s $HOME/.config/dotfiles/hypr $HOME/.config/hypr
ln -s $HOME/.config/dotfiles/hypr/waybar $HOME/.config/waybar
touch $HOME/.config/dotfiles/hypr/exec-once.conf
touch $HOME/.config/dotfiles/hypr/monitors.conf
fi
# Configure neovim
if [ $should_configure_neovim -eq 1 ]; then
# Create config files an directories
mkdir -p $HOME/.nvim/backups $HOME/.nvim/swp $HOME/.nvim/undo
mkdir -p $HOME/.config/nvim
ln -s $HOME/.config/dotfiles/nvim/init.lua $HOME/.config/nvim/init.lua
# 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
fi
# Configure nodejs
if [ $should_configure_nodejs -eq 1 ]; then
mkdir -p $HOME/.npmbin
npm config set prefix $HOME/.npmbin
npm install -g npm
fi
# Configure rust
if [ $should_install_rust -eq 1 ]; then
curl https://sh.rustup.rs -sSf | sh -s -- --no-modify-path -y
export PATH=$HOME/.cargo/bin:$PATH
rustup component add rust-analyzer
fi
}
banner() {
echo -e "\x1b[32;1m┌───────────────────────────────────────────────────────────────────────────────────────────┐"
echo "│ _ __ _ _ _ _ _ _ │"
echo "│ | | / _| (_) ( ) (_) | | | | | │"
echo "│ | |_| |_ ___ _ __ __ _ _ ___ _ __ ___|/ ___ _ _ __ ___| |_ __ _| | | ___ _ __ │"
echo "│ | __| _/ _ \| '__/ _\` | |/ _ \| '_ \ / _ \ / __| | | '_ \/ __| __/ _\` | | |/ _ \ '__| │"
echo "│ | |_| || (_) | | | (_| | | (_) | | | | __/ \__ \ | | | | \__ \ || (_| | | | __/ | │"
echo "│ \__|_| \___/|_| \__, |_|\___/|_| |_|\___| |___/ |_|_| |_|___/\__\__,_|_|_|\___|_| │"
echo "│ __/ | │"
echo "│ |___/ │"
echo "├───────────────────────────────────────────────────────────────────────────────────────────┤"
echo "│ WELCOME TO TFORGIONE'S INSTALLER │"
echo -e "└───────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m"
}
ending_banner() {
echo -e "\x1b[32;1m┌───────────────────────────────────────────────────────────────────────────────────────────┐"
echo "│ _ __ _ _ _ _ _ _ │"
echo "│ | | / _| (_) ( ) (_) | | | | | │"
echo "│ | |_| |_ ___ _ __ __ _ _ ___ _ __ ___|/ ___ _ _ __ ___| |_ __ _| | | ___ _ __ │"
echo "│ | __| _/ _ \| '__/ _\` | |/ _ \| '_ \ / _ \ / __| | | '_ \/ __| __/ _\` | | |/ _ \ '__| │"
echo "│ | |_| || (_) | | | (_| | | (_) | | | | __/ \__ \ | | | | \__ \ || (_| | | | __/ | │"
echo "│ \__|_| \___/|_| \__, |_|\___/|_| |_|\___| |___/ |_|_| |_|___/\__\__,_|_|_|\___|_| │"
echo "│ __/ | │"
echo "│ |___/ │"
echo "├───────────────────────────────────────────────────────────────────────────────────────────┤"
echo "│ TFORGIONE'S DOTFILES INSTALLED! HAVE A GREAT DAY! │"
echo -e "└───────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m"
}
main() {
banner
find_os
info "before anything, i need to know if your user can use sudo"
yes_no_ask "can your user use sudo?"
if [ $? -eq 0 ]; then
has_sudo=1
fi
ask_for_dotfiles
ask_for_neovim
ask_for_rust
ask_for_python
ask_for_nodejs
ask_for_hyprland
debug
run
ending_banner
}
main < /dev/tty