larbs

Luke's Auto-Rice Bootstrapping Scripts: Auto-Setup & Configure Arch Linux
Log | Files | Refs | README | LICENSE

larbs.sh (10975B)


      1 #!/bin/sh
      2 # Luke's Auto Rice Boostrapping Script (LARBS)
      3 # by Luke Smith <luke@lukesmith.xyz>
      4 # License: GNU GPLv3
      5 
      6 ### OPTIONS AND VARIABLES ###
      7 
      8 while getopts ":a:r:p:h" o; do case "${o}" in
      9 	h) printf "Optional arguments for custom use:\\n  -r: Dotfiles repository (local file or url)\\n  -p: Dependencies and programs csv (local file or url)\\n  -a: AUR helper (must have pacman-like syntax)\\n  -h: Show this message\\n" && exit ;;
     10 	r) dotfilesrepo=${OPTARG} && git ls-remote "$dotfilesrepo" || exit ;;
     11 	p) progsfile=${OPTARG} ;;
     12 	a) aurhelper=${OPTARG} ;;
     13 	*) printf "Invalid option: -%s\\n" "$OPTARG" && exit ;;
     14 esac done
     15 
     16 # DEFAULTS:
     17 [ -z "$dotfilesrepo" ] && dotfilesrepo="https://github.com/lukesmithxyz/voidrice.git"
     18 [ -z "$progsfile" ] && progsfile="https://raw.githubusercontent.com/LukeSmithxyz/LARBS/master/progs.csv"
     19 [ -z "$aurhelper" ] && aurhelper="yay"
     20 
     21 ### FUNCTIONS ###
     22 
     23 error() { clear; printf "ERROR:\\n%s\\n" "$1"; exit;}
     24 
     25 welcomemsg() { \
     26 	dialog --title "Welcome!" --msgbox "Welcome to Luke's Auto-Rice Bootstrapping Script!\\n\\nThis script will automatically install a fully-featured i3wm Arch Linux desktop, which I use as my main machine.\\n\\n-Luke" 10 60
     27 	}
     28 
     29 getuserandpass() { \
     30 	# Prompts user for new username an password.
     31 	name=$(dialog --inputbox "First, please enter a name for the user account." 10 60 3>&1 1>&2 2>&3 3>&1) || exit
     32 	while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do
     33 		name=$(dialog --no-cancel --inputbox "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>&1 1>&2 2>&3 3>&1)
     34 	done
     35 	pass1=$(dialog --no-cancel --passwordbox "Enter a password for that user." 10 60 3>&1 1>&2 2>&3 3>&1)
     36 	pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1)
     37 	while ! [ "$pass1" = "$pass2" ]; do
     38 		unset pass2
     39 		pass1=$(dialog --no-cancel --passwordbox "Passwords do not match.\\n\\nEnter password again." 10 60 3>&1 1>&2 2>&3 3>&1)
     40 		pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1)
     41 	done ;}
     42 
     43 usercheck() { \
     44 	! (id -u "$name" >/dev/null) 2>&1 ||
     45 	dialog --colors --title "WARNING!" --yes-label "CONTINUE" --no-label "No wait..." --yesno "The user \`$name\` already exists on this system. LARBS can install for a user already existing, but it will \\Zboverwrite\\Zn any conflicting settings/dotfiles on the user account.\\n\\nLARBS will \\Zbnot\\Zn overwrite your user files, documents, videos, etc., so don't worry about that, but only click <CONTINUE> if you don't mind your settings being overwritten.\\n\\nNote also that LARBS will change $name's password to the one you just gave." 14 70
     46 	}
     47 
     48 preinstallmsg() { \
     49 	dialog --title "Let's get this party started!" --yes-label "Let's go!" --no-label "No, nevermind!" --yesno "The rest of the installation will now be totally automated, so you can sit back and relax.\\n\\nIt will take some time, but when done, you can relax even more with your complete system.\\n\\nNow just press <Let's go!> and the system will begin installation!" 13 60 || { clear; exit; }
     50 	}
     51 
     52 adduserandpass() { \
     53 	# Adds user `$name` with password $pass1.
     54 	dialog --infobox "Adding user \"$name\"..." 4 50
     55 	useradd -m -g wheel -s /bin/bash "$name" >/dev/null 2>&1 ||
     56 	usermod -a -G wheel "$name" && mkdir -p /home/"$name" && chown "$name":wheel /home/"$name"
     57 	echo "$name:$pass1" | chpasswd
     58 	unset pass1 pass2 ;}
     59 
     60 refreshkeys() { \
     61 	dialog --infobox "Refreshing Arch Keyring..." 4 40
     62 	pacman --noconfirm -Sy archlinux-keyring >/dev/null 2>&1
     63 	}
     64 
     65 newperms() { # Set special sudoers settings for install (or after).
     66 	sed -i "/#LARBS/d" /etc/sudoers
     67 	echo "$* #LARBS" >> /etc/sudoers ;}
     68 
     69 manualinstall() { # Installs $1 manually if not installed. Used only for AUR helper here.
     70 	[ -f "/usr/bin/$1" ] || (
     71 	dialog --infobox "Installing \"$1\", an AUR helper..." 4 50
     72 	cd /tmp || exit
     73 	rm -rf /tmp/"$1"*
     74 	curl -sO https://aur.archlinux.org/cgit/aur.git/snapshot/"$1".tar.gz &&
     75 	sudo -u "$name" tar -xvf "$1".tar.gz >/dev/null 2>&1 &&
     76 	cd "$1" &&
     77 	sudo -u "$name" makepkg --noconfirm -si >/dev/null 2>&1
     78 	cd /tmp || return) ;}
     79 
     80 maininstall() { # Installs all needed programs from main repo.
     81 	dialog --title "LARBS Installation" --infobox "Installing \`$1\` ($n of $total). $1 $2" 5 70
     82 	pacman --noconfirm --needed -S "$1" >/dev/null 2>&1
     83 	}
     84 
     85 gitmakeinstall() {
     86 	dir=$(mktemp -d)
     87 	dialog --title "LARBS Installation" --infobox "Installing \`$(basename "$1")\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2" 5 70
     88 	git clone --depth 1 "$1" "$dir" >/dev/null 2>&1
     89 	cd "$dir" || exit
     90 	make >/dev/null 2>&1
     91 	make install >/dev/null 2>&1
     92 	cd /tmp || return ;}
     93 
     94 aurinstall() { \
     95 	dialog --title "LARBS Installation" --infobox "Installing \`$1\` ($n of $total) from the AUR. $1 $2" 5 70
     96 	echo "$aurinstalled" | grep "^$1$" >/dev/null 2>&1 && return
     97 	sudo -u "$name" $aurhelper -S --noconfirm "$1" >/dev/null 2>&1
     98 	}
     99 
    100 pipinstall() { \
    101 	dialog --title "LARBS Installation" --infobox "Installing the Python package \`$1\` ($n of $total). $1 $2" 5 70
    102 	command -v pip || pacman -S --noconfirm --needed python-pip >/dev/null 2>&1
    103 	yes | pip install "$1"
    104 	}
    105 
    106 installationloop() { \
    107 	([ -f "$progsfile" ] && cp "$progsfile" /tmp/progs.csv) || curl -Ls "$progsfile" | sed '/^#/d' > /tmp/progs.csv
    108 	total=$(wc -l < /tmp/progs.csv)
    109 	aurinstalled=$(pacman -Qm | awk '{print $1}')
    110 	while IFS=, read -r tag program comment; do
    111 		n=$((n+1))
    112 		echo "$comment" | grep "^\".*\"$" >/dev/null 2>&1 && comment="$(echo "$comment" | sed "s/\(^\"\|\"$\)//g")"
    113 		case "$tag" in
    114 			"") maininstall "$program" "$comment" ;;
    115 			"A") aurinstall "$program" "$comment" ;;
    116 			"G") gitmakeinstall "$program" "$comment" ;;
    117 			"P") pipinstall "$program" "$comment" ;;
    118 		esac
    119 	done < /tmp/progs.csv ;}
    120 
    121 putgitrepo() { # Downlods a gitrepo $1 and places the files in $2 only overwriting conflicts
    122 	dialog --infobox "Downloading and installing config files..." 4 60
    123 	dir=$(mktemp -d)
    124 	[ ! -d "$2" ] && mkdir -p "$2" && chown -R "$name:wheel" "$2"
    125 	chown -R "$name:wheel" "$dir"
    126 	sudo -u "$name" git clone --depth 1 "$1" "$dir/gitrepo" >/dev/null 2>&1 &&
    127 	sudo -u "$name" cp -rfT "$dir/gitrepo" "$2"
    128 	}
    129 
    130 serviceinit() { for service in "$@"; do
    131 	dialog --infobox "Enabling \"$service\"..." 4 40
    132 	systemctl enable "$service"
    133 	systemctl start "$service"
    134 	done ;}
    135 
    136 systembeepoff() { dialog --infobox "Getting rid of that retarded error beep sound..." 10 50
    137 	rmmod pcspkr
    138 	echo "blacklist pcspkr" > /etc/modprobe.d/nobeep.conf ;}
    139 
    140 resetpulse() { dialog --infobox "Reseting Pulseaudio..." 4 50
    141 	killall pulseaudio
    142 	sudo -n "$name" pulseaudio --start ;}
    143 
    144 finalize(){ \
    145 	dialog --infobox "Preparing welcome message..." 4 50
    146 	echo "exec_always --no-startup-id notify-send -i ~/.scripts/larbs.png 'Welcome to LARBS:' 'Press Super+F1 for the manual.' -t 10000"  >> "/home/$name/.config/i3/config"
    147 	dialog --title "All done!" --msgbox "Congrats! Provided there were no hidden errors, the script completed successfully and all the programs and configuration files should be in place.\\n\\nTo run the new graphical environment, log out and log back in as your new user, then run the command \"startx\" to start the graphical environment (it will start automatically in tty1).\\n\\n.t Luke" 12 80
    148 	}
    149 
    150 ### THE ACTUAL SCRIPT ###
    151 
    152 ### This is how everything happens in an intuitive format and order.
    153 
    154 # Check if user is root on Arch distro. Install dialog.
    155 pacman -Syu --noconfirm --needed dialog ||  error "Are you sure you're running this as the root user? Are you sure you're using an Arch-based distro? ;-) Are you sure you have an internet connection? Are you sure your Arch keyring is updated?"
    156 
    157 # Welcome user.
    158 welcomemsg || error "User exited."
    159 
    160 # Get and verify username and password.
    161 getuserandpass || error "User exited."
    162 
    163 # Give warning if user already exists.
    164 usercheck || error "User exited."
    165 
    166 # Last chance for user to back out before install.
    167 preinstallmsg || error "User exited."
    168 
    169 ### The rest of the script requires no user input.
    170 
    171 adduserandpass || error "Error adding username and/or password."
    172 
    173 # Refresh Arch keyrings.
    174 refreshkeys || error "Error automatically refreshing Arch keyring. Consider doing so manually."
    175 
    176 dialog --title "LARBS Installation" --infobox "Installing \`basedevel\` and \`git\` for installing other software." 5 70
    177 pacman --noconfirm --needed -S base-devel git >/dev/null 2>&1
    178 [ -f /etc/sudoers.pacnew ] && cp /etc/sudoers.pacnew /etc/sudoers # Just in case
    179 
    180 # Allow user to run sudo without password. Since AUR programs must be installed
    181 # in a fakeroot environment, this is required for all builds with AUR.
    182 newperms "%wheel ALL=(ALL) NOPASSWD: ALL"
    183 
    184 # Make pacman and yay colorful and adds eye candy on the progress bar because why not.
    185 grep "^Color" /etc/pacman.conf >/dev/null || sed -i "s/^#Color/Color/" /etc/pacman.conf
    186 grep "ILoveCandy" /etc/pacman.conf >/dev/null || sed -i "/#VerbosePkgLists/a ILoveCandy" /etc/pacman.conf
    187 
    188 # Use all cores for compilation.
    189 sed -i "s/-j2/-j$(nproc)/;s/^#MAKEFLAGS/MAKEFLAGS/" /etc/makepkg.conf
    190 
    191 manualinstall $aurhelper || error "Failed to install AUR helper."
    192 
    193 # The command that does all the installing. Reads the progs.csv file and
    194 # installs each needed program the way required. Be sure to run this only after
    195 # the user has been created and has priviledges to run sudo without a password
    196 # and all build dependencies are installed.
    197 installationloop
    198 
    199 # Install the dotfiles in the user's home directory
    200 putgitrepo "$dotfilesrepo" "/home/$name"
    201 rm -f "/home/$name/README.md" "/home/$name/LICENSE"
    202 
    203 # Install the LARBS Firefox profile in ~/.mozilla/firefox/
    204 putgitrepo "https://github.com/LukeSmithxyz/mozillarbs.git" "/home/$name/.mozilla/firefox"
    205 
    206 # Pulseaudio, if/when initially installed, often needs a restart to work immediately.
    207 [ -f /usr/bin/pulseaudio ] && resetpulse
    208 
    209 # Install vim `plugged` plugins.
    210 sudo -u "$name" mkdir -p "/home/$name/.config/nvim/autoload"
    211 curl "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > "/home/$name/.config/nvim/autoload/plug.vim"
    212 dialog --infobox "Installing (neo)vim plugins..." 4 50
    213 (sleep 30 && killall nvim) &
    214 sudo -u "$name" nvim -E -c "PlugUpdate|visual|q|q" >/dev/null 2>&1
    215 
    216 # Enable services here.
    217 serviceinit NetworkManager cronie
    218 
    219 # Most important command! Get rid of the beep!
    220 systembeepoff
    221 
    222 # This line, overwriting the `newperms` command above will allow the user to run
    223 # serveral important commands, `shutdown`, `reboot`, updating, etc. without a password.
    224 newperms "%wheel ALL=(ALL) ALL #LARBS
    225 %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 -Syyu,/usr/bin/packer -Syu,/usr/bin/packer -Syyu,/usr/bin/systemctl restart NetworkManager,/usr/bin/rc-service NetworkManager restart,/usr/bin/pacman -Syyu --noconfirm,/usr/bin/loadkeys,/usr/bin/yay,/usr/bin/pacman -Syyuw --noconfirm"
    226 
    227 # Last message! Install complete!
    228 finalize
    229 clear