fresh/fresh.sh

339 lines
11 KiB
Bash
Raw Normal View History

2018-09-15 22:17:30 +02:00
#!/bin/bash
2022-03-12 14:59:45 +01:00
# Author: Philip Gaber <phga@posteo.de>
2018-09-15 22:17:30 +02:00
2018-09-15 23:15:38 +02:00
scriptdir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
2018-09-15 22:17:30 +02:00
# Get params
while getopts "r:p:d:vush" opt;
2018-09-15 22:17:30 +02:00
do case $opt in
h) echo -e "-r: https://link-to-repo\n-p: /link/to/prog/file\n-d: devicename for special dotfiles\n-v: verbose, please supply logfile path (default is $HOME/install.log)" && exit ;;
d) device=${OPTARG} ;;
r) repo=${OPTARG} && git ls-remote "$repo" || exit ;;
p) prog=${OPTARG} ;;
a) aurhelper=${OPTARG} ;;
2018-09-15 23:13:20 +02:00
v) logfile="$scriptdir/install.log" ;;
u) todo="update" ;;
s) todo="services" ;;
2018-09-15 22:17:30 +02:00
\?) echo "-$OPTARG is not valid" >&2 && exit ;;
esac
done
# Default settings
2018-09-15 22:17:30 +02:00
device=${device:="480"}
2021-03-12 16:19:36 +01:00
repo=${repo:="https://phga@g.phga.de/phga/dotfiles"}
prog=${prog:="$scriptdir/pack.csv"}
2018-09-15 22:17:30 +02:00
aurhelper=${aurhelper:="yay"}
logfile=${logfile:="/dev/null"}
todo=${todo:="install"}
2018-09-15 22:17:30 +02:00
startup_msg() {
2022-02-05 12:36:00 +01:00
curr_platform=$(dialog --clear --title "Welcome to the Freshcript <3" --menu "Choose your current platform:" 0 0 3 "L" "Laptop" "D" "Desktop" "S" "Server" 2>&1>/dev/tty)
echo "Script started - Platform is: $curr_platform\n\n" >> $logfile
2018-09-15 22:17:30 +02:00
}
2020-04-06 04:00:18 +02:00
wait_for_network() {
dialog --infobox "Waiting for internet connection...\n\nDumdidum dum dumdiii dum" 0 0
dhcpcd >> $logfile
while [ ! $OK = 0 ]; do
ping -c 1 -q "one.one.one.one" >> $logfile && OK=$?
done
}
2018-09-15 22:17:30 +02:00
init() {
2020-04-06 04:00:18 +02:00
wait_for_network
2021-12-11 03:45:22 +01:00
pacman -S --noconfirm --needed dialog git make || { echo "Run as root user"; clear; exit; }
refresh_keyring
}
refresh_keyring() {
2018-09-15 23:19:42 +02:00
pacman --noconfirm -Sy archlinux-keyring &>> $logfile
dialog --infobox "Refresh of the Keyring" 0 0
2018-09-15 22:17:30 +02:00
}
pre_install() {
dialog --title "Start the installation" --yes-label "Okey Dokey Artischokey" --no-label "NOPE Byeee" --yesno "\nIf you decide to start the installation, grab a beer and watch some youtube vids (On another device ofc)\n\nIf not... then you should just DIY ._." 0 0 || { clear; exit; }
echo "Automatic part started\n\n" >> $logfile
}
pre_update() {
uname=$(logname)
dialog --title "Start the Update" --yes-label "Okey Dokey Artischokey" --no-label "NOPE Byeee" --yesno "\nUpdateriniho as user > $uname <" 0 0 || { clear; exit; }
echo "Automatic update started as user:$uname\n\n" >> $logfile
}
2018-09-15 22:17:30 +02:00
get_credentials() {
uname=$(dialog --inputbox "Enter an username" 0 0 2>&1 1>/dev/tty)
echo $uname
while ! [[ $uname =~ ^[_a-z][_a-z0-9]{0,30}$ ]]; do
uname=$(dialog --no-cancel --inputbox "$uname Please enter a - valid - username" 0 0 2>&1 1>/dev/tty)
done
# Grab the password and check if it matches
passw=$(dialog --no-cancel --passwordbox "Enter a password" 0 0 2>&1 1>/dev/tty)
passr=$(dialog --no-cancel --passwordbox "Confirm password" 0 0 2>&1 1>/dev/tty)
while [[ ${passw} != ${passr} || ${#passw} -lt 8 ]]; do
passw=$(dialog --no-cancel --passwordbox "${#passw} Passwords did not match or length < 8\nEnter a password" 0 0 2>&1 1>/dev/tty)
passr=$(dialog --no-cancel --passwordbox "Confirm password" 0 0 2>&1 1>/dev/tty)
done
echo "Username and password successfully supplied\n\n" >> $logfile
}
create_user() {
dialog --infobox "Creating user - $uname" 0 0
2018-09-15 23:19:42 +02:00
useradd -m -g wheel -s /bin/bash "$uname" &>> $logfile || usermod -a -G wheel "$uname" && mkdir -p /home/"$uname" && chown "$uname":wheel /home/"$uname"
2018-09-15 22:17:30 +02:00
echo "$uname:$passw" | chpasswd
unset passw passr ;
echo "User successfully created\n\n" >> $logfile
}
install_pacman() {
2018-09-15 23:13:20 +02:00
psd=$((psd+1))
dialog --title "Pacman packages" --infobox "Package $psd/$ps\n\nInstalling: $1\n\n> $2 <" 0 0
2018-09-15 23:19:42 +02:00
pacman --noconfirm --needed -S "$1" &>> $logfile
2018-09-15 22:17:30 +02:00
}
install_aur() {
2018-09-15 23:13:20 +02:00
asd=$((asd+1))
2018-09-16 00:46:03 +02:00
echo "Entered installationloop\n\n" >> $logfile
2018-09-15 23:13:20 +02:00
dialog --title "AUR packages" --infobox "AUR Package $asd/$as\n\nInstalling: $1\n\n> $2 <" 0 0
2018-09-15 22:17:30 +02:00
# is package already installed?
grep "^$1$" <<< "$aur_already_installed" && return
2021-12-11 03:45:22 +01:00
sudo -u $uname $aurhelper -S --noconfirm "$1" &>> $logfile
2018-09-15 22:17:30 +02:00
}
install_manual() {
2018-09-15 23:13:20 +02:00
msd=$((msd+1))
dialog --title "Manual packages" --infobox "Manual Package $msd/$ms\n\nInstalling: $1\n\n> $2 <" 0 0
cd "$scriptdir/packages/$1" || exit
2018-12-03 22:08:22 +01:00
make clean && make && make install && make clean &>> $logfile ;
2018-09-15 22:17:30 +02:00
cd "/home/$uname" || exit
}
2021-03-21 17:08:38 +01:00
# TODO: test with makepkg -si
2018-09-15 22:17:30 +02:00
install_aur_helper() {
dialog --title "Installing the AUR Helper" --infobox "Installing dependencies..." 0 0
2018-09-15 23:19:42 +02:00
pacman --noconfirm --needed -S go &>> $logfile
2018-09-15 22:17:30 +02:00
dialog --title "Installing the AUR Helper" --infobox "Cloning repository..." 0 0
2022-04-11 17:49:56 +02:00
cd $(sudo -u $uname mktemp -d)
2018-09-16 00:27:37 +02:00
git clone https://aur.archlinux.org/yay.git . &>> $logfile
2021-03-12 17:43:31 +01:00
sudo -u $uname makepkg --noconfirm -s &>> $logfile
2021-12-11 03:45:22 +01:00
pacman -U --noconfirm *.zst
2018-09-15 22:17:30 +02:00
}
installation_loop() {
echo "Entered installationloop\n\n" >> $logfile
([ -f "$prog" ] && cp "$prog" /tmp/pack.csv) || curl -Ls "$prog" > /tmp/pack.csv
# count packages and sum results for all and curr platform
ps=$(($(grep -e "A,P," /tmp/pack.csv | wc -l) + $(grep -e "$curr_platform,P," /tmp/pack.csv | wc -l)))
as=$(($(grep -e "A,A," /tmp/pack.csv | wc -l) + $(grep -e "$curr_platform,A," /tmp/pack.csv | wc -l)))
ms=$(($(grep -e "A,M," /tmp/pack.csv | wc -l) + $(grep -e "$curr_platform,M," /tmp/pack.csv | wc -l)))
2018-09-15 22:17:30 +02:00
aur_already_installed=$(pacman -Qqm)
#IFS separator
while IFS=, read -r platform prefix program info; do
2018-09-15 23:54:46 +02:00
if [[ ${platform} != "A" ]] && [[ ${platform} != ${curr_platform} ]]; then
2018-09-15 22:17:30 +02:00
continue
fi
case $prefix in
P) install_pacman "$program" "$info" ;;
A) install_aur "$program" "$info" ;;
M) install_manual "$program" "$info" ;;
2018-09-15 22:17:30 +02:00
esac
done < /tmp/pack.csv
2018-09-15 22:17:30 +02:00
}
enable_service() {
for service in "$@"; do
2019-06-10 20:29:42 +02:00
dialog --infobox "Enabling: $service" 0 0
if [[ $@ =~ "--user.*" ]]
then
sudo -u $uname systemctl enable "$service"
sudo -u $uname systemctl start "$service"
else
systemctl enable "$service"
systemctl start "$service"
fi
echo "Services $service started\n\n" >> $logfile
2018-09-15 22:17:30 +02:00
done
}
set_permissions() {
2021-03-12 16:19:36 +01:00
sed -i '/.*#phga/d' /etc/sudoers
echo -e "$@ #phga" >> /etc/sudoers
2018-09-15 22:17:30 +02:00
echo "Permissions set\n\n" >> $logfile
}
download_dotfiles() {
cd /home/$uname
2018-10-22 22:29:17 +02:00
sudo -u $uname mkdir -p .dotfiles && cd .dotfiles
sudo -u $uname git clone $repo .
2018-11-27 22:36:47 +01:00
sudo -u $uname ./scripts/link.sh
2018-09-16 00:52:35 +02:00
echo "Dotfiles downloaded and installed\n\n" >> $logfile
2018-09-15 22:17:30 +02:00
}
no_beep() {
rmmod pcspkr
echo "blacklist pcspkr" > /etc/modprobe.d/nobeep.conf
echo "Beep killed\n\n" >> $logfile
}
ready_steady_go() {
2018-11-24 14:56:23 +01:00
dialog --title "WE ARE DONE" --msgbox "The installation is complete\n\n#fingerscrossed everything worked :D\n\nTODOs\n\n- Move syncthing keys into .config\n" 0 0 ;
2018-09-15 22:17:30 +02:00
}
uncom() {
sed -i '/'"$1"'/s/^#//g' $2
}
com() {
sed -i '/'"$1"'/s/^/#/g' $2
}
add_after() {
# Example:
# add_after "Color" "ILoveCandy" "$HOME/test/pacman.conf"
2021-12-22 17:46:36 +01:00
sed -i '/'"$2"'/d'
sed -i '/'"$1"'/a '"$2"'' $3
}
2018-11-20 22:18:28 +01:00
add_xorg_conf() {
echo "Adding xorg conf\n\n" >> $logfile
2018-12-03 22:08:22 +01:00
cp $scriptdir/etc/X11/xorg.conf.d/* /etc/X11/xorg.conf.d/ &>> $logfile
2018-11-20 22:18:28 +01:00
}
add_udev_rules() {
2018-12-03 22:08:22 +01:00
echo "Adding udev rules from $scriptdir into udev folder\n\n" >> $logfile
cp $scriptdir/etc/udev/rules.d/* /etc/udev/rules.d/ &>> $logfile
}
add_pacman_hooks() {
echo "Adding pacman hooks\n\n" >> $logfile
mkdir -p /etc/pacman.d/hooks >> $logfile
2018-12-03 22:08:22 +01:00
cp $scriptdir/etc/pacman.d/hooks/* /etc/pacman.d/hooks/ &>> $logfile
}
2021-12-18 21:19:51 +01:00
add_xorg_conf() {
echo "Adding systemd configs\n\n" >> $logfile
cp $scriptdir/etc/systemd/network/00-wired.network /etc/systemd/network/00-wired.network &>> $logfile
}
set_pacman_config() {
echo "Setting pacman conf\n\n" >> $logfile
cp $scriptdir/etc/pacman.conf /etc/pacman.conf &>> $logfile
}
2018-11-24 14:18:22 +01:00
set_git_config() {
2022-01-07 03:50:37 +01:00
sudo -u $uname git config --global user.email "phga@posteo.de"
2021-12-10 23:40:29 +01:00
sudo -u $uname git config --global user.name "qhga"
2018-11-24 15:14:40 +01:00
sudo -u $uname git config --global credential.useHttpPath true
2021-12-10 23:40:29 +01:00
# gpg --list-keys --keyid-format=long
sudo -u $uname git config --global user.signingkey 5249548AA705F019
sudo -u $uname git config --global commit.gpgsign true
# sudo -u $uname git config --global credential.helper /usr/bin/pass-git-helper
2018-11-24 14:18:22 +01:00
}
2018-11-11 17:25:22 +01:00
set_system_stuff() {
# locale gen
echo "Enter system stuff\n\n" >> $logfile
dialog --title "Setting up the system" --infobox "Final adjustments" 0 0
2018-12-03 22:08:22 +01:00
localectl --no-convert set-x11-keymap us ,altgr-intl &>> $logfile
setxkbmap us -variant altgr-intl &>> $logfile
add_udev_rules
set_pacman_config
add_pacman_hooks
2018-11-20 22:18:28 +01:00
add_xorg_conf
2018-11-24 14:18:22 +01:00
set_git_config
2018-11-24 14:56:23 +01:00
#link syncthing config and move certs
# sudo -u $uname /home/$uname/.dotfiles/syncthing/link-confs.sh &>> $logfile
echo "Leave system stuff\n\n" >> $logfile
2018-11-11 17:25:22 +01:00
}
add_user_to_groups() {
dialog --infobox "Adding $uname to necessary groups" 0 0
ugroups="audio realtime docker"
usermod -a -G "$ugroups" "$uname"
echo "Added user $uname to groups: $ugroups\n\n" >> $logfile
}
set_root_bashrc() {
2020-04-06 04:27:23 +02:00
echo ". /root/.bashrc" > /root/.bash_profile
cat <<EOF > /root/.bashrc
# Fix for tramp connections
[ $TERM = "dumb" ] && return
set -o vi
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
PROMPT_COMMAND='history -a'
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
alias ls='ls --color=auto --group-directories-first'
alias grep='grep --color=auto'
alias ll='ls -alSFhv'
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
# PS1 config
export PS1="\[$(tput bold)\]\[\033[38;5;1m\]ROOT\[$(tput sgr0)\]@\w\n->\[$(tput sgr0)\]"
EOF
}
2018-09-16 01:23:37 +02:00
# ACTUAL ROUTINE
2018-09-15 22:17:30 +02:00
case $todo in
install)
init
startup_msg
get_credentials
pre_install
create_user
install_aur_helper
installation_loop
download_dotfiles
set_permissions "Defaults timestamp_timeout=45\n%wheel ALL=(ALL) ALL\n%wheel ALL=(ALL) NOPASSWD: /usr/bin/shutdown,/usr/bin/reboot,/usr/bin/systemctl suspend,/usr/bin/wifi-menu,/usr/bin/mount,/usr/bin/umount,/usr/bin/pacman -Syu,/usr/bin/pacman -Ss,/usr/bin/pacman -Syyu,/usr/bin/pacman -Syyu --noconfirm,/usr/bin/loadkeys"
no_beep
set_system_stuff
2021-12-11 03:45:22 +01:00
enable_service "--user syncthing"
2021-03-12 17:43:31 +01:00
if [[ ${curr_platform} == "L" ]]; then
enable_service "netctl-auto@wlp3s0"
2021-12-18 21:19:51 +01:00
else
enable_service "systemd-networkd"
2021-03-12 17:43:31 +01:00
fi
enable_service "systemd-timesyncd" "atd"
set_root_bashrc
add_user_to_groups
ready_steady_go
clear
;;
update)
init
startup_msg
refresh_keyring
pre_update
installation_loop
set_system_stuff
ready_steady_go
clear
;;
services)
init
startup_msg
pre_update
2021-03-12 17:43:31 +01:00
if [[ ${curr_platform} == "L" ]]; then
enable_service "netctl-auto@wlp3s0"
2021-12-18 21:19:51 +01:00
else
enable_service "systemd-networkd"
2021-03-12 17:43:31 +01:00
fi
enable_service "systemd-timesyncd" "atd"
ready_steady_go
clear
;;
esac