#!/bin/bash
# Author: Philip Gaber <phga@posteo.de>
scriptdir = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
# Get params
while getopts "r:p:d:vush" opt;
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 } ; ;
v) logfile = " $scriptdir /install.log " ; ;
u) todo = "update" ; ;
s) todo = "services" ; ;
\? ) echo " - $OPTARG is not valid " >& 2 && exit ; ;
esac
done
# Default settings
device = ${ device : = "480" }
repo = ${ repo : = "https://phga@g.phga.de/phga/dotfiles" }
prog = ${ prog : = " $scriptdir /pack.csv " }
aurhelper = ${ aurhelper : = "yay" }
logfile = ${ logfile : = "/dev/null" }
todo = ${ todo : = "install" }
startup_msg( ) {
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
}
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
}
init( ) {
wait_for_network
pacman -S --noconfirm --needed dialog git make || { echo "Run as root user" ; clear; exit; }
refresh_keyring
}
refresh_keyring( ) {
pacman --noconfirm -Sy archlinux-keyring & >> $logfile
dialog --infobox "Refresh of the Keyring" 0 0
}
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
}
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
useradd -m -g wheel -s /bin/bash " $uname " & >> $logfile || usermod -a -G wheel " $uname " && mkdir -p /home/" $uname " && chown " $uname " :wheel /home/" $uname "
echo " $uname : $passw " | chpasswd
unset passw passr ;
echo "User successfully created\n\n" >> $logfile
}
install_pacman( ) {
psd = $(( psd+1))
dialog --title "Pacman packages" --infobox " Package $psd / $ps \n\nInstalling: $1 \n\n> $2 < " 0 0
pacman --noconfirm --needed -S " $1 " & >> $logfile
}
install_aur( ) {
asd = $(( asd+1))
echo "Entered installationloop\n\n" >> $logfile
dialog --title "AUR packages" --infobox " AUR Package $asd / $as \n\nInstalling: $1 \n\n> $2 < " 0 0
# is package already installed?
grep " ^ $1 $" <<< " $aur_already_installed " && return
sudo -u $uname $aurhelper -S --noconfirm " $1 " & >> $logfile
}
install_manual( ) {
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
make clean && make && make install && make clean & >> $logfile ;
cd " /home/ $uname " || exit
}
# TODO: test with makepkg -si
install_aur_helper( ) {
dialog --title "Installing the AUR Helper" --infobox "Installing dependencies..." 0 0
pacman --noconfirm --needed -S go & >> $logfile
dialog --title "Installing the AUR Helper" --infobox "Cloning repository..." 0 0
cd $( sudo -u $uname mktemp -d)
git clone https://aur.archlinux.org/yay.git . & >> $logfile
sudo -u $uname makepkg --noconfirm -s & >> $logfile
pacman -U --noconfirm *.zst
}
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) ))
aur_already_installed = $( pacman -Qqm)
#IFS separator
while IFS = , read -r platform prefix program info; do
if [ [ ${ platform } != "A" ] ] && [ [ ${ platform } != ${ curr_platform } ] ] ; then
continue
fi
case $prefix in
P) install_pacman " $program " " $info " ; ;
A) install_aur " $program " " $info " ; ;
M) install_manual " $program " " $info " ; ;
esac
done < /tmp/pack.csv
}
enable_service( ) {
for service in " $@ " ; do
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
done
}
set_permissions( ) {
sed -i '/.*#phga/d' /etc/sudoers
echo -e " $@ #phga " >> /etc/sudoers
echo "Permissions set\n\n" >> $logfile
}
download_dotfiles( ) {
cd /home/$uname
sudo -u $uname mkdir -p .dotfiles && cd .dotfiles
sudo -u $uname git clone $repo .
sudo -u $uname ./scripts/link.sh
echo "Dotfiles downloaded and installed\n\n" >> $logfile
}
no_beep( ) {
rmmod pcspkr
echo "blacklist pcspkr" > /etc/modprobe.d/nobeep.conf
echo "Beep killed\n\n" >> $logfile
}
ready_steady_go( ) {
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 ;
}
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"
sed -i '/' " $2 " '/d'
sed -i '/' " $1 " '/a ' " $2 " '' $3
}
add_xorg_conf( ) {
echo "Adding xorg conf\n\n" >> $logfile
cp $scriptdir /etc/X11/xorg.conf.d/* /etc/X11/xorg.conf.d/ & >> $logfile
}
add_udev_rules( ) {
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
cp $scriptdir /etc/pacman.d/hooks/* /etc/pacman.d/hooks/ & >> $logfile
}
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
}
set_git_config( ) {
sudo -u $uname git config --global user.email "phga@posteo.de"
sudo -u $uname git config --global user.name "qhga"
sudo -u $uname git config --global credential.useHttpPath true
# 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
}
set_system_stuff( ) {
# locale gen
echo "Enter system stuff\n\n" >> $logfile
dialog --title "Setting up the system" --infobox "Final adjustments" 0 0
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
add_xorg_conf
set_git_config
#link syncthing config and move certs
# sudo -u $uname /home/$uname/.dotfiles/syncthing/link-confs.sh &>> $logfile
echo "Leave system stuff\n\n" >> $logfile
}
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( ) {
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
}
# ACTUAL ROUTINE
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
enable_service "--user syncthing"
if [ [ ${ curr_platform } = = "L" ] ] ; then
enable_service "netctl-auto@wlp3s0"
else
enable_service "systemd-networkd"
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
if [ [ ${ curr_platform } = = "L" ] ] ; then
enable_service "netctl-auto@wlp3s0"
else
enable_service "systemd-networkd"
fi
enable_service "systemd-timesyncd" "atd"
ready_steady_go
clear
; ;
esac