#!/bin/bash
#set -e
##################################################################################################################
# Author    : Erik Dubois
# Website   : https://www.erikdubois.be
# Website   : https://www.alci.online
# Website   : https://www.arcolinux.info
# Website   : https://www.arcolinux.com
# Website   : https://www.arcolinuxd.com
# Website   : https://www.arcolinuxb.com
# Website   : https://www.arcolinuxiso.com
# Website   : https://www.arcolinuxforum.com
##################################################################################################################
#
#   DO NOT JUST RUN THIS. EXAMINE AND JUDGE. RUN AT YOUR OWN RISK.
#
##################################################################################################################
#tput setaf 0 = black 
#tput setaf 1 = red 
#tput setaf 2 = green
#tput setaf 3 = yellow 
#tput setaf 4 = dark blue 
#tput setaf 5 = purple
#tput setaf 6 = cyan 
#tput setaf 7 = gray 
#tput setaf 8 = light blue
##################################################################################################################

echo "###############################################################################"
echo "Getting latest /etc/pacman.conf"
echo "###############################################################################"

Online=0
file_boolean=0

function check_connectivity() {

    local test_ip
    local test_count

    test_ip="8.8.8.8"
    test_count=1

    if ping -c ${test_count} ${test_ip} > /dev/null; then
       echo "You have internet connectivity"
       Online=1
    else
       echo "You do not have connectivity"
       Online=0
    fi
 }

function check_file() {

	file="/usr/local/share/arcolinux/pacman.conf"
	if [[ -f $file ]];then
    	echo $file " exists"
    	file_boolean=1
	else
    	echo $file " doesn't exist"
    	file_boolean=0
	fi
}

check_connectivity
check_file


# pacman config + online
if [ $Online -eq 1 ] ; then
	echo "getting latest /etc/pacman.conf from github"
	sudo wget https://raw.githubusercontent.com/arcolinux/arcolinuxl-iso/master/archiso/airootfs/etc/pacman.conf -O $workdir/etc/pacman.conf
fi

# pacman config + not online
if [ $Online -eq 0 ] && [ $file_boolean -eq 1 ] ; then
	sudo cp /usr/local/share/arcolinux/pacman.conf /etc/pacman.conf
	echo "/etc/pacman.conf saved from local system"
fi

#putting old pacman config back
if [ $Online -eq 0 ]  && [ $file_boolean -eq 0 ] ; then
	echo "Run this script once you are back online"
fi

echo "###############################################################################"
echo "###                               DONE                                     ####"
echo "###############################################################################"
