#!/bin/sh
# -*- coding: utf-8 -*-

# While the idea of this script is noble, there are quite a few showstoppers along the way.

# First, you cannot compile certain PHP 7.1 components while their 5.6 counterparts are installed.
# Next, pecl-intl needs to be removed.
# Finally, this script doesn't handle ports with names other than php56-*, not to mention mod_php56 and pear-*.

# Oh, and ownCloud 9.x doesn't handle PHP 7.1, but maybe it can live with PHP 7.0.
# Let's wait for ownCloud 10.x to appear in the ports tree.

# Conclusion: Use your own metaport(s) for specifying what to install on
# each machine, remove all traces of PHP 5.6, adjust /etc/make.conf, and
# reinstall all the missing pieces.

# Copyright © 2017, Trond Endrestøl <Trond.Endrestol@ximalas.info>
# 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR 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.

# Check to see if lang/php56 exists and abort if it doesn't.
/usr/local/sbin/pkg query %o php56 >/dev/null 2>&1 || (echo "${0}: lang/php56 not installed" >/dev/stderr; exit 69)

# Forcefully upgrade lang/php56 to lang/php71, only this port.
# The first command lets the user configure the new port.
# The next command does the hard work.
/usr/local/sbin/portupgrade -ncfpvo lang/php71 lang/php56 || exit
/usr/local/sbin/portupgrade -fpvo lang/php71 lang/php56 || exit

# Gather a list of ports named php56-*.
PHP56_PORTS=`/usr/local/sbin/pkg query -a %o | /usr/bin/grep 'php56-'`

# Stop if there's nothing more to do.
if [ -z "${PHP56_PORTS}" ]; then
  echo "${0}: no installed ports named php56-*" >/dev/stderr;
  exit 69;
fi

MISSING_PORTS=""
EXCLUDED_PORTS="-x lang/php71"

# Give the user a chance to configure the new ports, and gather any missing dependencies.
for p in ${PHP56_PORTS}; do
  PHP71_PORT_ORIGIN=`echo ${p} | /usr/bin/sed 's/php56/php71/'`;
  MISSING_PORTS="${MISSING_PORTS} `/usr/bin/make -C /usr/ports/${PHP71_PORT_ORIGIN} missing`";
  EXCLUDED_PORTS="${EXCLUDED_PORTS} -x ${PHP71_PORT_ORIGIN}";
  /usr/local/sbin/portupgrade -ncfpvo ${PHP71_PORT_ORIGIN} ${p} || exit;
done

# Sort the list of new dependencies and remove any duplicates.
MISSING_PORTS=`echo ${MISSING_PORTS} | /usr/bin/sort | /usr/bin/uniq`

# If there's anything missing, now's the chance to configure and install them.
if [ -n "${MISSING_PORTS}" ]; then
  /usr/local/sbin/portupgrade -ncNfprv ${MISSING_PORTS} || exit;
  /usr/local/sbin/portupgrade -Nfprv ${MISSING_PORTS} || exit;
fi
# Hopefully portupgrade should have done its job by now, but don't hold your breath.

# Upgrade each PHP56 port individually.
for p in ${PHP5_PORTS}; do
  PHP71_PORT_ORIGIN=`echo ${p} | /usr/bin/sed 's/php56/php71/'`;
  /usr/local/sbin/portupgrade -fpvo ${PHP71_PORT_ORIGIN} ${p} || exit;
done

# Forcefully recreate anything depending on lang/php71 including user applications, e.g. Moodle, ownCloud, WordPress, etc.
/usr/local/sbin/portupgrade -fprv ${EXCLUDED_PORTS} lang/php71 || exit

# Tell the user what to do next.
echo
echo "${0}: now you should restart Apache"
