#!/usr/bin/env bash

#-----------------------------------
# Name:     check
# Version:  1.0.0
# Author:   Solomon Laing
# Description:
# Validates that the system is ready
# for setup.
#-----------------------------------

# check we aren't root
if [ $(whoami) == "root" ]; then
    echo "These scripts should not be run as root, please log into a user account."
    exit 1
fi

# check sudo is installed and user is in sudo or wheel group
# if not, direct user how to resolve
if ! command -v sudo &>/dev/null; then
    echo "sudo must be intsalled!"
    echo "
To install sudo:
su -
<enter root password>
pacman -Sy sudo
exit
    "
    exit 1
fi

groups=$(whoami | groups $1)

if [[ "wheel" =~ "$groups" ]] || [[ "sudo" =~ "$groups" ]]; then
    echo "User must be in wheel or sudo group and the chosen group must be set in sudoers file /etc/sudoers."
    echo "I.e.: append '%wheel ALL=(ALL) ALL' to the file."
    exit 1
fi

# check for internet connection
connectivity=$(
    ping -c 1 -q google.com >&/dev/null
    echo $?
)

if [ $connectivity -ne 0 ]; then
    echo "You must be connected to the internet to run this collection of scripts. If you cloned these scripts in arch-chroot you may have problems."
    echo "Please set up an active internet conncetion and try again."
    exit 1
fi
