#!/bin/sh # My Arch Linux installation script # by David T. Sadler # License: GNU GPLv3 ### OPTIONS AND VARIABLES ### while getopts ":p:h" o; do case "${o}" in h) printf "Optional arguments for custom use:\\n -p: Dependencies and programs csv (local file or url)\\n -h: Show this message\\n" && exit ;; p) packages=${OPTARG} ;; *) printf "Invalid option: -%s\\n" "$OPTARG" && exit ;; esac done [ -z "$packages" ] && packages="https://raw.githubusercontent.com/davidtsadler/gohan/master/packages.csv" ### FUNCTIONS ### install_package() { pacman --noconfirm --needed -S "$1" >/dev/null 2>&1 } error() { clear printf "ERROR:\\n%s\\n" "$1" exit } welcome_msg() { dialog --title "Welcome!" --msgbox "Welcome to my Arch Linux installation script!\\n\\nThis script will automatically install a fully-featured Linux desktop, which I use as my main machine.\\n\\n-David" 10 60 dialog --colors --title "Important Note!" --yes-label "All ready!" --no-label "Return..." --yesno "Be sure the computer you are using has current pacman updates.\\n\\nIf it does not, the installation of some programs might fail." 8 70 } get_username_and_password() { # Prompts user for new username and password. name=$(dialog --inputbox "First, please enter a name for the user account." 10 60 3>&1 1>&2 2>&3 3>&1) || exit while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do 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) done pass1=$(dialog --no-cancel --passwordbox "Enter a password for that user." 10 60 3>&1 1>&2 2>&3 3>&1) pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1) while ! [ "$pass1" = "$pass2" ]; do unset pass2 pass1=$(dialog --no-cancel --passwordbox "Passwords do not match.\\n\\nEnter password again." 10 60 3>&1 1>&2 2>&3 3>&1) pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1) done } user_check() { ! (id -u "$name" >/dev/null) 2>&1 || dialog --colors --title "WARNING!" --yes-label "CONTINUE" --no-label "No wait..." --yesno "The user \`$name\` already exists on this system. GOHAN can install for a user already existing, but it will \\Zboverwrite\\Zn any conflicting settings/dotfiles on the user account.\\n\\nGOHAN will \\Zbnot\\Zn overwrite your user files, documents, videos, etc., so don't worry about that, but only click if you don't mind your settings being overwritten.\\n\\nNote also that GOHAN will change $name's password to the one you just gave." 14 70 } preinstall_msg() { dialog --title "Last chance!" --yes-label "Let's go!" --no-label "No, nevermind!" --yesno "Just press and the system will begin installation!" 13 60 || { clear; exit; } } add_user() { # Adds user `$name` with password $pass1. dialog --infobox "Adding user \"$name\"..." 4 50 useradd -m -s /bin/bash "$name" >/dev/null 2>&1 || mkdir -p /home/"$name" && chown "$name":"$name" /home/"$name" echo "$name:$pass1" | chpasswd unset pass1 pass2 } get_github_ssh_keys() { # Prompts user for private and public ssh keys for connecting to GitHub. touch /tmp/emptyfile dialog --title "SSH" --msgbox "You will be prompted to enter the private SSH key used to connect to GitHub" 10 60 githubPrivateKey=$(dialog --editbox /tmp/emptyfile 60 60 3>&1 1>&2 2>&3 3>&1) || exit dialog --title "SSH" --msgbox "You will know be prompted to enter the public SSH key used to connect to GitHub" 10 60 githubPublicKey=$(dialog --editbox /tmp/emptyfile 10 60 3>&1 1>&2 2>&3 3>&1) rm /tmp/emptyfile } save_github_ssh_keys() { [ ! -d "/home/$name/.ssh" ] && mkdir -p "/home/$name/.ssh/github.com" echo "$githubPrivateKey" > "/home/$name/.ssh/github.com/id_rsa" echo "$githubPublicKey" > "/home/$name/.ssh/github.com/id_rsa.pub" cat << EOF > "/home/$name/.ssh/config" Host github.com IdentityFile /home/${name}/.ssh/github.com/id_rsa EOF chown -R "$name":"$name" "/home/$name/.ssh" chmod 700 "/home/$name/.ssh" chmod 644 "/home/$name/.ssh/config" chmod 700 "/home/$name/.ssh/github.com" chmod 600 "/home/$name/.ssh/github.com/id_rsa" chmod 644 "/home/$name/.ssh/github.com/id_rsa.pub" } maininstall() { dialog --title "GOHAN Installation" --infobox "Installing \`$1\` ($n of $total). $1 $2" 5 70 install_package "$1" } install_packages() { ([ -f "$packages" ] && cp "$packages" /tmp/packages.csv) || curl -Ls "$packages" > /tmp/packages.csv total=$(wc -l < /tmp/packages.csv) while IFS=, read -r program comment; do n=$((n+1)) echo "$comment" | grep "^\".*\"$" >/dev/null 2>&1 && comment="$(echo "$comment" | sed "s/\(^\"\|\"$\)//g")" maininstall "$program" "$comment" done < /tmp/packages.csv } ### THE ACTUAL SCRIPT ### install_package dialog || error "Are you sure you're running this as the root user and have an internet connection?" welcome_msg || error "User exited." get_username_and_password || error "User exited." user_check || error "User exited." preinstall_msg || error "User exited." add_user || error "Error adding username and/or password." get_github_ssh_keys || error "Error getting the GitHub SSH keys." save_github_ssh_keys || error "Error saving the GitHub SSH keys." install_packages clear