#!/bin/sh
#
# pre_upgrade_command
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
# 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.
#
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgment:
#    "This product includes software developed by the pfSense Project
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
#
# 4. The names "pfSense" and "pfSense Project" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For written permission, please contact
#    coreteam@pfsense.org.
#
# 5. Products derived from this software may not be called "pfSense"
#    nor may "pfSense" appear in their names without prior written
#    permission of the Electric Sheep Fencing, LLC.
#
# 6. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#
# "This product includes software developed by the pfSense Project
# for use in the pfSense software distribution (http://www.pfsense.org/).
#
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
# EXPRESSED 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 pfSense PROJECT OR
# ITS CONTRIBUTORS 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.

/etc/rc.conf_mount_rw

# Record the previous version
PRIOR_VERSION=`uname -r | cut -d'.' -f1`
echo $PRIOR_VERSION > /tmp/pre_upgrade_version

# De-activate sync on root filesystem. See ticket #4523
# Back up original fstab
/bin/cp /etc/fstab /etc/fstab.orig
# De-activate sync on the root slice only. This will not match NanoBSD since it already has sync,noatime
/usr/bin/sed -i '' 's/^\(\/.*[[:space:]]*\/[[:space:]]*ufs[[:space:]]*\)rw,sync\([[:space:]]*[[:digit:]][[:space:]]*[[:digit:]]\)$/\1rw\2/' /etc/fstab

# Uninstall all packages before upgrade to 2.3
PFSENSE_VERSION=$(cat /etc/version)

if echo "$PFSENSE_VERSION" | grep -q '^1.2'; then
	UNINSTALL="uninstall_package_from_name"
else
	UNINSTALL="uninstall_package"
fi

cat >/tmp/remove_all_packages.php <<EOD
#!/usr/local/bin/php
<?php

require_once("pkg-utils.inc");

global \$pkg_interface;
\$pkg_interface = "console";

if (!isset(\$config['installedpackages']['package'])) {
	exit(0);
}

if (!is_array(\$config['installedpackages']['package'])) {
	exit(0);
}

\$removed_packages = array();
foreach (\$config['installedpackages']['package'] as \$package) {
	if (isset(\$package['internal_name'])) {
		\$removed_packages[] = \$package['internal_name'];
	} else {
		\$rename_arr = array("Quagga OSPF" => "Quagga_OSPF", "System Patches" => "System_Patches");
		if (key_exists(\$package['name'], \$rename_arr)) {
			\$removed_packages[] = \$rename_arr[\$package['name']];
		} else {
			\$removed_packages[] = \$package['name'];
		}
	}
	${UNINSTALL}(\$package['name']);
}

@file_put_contents("{\$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt", implode("\\n",\$removed_packages));

?>
EOD

/usr/local/bin/php /tmp/remove_all_packages.php
rm -f /tmp/remove_all_packages.php

# Cleanup PBIs
if which pbi_info >/dev/null 2>&1; then
	for pbi in $(pbi_info); do
		echo ">>> Removing PBI ${pbi} and all symlinks"
		pbi_prefix=$(pbi_info ${pbi} | awk '$1 == "Prefix:" { print $2  }')

		for subdir in bin sbin; do
			if [ ! -d "${pbi_prefix}/${subdir}" ]; then
				continue
			fi

			for pbiopt in ${pbi_prefix}/${subdir}/*.pbiopt; do
				link_name=$(awk '$1 == "TARGET:" { print $2 }' ${pbiopt})

				if [ -z "${link_name}" ]; then
					continue
				fi

				if [ -L /usr/local/${link_name} ]; then
					rm -f /usr/local/${link_name}
				fi
			done
		done

		pbi_delete ${pbi}
	done

	# Cleanup remaining symbolic links
	find / -type l -lname '/usr/pbi/*' -delete
fi

# Hack to workaround ticket #3749
if [ ${PRIOR_VERSION} -le 8 ] && grep -q 'sh /etc/rc.reboot' /etc/rc.firmware; then
	PROC=$(ps axwww | grep '/etc/rc.firmware *pfSenseupgrade')
	PID=''
	IMG=''
	if [ -n "${PROC}" ]; then
		PID=$(echo "${PROC}" | awk '{print $1}')
		IMG=$(echo "${PROC}" | sed 's,^.*pfSenseupgrade *,,')
	fi

	if [ -n "${PID}" -a -n "${IMG}" -a -f "${IMG}" ]; then
		cp -fp /bin/sh /tmp/sh.old
		kill ${PID} >/dev/null 2>&1
		kill -9 ${PID} >/dev/null 2>&1
		sed -i '' -e 's,sh /,/tmp/sh.old /,' \
			  -e 's,/usr/sbin/mtree,/tmp/mtree.old,' /etc/rc.firmware
		# To avoid restarting rc.firmware twice because of #4328
		sed -i '' -e '/^restore_chflags()/, /^}/ s/mtree *-PU/mtree -P/' /etc/rc.firmware
		/etc/rc.firmware pfSenseupgrade "${IMG}"
		exit
	fi
fi

# One more hack to workaround #4328
if grep -q 'mtree *-PU' /etc/rc.firmware; then
	PROC=$(ps axwww | grep '/etc/rc.firmware *pfSenseupgrade')
	PID=''
	IMG=''
	if [ -n "${PROC}" ]; then
		PID=$(echo "${PROC}" | awk '{print $1}')
		IMG=$(echo "${PROC}" | sed 's,^.*pfSenseupgrade *,,')
	fi

	if [ -n "${PID}" -a -n "${IMG}" -a -f "${IMG}" ]; then
		kill ${PID} >/dev/null 2>&1
		kill -9 ${PID} >/dev/null 2>&1
		sed -i '' -e '/^restore_chflags()/, /^}/ s/mtree *-PU/mtree -P/' /etc/rc.firmware
		/etc/rc.firmware pfSenseupgrade "${IMG}"
		exit
	fi
fi

# Make sure /boot files are not protected with schg
chflags -R noschg /boot >/dev/null 2>&1

rm /boot/kernel/*

