#!/bin/bash
#
# Copyright (c) 2007 Juan Manuel Palacios <jmpp@macports.org>, MacPorts Team.
# 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. Neither the name of MacPorts Team nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# 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.


###
# dp2mp-move upgrading facility for users installing MacPorts through the pkg installer in the dmg.
# This script is meant to run as the preflight of the installer, to upgrade an existing MacPorts
# installation to the MacPorts namespace in the same way the "upgrade" target in base/Makefile
# does for users installing from source or selfupdate.
# preflight
# $Id: preflight 26533 2007-06-27 22:39:54Z jmpp@macports.org $
###


PATH=/bin:/sbin:/usr/bin:/usr/sbin
UPGRADECHECK=/opt/local/var/macports/.mprename


[ ! -d /Library/Tcl/darwinports1.0 ] || rm -rf /Library/Tcl/darwinports1.0

[ ! -d /opt/local/share/darwinports ] || rm -rf /opt/local/darwinports

[ ! -f /opt/local/etc/ports/dp_version ] || rm -vf /opt/local/etc/ports/dp_version

[ ! -f /opt/local/share/man/man5/ports.conf.5 ] || rm -vf /opt/local/share/man/man5/ports.conf.5

[ ! -d /opt/local/etc/ports ] || mv -v /opt/local/etc/ports /opt/local/etc/macports

[ -d /opt/local/var/macports ] || mkdir -vp /opt/local/var/macports
for dir in distfiles packages receipts software; do
    [ ! -d /opt/local/var/db/dports/${dir} ] || mv -v /opt/local/var/db/dports/${dir} /opt/local/var/macports
done

[ ! -d /opt/local/var/db/dports/sources/rsync.rsync.darwinports.org_dpupdate_dports ] || {
    mkdir -vp /opt/local/var/macports/sources/rsync.macports.org/release && mv -v \
        /opt/local/var/db/dports/sources/rsync.rsync.darwinports.org_dpupdate_dports /opt/local/var/macports/sources/rsync.macports.org/release/ports ;
}

for receipt in /opt/local/var/macports/receipts/*/*/receipt.bz2 ; do
    [ ! \( -f ${receipt} -a ! -f ${receipt}.mpsaved \) ] || { cp -v ${receipt} ${receipt}.mpsaved && {
      bzip2 -q -dc ${receipt} | sed 's/db\/dports/macports/g' | bzip2 -q -zf > ${receipt}.new ;
    } && mv -v ${receipt}.new ${receipt} ;
}; done

[ ! \( -f /opt/local/etc/macports/ports.conf -a ! -f ${UPGRADECHECK} \) ] || {
  mv -v /opt/local/etc/macports/ports.conf /opt/local/etc/macports/macports.conf.mpsaved
  sed 's/etc\/ports/etc\/macports/g' /opt/local/etc/macports/macports.conf.mpsaved > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/db\/dports/macports/g' /opt/local/etc/macports/macports.conf > /opt/local/etc//macports/macports.conf.tmp && \
    mv -v /opt/local/etc//macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/darwinports/macports/g' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/dpupdate1\/base/release\/base/g' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/dpupdate\/base\/\{0,1\}/trunk\/base\//g' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed '/^rsync_options/s/"\(.*\)"/\1/' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/ --delete / /' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/ ports.conf(5)/ macports.conf(5)/g' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
  sed 's/[Dd]\(arwin\)\{0,1\}[Pp]\(orts\)\{0,1\}/MacPorts/g' /opt/local/etc/macports/macports.conf > /opt/local/etc/macports/macports.conf.tmp && \
    mv -v /opt/local/etc/macports/macports.conf.tmp /opt/local/etc/macports/macports.conf
}

[ ! \( -f /opt/local/etc/macports/sources.conf -a ! -f ${UPGRADECHECK} \) ] || {
  cp -v /opt/local/etc/macports/sources.conf /opt/local/etc/macports/sources.conf.mpsaved
  sed 's/darwinports/macports/g' /opt/local/etc/macports/sources.conf > /opt/local/etc/macports/sources.conf.tmp && \
    mv -v /opt/local/etc/macports/sources.conf.tmp /opt/local/etc/macports/sources.conf
  sed 's/dpupdate\/dports/release\/ports\//g' /opt/local/etc/macports/sources.conf > /opt/local/etc/macports/sources.conf.tmp && \
    mv -v /opt/local/etc/macports/sources.conf.tmp /opt/local/etc/macports/sources.conf
}

[ ! \( -f ${HOME}/.macports/ports.conf -a ! -f ${UPGRADECHECK} \) ] || {
  mv -v ${HOME}/.macports/ports.conf ${HOME}/.macports/macports.conf.mpsaved
  sed 's/etc\/ports/etc\/macports/g' ${HOME}/.macports/macports.conf.mpsaved > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/db\/dports/macports/g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/darwinports/macports/g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/dpupdate1\/base/release\/base/g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/dpupdate\/base\/\{0,1\}/trunk\/base\//g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed '/^rsync_options/s/"\(.*\)"/\1/' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/ --delete / /' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
  sed 's/ ports.conf(5)/ macports.conf(5)/g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
   sed 's/[Dd]\(arwin\)\{0,1\}[Pp]\(orts\)\{0,1\}/MacPorts/g' ${HOME}/.macports/macports.conf > ${HOME}/.macports/macports.conf.tmp && \
    mv -v ${HOME}/.macports/macports.conf.tmp ${HOME}/.macports/macports.conf
}

[ -f ${UPGRADECHECK} ] || {
    echo ""; echo "MacPorts installation successfully upgraded from the old DarwinPorts namespace!"; echo ""
	echo "MacPorts rename update done!" > ${UPGRADECHECK} ;
}
