# Configure the system
export SOLN_NON_INTERACTIVE="YES"
if [ -z "${SOLINSTALL_IS_RUNNING}" ]; then export SOLINSTALL_IS_RUNNING='1'; fi
/usr/local/SolutionInc/etc/templates/reconfig-all.pl /tmp/soln.conf hy
/bin/rm -f /tmp/soln.conf

# Sometimes reconfig could fail and the root password doesn't get set.
# So we will set it now, just to be sure
. /usr/local/SolutionInc/etc/templates/latest_config_file
/usr/sbin/usermod -p $ROOT_PASSWORD root

chmod 644 /usr/local/SolutionInc/etc/templates/latest_config_file
chgrp nobody /usr/local/SolutionInc/etc/templates/latest_config_file

# Symlink for SolutionIP config
ln -sf /usr/local/SolutionInc/etc/templates/latest_config_file /etc/soln.conf

# Initialize the driver from data_init
/usr/local/SolutionInc/bin/solcl -R -F /usr/local/etc/data_init
rm -f /var/SolutionInc/soln_backup*

touch /var/log/messages
chmod 644 /var/log/messages

chmod 755 /var/log/apache2
touch /var/log/apache2/access_log
touch /var/log/apache2/error_log
chmod 644 /var/log/apache2/access_log
chmod 644 /var/log/apache2/error_log

mkdir /var/lib/nobody
chown nobody /var/lib/nobody

echo -e  "$(expr $(date +%s) / 60 % 5 )-59/5 * * * *\troot\t sleep \$(shuf -i0-59 -n1); /usr/local/SolutionInc/scripts/soln-reporting-edge.pl 2>&1 | logger -t soln-reporting-edge.pl" > /etc/cron.d/soln-reporting-edge

# Set vga GRUB_CMDLINE_LINUX
cat /proc/cmdline | grep -q vga=

if [ $? -eq 0 ]
then
	for i in `cat /proc/cmdline`
	do
		echo $i | grep -q vga=

		if [ $? -eq 0 ]
		then
			sed -i "s/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"$i\"/" /etc/default/grub
		fi
	done
fi

if [ -d /sys/firmware/efi ]
then
	# Install grub2 (EFI)
	mkdir /boot/efi/EFI/debian
	echo 'search --file --set=root /usr/local/SolutionInc/etc/templates/latest_config_file' > /boot/efi/EFI/debian/grub.cfg
	echo 'set prefix=($root)/boot/grub2' >> /boot/efi/EFI/debian/grub.cfg
	echo 'source $prefix/grub.cfg' >> /boot/efi/EFI/debian/grub.cfg

	echo "Running grub2-install --target=x86_64-efi"
	grub2-install --target=x86_64-efi
	grub2-mkconfig -o /boot/grub2/grub.cfg

	echo -ne "LOADER_TYPE='grub2-efi'\nSECURE_BOOT='no'\n" > /etc/sysconfig/bootloader

	echo "Running efibootmgr -c -L 'SolutionIP Flex' -l '\EFI\boot\bootx64.efi' -d ${1} -p 3"
	efibootmgr -c -L 'SolutionIP Flex' -l '\EFI\boot\bootx64.efi' -d ${1} -p 3

else
	# Install grub2 (BIOS)
	echo "Running grub2-install --target=i386-pc $1"
	grub2-install --target=i386-pc $1
	grub2-mkconfig -o /boot/grub2/grub.cfg

	echo -ne "LOADER_TYPE='grub2'\n" > /etc/sysconfig/bootloader
fi
