#!/usr/bin/bash # vim:ft=sh:fdm=marker:fmr={,} # archlabs installer library script file # this file is not meant to be run directly # sourcing this file in a non bash shell is not advised chroot_cmd() { arch-chroot $MNT /bin/bash -c "$1" } identify_system() { if grep -qi 'apple' /sys/class/dmi/id/sys_vendor; then modprobe -r -q efivars else modprobe -q efivarfs fi if [[ -d /sys/firmware/efi ]]; then SYS="UEFI" # [[ $(mount) =~ $EFI ]] && mount -t efivarfs efivarfs $EFI >/dev/null 2>&1 local bitness bitness=$(cat /sys/firmware/efi/fw_platform_size) if [[ $bitness != "" && $bitness == 64 ]]; then IS_64BIT=true else IS_64BIT=false fi else SYS="BIOS" fi if [[ $IS_64BIT == true ]]; then readonly BT="$DIST Installer - $SYS (i686) - Version $VER" else readonly BT="$DIST Installer - $SYS (x86_64) - Version $VER" fi return 0 } check_requirements() { local err=0 if [[ $(whoami) != "root" ]]; then infobox "$_ErrTitle" "$_NotRoot\n$_Exit" err=1 elif ! grep -qw 'lm' /proc/cpuinfo; then infobox "$_ErrTitle" "$_Not64Bit\n$_Exit" err=1 elif ! (ping -c 1 archlinux.org || ping -c 1 archlabslinux.com || ping -c 1 google.com || ping -c 1 bitbucket.org || ping -c 1 github.com || ping -c 1 sourceforge.net) >/dev/null 2>&1; then ([[ $(systemctl is-active NetworkManager) == "active" ]] && hash nmtui >/dev/null 2>&1) && { tput civis; nmtui; } if ! (ping -c 1 archlinux.org || ping -c 1 archlabslinux.com || ping -c 1 google.com || ping -c 1 bitbucket.org || ping -c 1 github.com || ping -c 1 sourceforge.net) >/dev/null 2>&1; then infobox "$_ErrTitle" "$_NoNetwork\n$_Exit" err=1 fi fi [[ $err -eq 1 ]] && die 1 || return 0 } check_for_errors() { # return if the last process exited normally (( $? == 0 )) && return 0 local msg="\nThe command exited abnormally: $1" # get error message from logfile and strip any non-printable characters, # escape sequences, and other messy text local err err="$(sed 's/[^[:print:]]//g; s/\[[0-9\;:]*\?m//g; s/==> //g; s/] ERROR:/]\nERROR:/g' "$ERR")" [[ $err != "" ]] && msgbox "$_ErrTitle" "$msg\n\nWith the following error message:\n\n$err" msg="$([[ $err == "" ]] && echo -n "$msg")\n$_ErrChoice" if [[ -e /tmp/debug-log && $TERM == 'linux' ]]; then msg="$([[ $err == "" ]] && echo -n "$msg")\n$_ErrChoiceConsole" yesno "$_ErrTitle" "$msg" "Exit & Open Log" "Ignore & Continue" && { less /tmp/debug-log; die 0; } else yesno "$_ErrTitle" "$msg" "Exit & Shutdown" "Ignore & Continue" && die 'shutdown -h now' fi return 0 } check_install_ready() { if ! [[ $(lsblk -o MOUNTPOINT) =~ $MNT ]]; then msgbox "$_ErrTitle" "$_ErrNoMount" MENU_HIGHLIGHT=4 return 1 elif [[ $CONFIG_DONE != true ]]; then msgbox "$_ErrTitle" "$_ErrNoConfig" MENU_HIGHLIGHT=5 return 1 fi return 0 } unmount_partitions() { swapoff -a for i in $(mount | awk "/${MNT//\//\\/}/"' {print $3}' | sort -r); do umount -r "$i" >/dev/null 2>&1 done } getinput() { local answer answer="$(dialog --cr-wrap --max-input 63 --stdout --no-cancel \ --backtitle "$BT" --title " $1 " --inputbox "$2" 0 0 "$3")" [[ $? != 0 || $answer == "" ]] && return 1 echo "$answer" } msgbox() { tput civis dialog --cr-wrap --backtitle "$BT" --title " $1 " --msgbox "$2\n" 0 0 } infobox() { local time="$3" tput civis dialog --cr-wrap --backtitle "$BT" --title " $1 " --infobox "$2\n" 0 0 sleep ${time:-2} } yesno() { tput civis if [[ $# -eq 5 && $5 == "no" ]]; then # option for default no using custom labels dialog --cr-wrap --backtitle "$BT" --defaultno --title " $1 " \ --yes-label "$3" --no-label "$4" --yesno "$2\n" 0 0 elif [[ $# -eq 4 ]]; then # option for custom labels with standard default yes dialog --cr-wrap --backtitle "$BT" --title " $1 " --yes-label "$3" \ --no-label "$4" --yesno "$2\n" 0 0 else # basic yes no without custom labels and default yes dialog --cr-wrap --backtitle "$BT" --title " $1 " --yesno "$2\n" 0 0 fi } wrap_up() { yesno "$_CloseInst" "$1" "$2" "$3" || return 0 [[ $4 == 'reboot' ]] && die 'reboot' || die 0 } die() { tput cnorm unmount_partitions pgrep -f "$TERM_CMD -e tail" && pkill -f "$TERM_CMD -e tail" [[ $1 =~ [0-9] ]] && exit $1 || $1 } sigint() { echo -e "\n** CTRL-C caught" die 1 } oneshot() { [[ -e /tmp/.ai_$1 || ! $(type $1) ]] && return 0 $1 || return 1 touch "/tmp/.ai_$1" return 0 } debug() { set -x exec 3>| /tmp/debug-log BASH_XTRACEFD=3 if [[ $DISPLAY && $TERM != 'linux' ]]; then if [[ $TERM_CMD == 'st' ]]; then $TERM_CMD -e tail -f /tmp/debug-log & else $TERM_CMD -e "tail -f /tmp/debug-log" & fi fi }