dotfiles

beau's configuration files
git clone https://git.beauhilton.com/dotfiles.git
Log | Files | Refs | README

blarbs.sh (10713B)


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