#!/bin/sh # GOHAN # # My Arch Linux installation script # by David T. Sadler # License: GNU GPLv3 ### OPTIONS AND VARIABLES ### ### FUNCTIONS ### install_from_pacman() { pacman --noconfirm --needed -S "$1" >/dev/null 2>&1 } install_from_repo() { progname="$(basename "$1" .git)" dir="$3/$progname" [ -d "$dir" ] || git clone "$1" "$dir" >/dev/null 2>&1 } # Install packages required by GOHAN. install_dependancies() { echo "Installing dependancies ..." install_from_pacman base-devel install_from_pacman git install_from_pacman go } add_user() { get_username_and_password if [ -d "/home/$name" ]; then echo "Skipping adding of user as $name already exists." else echo "Adding user $name ..." useradd -m -s /bin/zsh "$name" >/dev/null 2>&1 mkdir -p /home/"$name" chown "$name":"$name" /home/"$name" usermod -aG wheel "$name" echo "$name:$pass1" | chpasswd fi unset pass1 pass2 } read_password() { PASSWORD="$( # always read from the tty even when redirected: exec < /dev/tty || exit # || exit only needed for bash # save current tty settings: tty_settings=$(stty -g) || exit # schedule restore of the settings on exit of that subshell # or on receiving SIGINT or SIGTERM: trap 'stty "$tty_settings"' EXIT INT TERM # disable terminal local echo stty -echo || exit # prompt on tty printf "$1" > /dev/tty # read password as one line, record exit status IFS= read -r password; ret=$? # display a newline to visually acknowledge the entered password echo > /dev/tty # return the password for $REPLY printf '%s\n' "$password" exit "$ret" )" } get_username_and_password() { printf "Enter a name for the user account: " read name while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do unset name printf "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _.\n\n" printf "Enter a name for the user account: " read name done read_password "Enter a password for that user: " pass1=$PASSWORD read_password "Retype password: " pass2=$PASSWORD while ! [ "$pass1" = "$pass2" ]; do unset pass2 echo "Passwords do not match." read_password "Enter a password again: " pass1=$PASSWORD read_password "Retype password: " pass2=$PASSWORD done unset PASSWORD } configure_sudo() { install_from_pacman sudo [ -f "/etc/sudoers.d/$name" ] || echo "$name ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/$name" } # Sets up the home directory for the user account. setup_user_home_directory() { echo "Setting up user home directory ..." clean_user_home_directory } # Remove un-needed files and directories from the home directory. clean_user_home_directory() { [ -f "/home/$name/.bash_history" ] && rm /home/$name/.bash_history [ -f "/home/$name/.bash_logout" ] && rm /home/$name/.bash_logout [ -f "/home/$name/.bash_profile" ] && rm /home/$name/.bash_profile [ -f "/home/$name/.bashrc" ] && rm /home/$name/.bashrc } install_yay() { [ -f "/usr/bin/yay" ] || ( echo "Installing yay ..." # Use all cores for compilation. sed -i "s/-j2/-j$(nproc)/;s/^#MAKEFLAGS/MAKEFLAGS/" /etc/makepkg.conf cd /tmp rm -rf /tmp/yay git clone https://aur.archlinux.org/yay.git chown -R "$name":"$name" yay cd yay sudo -u "$name" makepkg --noconfirm -si >/dev/null 2>&1 cd /tmp rm -rf /tmp/yay ); } install_packages() { curl -Ls https://git.davidtsadler.com/gohan/plain/packages.csv?h=version-1.0.0 > /tmp/packages.csv total=$(wc -l < /tmp/packages.csv) while IFS=, read -r tag program comment; do n=$((n+1)) echo "$comment" | grep "^\".*\"$" >/dev/null 2>&1 && comment="$(echo "$comment" | sed "s/\(^\"\|\"$\)//g")" case "$tag" in "A") install_from_aur "$program" "$comment" ;; "G") install_repo "$program" "$comment" "/usr/local/src" ;; "#Tag") ;; *) install_package "$program" "$comment" ;; esac done < /tmp/packages.csv rm /tmp/packages.csv } install_package() { printf "Installing \`$1\` ($n of $total). $1 $2\n" install_from_pacman "$1" } install_from_aur() { printf "Installing \`$1\` ($n of $total) from the AUR. $1 $2\n" sudo -u "$name" yay -S --noconfirm "$1" >/dev/null 2>&1 } install_repo() { progname="$(basename "$1" .git)" dir="$3/$progname" printf "Installing \`$progname\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2\n" install_from_repo $1 $2 $3 [ -d "$dir" ] || ( cd "$dir" make clean install >/dev/null 2>&1 ) } ### THE ACTUAL SCRIPT ### install_dependancies add_user configure_sudo setup_user_home_directory install_yay install_packages