#!/bin/sh

# PROVIDE: firstboot
# REQUIRE: NETWORKING FILESYSTEMS
# KEYWORD: nojail

. /etc/rc.subr

name="firstboot"
rcvar=
start_cmd="firstboot_start"

firstboot_start()
{
	local lsuffix
	local red
	local yellow
	local receipt

	lsuffix="\e[0m"
	red="\e[1;31;40m"
	yellow="\e[1;33;40m"
	receipt="/etc/fbreceipt"

	if [ ! -f ${receipt} ]; then
		echo -e "\r${red}MidnightBSD FirstBoot Configuration${lsuffix}"
		echo "This utility requires an active Internet connection."

		echo -e "\r${yellow}Set your preferred package mirror? (us,uk,jp)${lsuffix}"
		read mportmirror
		/usr/sbin/mport config set mirror_region ${mportmirror}

		echo -e "\r${yellow}Would you like to report your install via bsdstats? (yes or no)${lsuffix}"
		read installbstat
		if [ ${installbstat} = "yes" ]; then
			if [ ! -f /usr/local/etc/rc.d/bsdstats.sh ]; then
				/sbin/ipfw disable firewall
				/usr/sbin/mport install bsdstats
				/sbin/ipfw enable firewall
			fi
			/usr/sbin/sysrc bsdstats_enable=YES
		fi

		echo -e "\r${yellow}Would you like to report your install via hw-probe? (yes or no)${lsuffix}"
		read installhp
		if [ ${installhp} = "yes" ]; then
			if [ ! -f /usr/local/bin/hw-probe ]; then
				/sbin/ipfw disable firewall
				/usr/sbin/mport install hw-probe
				/sbin/ipfw enable firewall
			fi
			echo "Scanning and uploading... (one-time event)"
			/usr/local/bin/hw-probe -upload -all
		fi

		if [ `uname -m` == 'amd64' ]; then
			echo -e "\r${yellow}Would you like to install Ravenports, an alternative package manager? (yes or no)${lsuffix}"
                	read ravenports
                	if [ ${ravenports} = "yes" ]; then
				/usr/bin/fetch http://www.ravenports.com/repository/ravenports-downloader.sh -o - | /bin/sh
				sysrc local_startup+="/raven/etc/rc.d"

				echo "You can now use Ravenports or mports to install software on this system."
				echo "Consult http://www.ravenports.com/catalog/ for a list of available software for Ravenports."
			fi
		fi

		echo -e "\r${yellow}Do you wish to enable a graphical environment? (yes or no)${lsuffix}"
		read installgui

		if [ ${installgui} = "yes" ]; then
			echo -e "\r${red}Installing Graphical User Interface${lsuffix}"
#			xfce/mlogind
				/sbin/ipfw disable firewall
				/usr/sbin/mport install midnightbsd-desktop
				/sbin/ipfw enable firewall
				/usr/sbin/sysrc mlogind_enable=YES
				/usr/sbin/sysrc dbus_enable=YES

			echo "The following options will help you install GPU drivers. You may need to perform further x.org configuration to make them work properly"

			if [ -f /usr/local/bin/xconfig ]; then
				echo "Attempting xconfig configuration.  You can run the utility again by typing xconfig to change settings."
				/usr/local/bin/xconfig auto
			else
			echo -e "\r${yellow}Do you wish to install Intel or AMD GPU drivers? (yes or no)${lsuffix}"
			read drmkmod
			if [ ${drmkmod} = "yes" ]; then
				/usr/sbin/mport install drm-510-kmod
				echo -e "\r${yellow}Which GPU module do you want to activate? (amd, radeonkms or intel)${lsuffix}"
				read kmsmodule
				if [ ${kmsmodule} = "intel" ]; then
					/usr/sbin/sysrc kld_list="/boot/modules/i915kms.ko"
				elif [ ${kmsmodule} = "amd" ]; then
					/usr/sbin/sysrc kld_list="amdgpu"
				else 
					/usr/sbin/sysrc kld_list="/boot/modules/radeonkms.ko"
				fi
			fi

			echo -e "\r${yellow}Do you wish to install nvidia GPU driver? (no, 340, 390, 470)${lsuffix}"
			read nvidia
			if [ ${nvidia} = "340" ]; then
				/usr/sbin/mport install nvidia-driver-340
				/usr/sbin/sysrc kld_list="nvidia"
			elif [ ${nvidia} = "390" ]; then
				/usr/sbin/mport install nvidia-driver-390
				/usr/sbin/sysrc kld_list="nvidia"
			elif [ $[nvidia} = "470" ]; then
				/usr/sbin/mport install nvidia-driver-470
				/usr/sbin/sysrc kld_list="nvidia"
			fi
			echo "Please restart to enable graphical login."
			echo -e "\r${yellow}Done.${lsuffix}"
			fi
		else
			echo "To run this utility again, rm ${receipt} and restart."
		fi
		touch ${receipt}
	fi
}

load_rc_config $name
run_rc_command "$1"
