#!/bin/sh
#
# Zen Installer Network Configuration and Startup Script
#
# Written by Jody James for the Revenge(now Zen) Installer
#
# This script will greet the user, ensure an internet connection
# and download the latest version of the Zen Installer from 
# Github. 
#
# This program is free software, provided under the GNU General Public License
# as published by the Free Software Foundation. So feel free to copy, distribute,
# or modify it as you wish.
#
# Setting title variable
title="Zen Installer Network Tool"

# Greeting user and advising user to connect if not already
greeting () { 
zenity --question --width=450 --height=500 --title="$title" --text "Welcome to the Zen Installer\nPlease ensure that you have an active internet connection.\nIf you need to connect via wifi, you may click the network icon in the above panel.\nIf you are using a wired connection, Network Manager should automatically detect it and connect.\nWhen you have connected to the internet, click the 'yes' button to proceed.\nIf you would like to exit the installer you may click 'no'.\nTo open a terminal and use this as a rescue cd, hit ctrl+alt+t."

if [ "$?" = "1" ]
	then exit
fi
}

# function to download installer and make sure it is executable
download_installer() {
  #git clone --depth 5 https://github.com/spookykidmm/zen_installer
  #chmod +x zen_installer/zif
  curl https://raw.githubusercontent.com/spookykidmm/zen_installer/master/zif -O zif
  chmod +x zif
}

# function to run installer, and provide choices to user after installer closes
run_installer() {
 sudo ./zif
  ans=$(zenity --list --title="$title" --radiolist --text "What would you like to do now?"  --column "Select" --column "Option" FALSE Restart FALSE Close FALSE "Chroot into new system" FALSE "Open pacman.conf")
	if [ "$ans" = "Restart" ]
	then reboot
	elif [ "$ans" = "Chroot into new system" ]
	then sudo mount $(cat root_part.txt) /mnt
     lxterminal -e sudo arch-chroot /mnt
     umount -R /mnt
	elif [ "$ans" = "Open pacman.conf" ]
	then sudo mount $(cat root_part.txt) /mnt
	lxterminal -e sudo nano /mnt/etc/pacman.conf     
	else exit
	fi
}

# execution
greeting
# function to check for an active internet connection
  if [[ ! $(ping -c 1 google.com) ]]; then
     zenity --info --title="$title" --text "The internet connection was not detected, please try again."
     greeting
  fi
download_installer
run_installer

