#!/usr/bin/php -q
<?PHP
/* Copyright 2015-2023, Lime Technology
 * Copyright 2015-2016, Guilherme Jardim, Eric Schultz, Jon Panozzo.
 * Copyright 2012-2023, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
/* Define the path to the docker configuration file */
$cfgfile = "/boot/config/docker.cfg";

/* Define the default configuration values */
$cfg_defaults = [
	"DOCKER_ENABLED" => "no",
	"DOCKER_IMAGE_FILE" => "/mnt/user/system/docker/docker.img",
	"DOCKER_IMAGE_SIZE" => "20",
	"DOCKER_APP_CONFIG_PATH" => "/mnt/user/appdata/",
	"DOCKER_APP_UNRAID_PATH" => "",
	"DOCKER_READMORE" => "yes",
	"DOCKER_PID_LIMIT" => ""
];

/* Initialize the new configuration with the default values */
$cfg_new = $cfg_defaults;

/* Check if the configuration file exists */
if (file_exists($cfgfile)) {
	/* Parse the existing configuration file */
	$cfg_old = parse_ini_file($cfgfile);

	/* If the existing configuration is not empty, merge it with the defaults */
	if (!empty($cfg_old)) {
		/* Merge only missing keys from defaults */
		$cfg_new = array_merge($cfg_defaults, $cfg_old);

		/* If there are no changes between the new and old configurations, unset the new configuration */
		if (empty(array_diff_assoc($cfg_new, $cfg_old))) {
			$cfg_new = null;
		}
	}
}

/* If the new configuration is set, write it to the configuration file */
if (isset($cfg_new)) {
	$tmp = '';
	foreach ($cfg_new as $key => $value) {
		$tmp .= "$key=\"$value\"\n";
	}
	file_put_contents($cfgfile, $tmp);
}
?>
