#!/bin/sh

## live-config(7) - System Configuration Components
## Copyright (C) 2014 Rohan Garg <rohan@kde.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

Cmdline ()
{
	# Reading kernel command line
	for _PARAMETER in ${LIVE_CONFIG_CMDLINE}
	do
		case "${_PARAMETER}" in
			live-config.no_calamares=|no_calamares=)
				NO_CALAMARES="true"
				;;

			live-config.username=*|username=*)
				LIVE_USERNAME="${_PARAMETER#*username=}"
				;;
		esac
	done
}

Init ()
{
	# Checking if package is installed or already configured
	if  [ ! -e /usr/bin/calamares ]
	then
		exit 0
	fi

	echo -n " calamares"
}

Config ()
{
	if [ ! -e /usr/bin/calamares ]
	then
		exit 0
	fi

	if [ "${NO_CALAMARES}" != "true" ]
	then
		if [ ! -e /home/${LIVE_USERNAME}/Desktop ]
		then
			mkdir -p "/home/${LIVE_USERNAME}/Desktop"
		fi

cat > "/home/${LIVE_USERNAME}/Desktop/calamares.desktop" << EOF
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Version=1.0
Name=Install Netrunner Core
GenericName=Install Netrunner Core
TryExec=calamares
Exec=pkexec /usr/bin/calamares
Comment=Calamares — System Installer
Icon=calamares
Terminal=false
StartupNotify=true
Categories=Qt;System;
EOF

		chmod a+x "/home/${LIVE_USERNAME}/Desktop/calamares.desktop"
		chown -R ${LIVE_USERNAME}:${LIVE_USERNAME}  "/home/${LIVE_USERNAME}/Desktop"

	fi
	# Creating state file
	touch /var/lib/live/config/calamares
}

Cmdline
Init
Config
