#!/usr/local/bin/php
<?php

/*
 * Copyright (C) 2014-2023 Franco Fichtner <franco@opnsense.org>
 * Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
 * Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
 * Copyright (C) 2009 Erik Kristensen <erik@erikkristensen.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* looks weird, but means we started PHP successfully */
echo "done.\n";

echo "Initializing...";
flush();

$inc_files = [
    'config.inc',
    'console.inc',
    'auth.inc',
    'util.inc',
    'interfaces.inc',
    'system.inc',
    'filter.inc',
    'rrd.inc',
];

foreach ($inc_files as $inc_file) {
    require_once $inc_file;
    echo '.';
    flush();
}

echo "done.\n";

/*
 * If this was an import or restore or reset, once run
 * the migration, but do not display the status here as
 * otherwise this prints all the models that keep their
 * current version level.
 */
convert_config();

system_devd_configure(true);
system_login_configure(true);

/*
 * Determine if we need to throw an interface exception
 * and ask the user to reassign interfaces.  This will
 * avoid a reboot and thats a good thing.
 */
if (is_interface_mismatch()) {
    echo "\nDefault interfaces not found -- Running interface assignment option.\n";
    while (!set_networking_interfaces_ports(true));
}

interfaces_loopback_configure(true); /* loopback device is required early for syslog */
system_kernel_configure(true);
system_sysctl_configure(true);
system_timezone_configure(true);
system_firmware_configure(true);
system_trust_configure(true);
system_hostname_configure(true);
system_resolver_configure(true);
system_syslog_start(true);

filter_configure_sync(true, false); /* apply default policy before interface setup */
interfaces_hardware(true);
interfaces_configure(true);
system_resolver_configure(true); /* adapts to runtime interface configuration */
filter_configure_sync(true);
plugins_configure('early', true);
system_routing_configure(true, null, false);

plugins_configure('dhcp', true);
plugins_configure('dhcrelay', true);
plugins_configure('dns', true);

plugins_configure('monitor', true, [null, true]);
filter_configure_sync(true);
plugins_configure('vpn', true);
plugins_configure('bootup', true);
rrd_configure(true, true);

system_powerd_configure(true); /* XXX this seems misplaced */

exit(0);
