#!/bin/sh

# Copyright (C) 2007  International Business Machines.
# All Rights Reserved.
# This file is distributed under the Eclipse Public License.
# It is part of the BuildTools project in COIN-OR (www.coin-or.org)
#
# $Id$
#
# Adapted from prepare_new_release by Lou Hafer, SFU, 100507. 

#set -x -v
set -e

# Know thy self. If there are no '/' chars in the command name, we're running
# in the current directory. Otherwise, strip the command name, leaving the
# prefix.  Coin-functions is expected to live in the same directory.

if expr "$0" : '.*/.*' >/dev/null 2>&1 ; then
  cmdDir=`echo $0 | sed -e 's,\(.*\)/[^/]*,\1,'`
else
  cmdDir='.'
fi
cmdDir=`cd $cmdDir ; pwd`
if test -r $cmdDir/coin-functions ; then
  . $cmdDir/coin-functions
else
  echo "Cannot find utility functions file coin-functions; exiting."
fi
#################### end of function definitions ##########################


# Note that plain sh does not accept negative exit values

exitValue=0

# Specify the COIN URL base for convenience.

coinURL="https://projects.coin-or.org/svn"

# Begin parameter processing.

printHelp=0
ignoreBuildToolsMismatch=no
mismatchBTExternals=
bumpMajor=0
suppressCheckout=0

# srcURL will be the parent for the stable we are building. We'll need to be
# able to distinguish ThirdParty and Data builds, and BuildTools itself; they
# require special handling.  cmdBTURL points to a BuildTools source and is
# required when srcURL specifies a ThirdParty or Data project --- we'll need to
# assemble a temporary package while creating the stable candidate.

srcURL=
isThirdParty=no
isData=no
isBuildTools=no
cmdBTURL=

# trunkExternals specifies externals for which we want to do simultaneous
# creation of stable branches. We will use the trunk as the external while
# preparing and testing the stable candidate, changing the Dependencies file
# to specify a (nonexistent) stable branch of the external at the last moment.

trunkExternals=

# exciseExternals specifies externals that should be removed when creating the
# new stable branch.

exciseExternals=

# We need at least one parameter.

if test "$#" -eq 0; then
  printHelp=1
else

# Process the parameters. A parameter without an opening `-' is assumed to be
# the spec for the source branch.

  while test $# -gt 0 && test $exitValue = 0 && test $printHelp = 0 ; do
    case "$1" in
      -h* | --h*) printHelp=1 ;;
      -i* | --i*)
	   if expr "$1" : '.*-i.*=.*' 2>&1 >/dev/null ; then
	     mismatchBTExternals=`echo $1 | sed -n -e 's/[^=]*=\(.*\)/\1/p'`
	   else
	     shift
	     mismatchBTExternals=$1
	   fi
	   if test "x$mismatchBTExternals" = "xall" ; then
	     ignoreBuildToolsMismatch=all
	   else
	     ignoreBuildToolsMismatch=partial
	   fi
	   ;;
      -m* | --m*) bumpMajor=1 ;;
      -p* | --p*) suppressCheckout=1 ;;
      -t* | --t*)
	   if expr "$1" : '.*-t.*=.*' 2>&1 >/dev/null ; then
	     trunkExternals=`echo $1 | sed -n -e 's/[^=]*=\(.*\)/\1/p'`
	   else
	     shift
	     trunkExternals=$1
	   fi
	   ;;
      -x* | --x*)
	   if expr "$1" : '.*-x.*=.*' 2>&1 >/dev/null ; then
	     exciseExternals=`echo $1 | sed -n -e 's/[^=]*=\(.*\)/\1/p'`
	   else
	     shift
	     exciseExternals=$1
	   fi
	   ;;
      -b* | --b*)
	   if expr "$1" : '.*-b.*=.*' 2>&1 >/dev/null ; then
	     cmdBTURL=`echo $1 | sed -n -e 's/[^=]*=\(.*\)/\1/p'`
	   else
	     shift
	     cmdBTURL=$1
	   fi
	   if expr "$cmdBTURL" : '.*BuildTools.*' 2>&1 >/dev/null ; then
	     case $cmdBTURL in
	       http*) ;;
		   *) cmdBTURL=${coinURL}/$cmdBTURL
		      ;;
	     esac
	   else
	     echo ''
	     echo "URL $cmdBTURL does not point to BuildTools."
	     echo ''
	     printHelp=1
	     exitValue=3
	    fi
	   ;;
      -*)  echo "$0: unrecognised command line switch '"$1"'."
	   printHelp=1
	   exitValue=1
	   ;;
       *)  srcURL=$1
	   case $srcURL in
	     http* ) ;;
	     BuildTools/ThirdParty/* )
		 srcURL=${coinURL}/$srcURL
		 ;;
	     ThirdParty/* )
		 srcURL=${coinURL}/BuildTools/$srcURL
		 ;;
	     * ) srcURL=${coinURL}/$srcURL
		    ;;
	   esac
	   ;;
    esac
    shift
  done

# Consistency check: Make sure that the source URL exists.  Note that it's not
# possible to specify a Data URL without including trunk/stable/release in the
# URL. For others, helpfully append `trunk'.

  srcURL=`echo $srcURL | sed -e 's/\/$//'`
  urlType=`extractTypeFromURL "$srcURL"`
  if test $printHelp = 0 && test $exitValue = 0 ; then
    if svn list $srcURL 2>&1 >/dev/null ; then
      :
    else
      echo "Source URL $srcURL does not seem to exist."
      printHelp=1
      exitValue=5
    fi
    if test "$urlType" = invalid ; then
      srcURL=$srcURL/trunk
      urlType=trunk
    fi
  fi

# Just what are we building? Order is important; BuildTools without ThirdParty
# means we're actually doing a BuildTools stable.

  if test $printHelp = 0 && test $exitValue = 0 ; then
    case $srcURL in
      *ThirdParty* )
	isThirdParty=yes
	;;
      *BuildTools* )
	isBuildTools=yes
	;;
      *Data* )
	isData=yes
	;;
      *)
	;;
    esac

# If we're building a ThirdParty or Data release, we need a BuildTools URL.

    if test $isThirdParty = yes || test $isData = yes ; then
      if test -z "$cmdBTURL" ; then
	cmdBTURL=`bestRelease $coinURL/BuildTools -1 -1`
        echo "A BuildTools URL is required for Data or ThirdParty projects."
	echo "Using $cmdBTURL; override with -b"
      else
	if svn list $cmdBTURL 2>&1 >/dev/null ; then
	  :
	else
	  echo "BuildTools URL $cmdBTURL does not seem to exist."
	  printHelp=1
	  exitValue=6
	fi
      fi
    fi
  fi
fi  # if "$#" .eq 0

if test $printHelp = 1 ; then
  cat <<EOF
Usage: prepare_new_stable [options] <source URL>

This script will create a new stable branch from the head of the specified
URL. Typically this will be the trunk, but it can be an existing stable
branch.  You can specify the entire URL, or just enter what comes after
"https://projects.coin-or.org/svn".  A typical example is

  prepare_new_stable Ipopt/trunk

Options:
  -b <BuildToolsURL>	URL for BuildTools; required to generate a release
  			for a ThirdParty or Data project.
  -i <projectlist>	Ignore BuildTools version mismatches for the listed
  			externals (comma-separated list of project names,
			e.g., -i Osi,Cbc). '-i all' ignores all mismatches.
  -p			Suppress checkout (useful for testing)
  -m			Bump the major version number.
  -t <project-list>	Suppress conversion from trunk to stable for the
			listed externals (comma-separated list of project
			names).
  -x <project-list>	Remove the listed projects from the list of externals
			(comma-separated list of project names).

This script will do the following:

  - Set the new stable version number as the next minor version number in
    the current major version number. Use the -m flag to bump the major
    version number.

  - Convert externals from trunk to the top stable branch. Externals which
    are currently stable or release are left untouched. Use -t to suppress
    the change from trunk to stable. Set_externals is then invoked to set
    release externals where available.

  - Check out externals. The BuildTools version used by externals (if any)
    is checked, and the script issues a warning if it doesn't match the
    version used by the source URL.

  - Run the scripts to download any ThirdParty code.

  - Run run_autotools to rebuild configure and make files.

  - Run configure, make, and make test

  - Tweak the externals to upgrade trunk (-t) dependencies to stable (you
    won't get to release in this case).

If there is any error during these tasks the script will stop and you should
examine the output.

If the script completes without error, examine the output, particularly the
output of the unit test ('make test') and the set of externals specified in
the Dependencies file. Whether you start with Externals or Dependencies, the
stable will have a proper Dependencies. Externals will be unmodified.

This script does not make any changes to the repository.  If you want to
commit the new stable branch, run the "commit_new_stable" script, as described
at the end of the output.

EOF
fi

if test $exitValue != 0 || test $printHelp = 1 ; then
  exit $exitValue
fi

# End of parameter parsing. We have a source URL to work with.  Tell the
# user what we've seen.

srcURL=`echo $srcURL | sed -e 's|/$||'`
srcProj=`extractProjFromURL $srcURL`

echo "Source URL..........: $srcURL"
echo "Base project........: $srcProj"
if test $isThirdParty = yes || test $isData = yes ; then
  echo "BuildTools URL......: $cmdBTURL"
fi

# Figure out the URL of the new stable branch. Consider that there may not be
# any existing stable branches.

topStableURL=`bestStable $srcURL -1`
echo "Top stable URL......: ${topStableURL:-none}"

if test -z "$topStableURL" ; then
  majVer=0
  minVer=1
  case "$srcURL" in
    */Data/* )
	newStableURL=$coinURL/Data/stable/$majVer.$minVer/$srcProj
	;;
    */ThirdParty/* )
	newStableURL=$coinURL/BuildTools/ThirdParty/$srcProj/stable/$majVer.$minVer
	;;
    * )
	newStableURL=$coinURL/$srcProj/stable/$majVer.$minVer
	;;
  esac
else
  majVer=`extractMajorFromURL $topStableURL`
  if test $bumpMajor = 1 ; then
    majVer=`expr $majVer + 1`
    minVer=0
  else
    minVer=`extractMinorFromURL $topStableURL`
    minVer=`expr $minVer + 1`
  fi
  newStableURL=`replaceVersionInURL $topStableURL $majVer $minVer`
fi
echo "New stable URL......: $newStableURL"
newVersion=${majVer}.${minVer}

# Construct a build directory name.

topBuildDir=`echo $srcProj | sed -e 's|.*/\([^/]*\)$|\1|'`
topBuildDir=${topBuildDir}-${newVersion}
echo "Build directory.....: $topBuildDir"

# Now decide where to check out code.

if test $isThirdParty = yes; then
  coDir=$topBuildDir/Thirdparty/$srcProj
elif test $isData = yes; then
  coDir=$topBuildDir/Data/$srcProj
else
  coDir=$topBuildDir
fi
echo "Checkout directory..: $coDir"

echo ''
echo "===> Checking out source $srcURL without externals ..."
echo ''

cmd="svn co --ignore-externals $srcURL $coDir"
if test $suppressCheckout = 1 ; then
  echo "Pretending to do: $cmd"
else
  rm -rf $topBuildDir
  echo $cmd
  eval $cmd
fi

if test $isThirdParty = yes || test $isData = yes; then
  echo ''
  echo '===> Checking out BuildTools (Data or ThirdParty) ...'
  echo ''
  cmd="svn co $cmdBTURL $topBuildDir/BuildTools"
  if test $suppressCheckout = 1 ; then
    echo "Pretending to do: $cmd"
  else
    echo $cmd
    eval $cmd
  fi
fi

startDir=`pwd`
coDir=`cd $coDir; pwd`
topBuildDir=`cd $topBuildDir; pwd`

cd $coDir

# Find configure.ac files for the package and project and update the version.
# We have no externals at this point, and no third-party code, so there will
# be two files for a standard project, one for a ThirdParty or Data project.

echo ''
bak_files=`find . -name 'configure.ac'`
echo "===> Creating backup (.bak) for configure.ac files..."
for i in $bak_files; do
  cp $i $i.bak
done

# Take the attitude that [] around parameters in AC_INIT is optional,
# it's the commas that count. This does make for a surpassing ugly regular
# expression.  A comma in the version string will cause a spectacular failure.
# In AC_COIN_PROJECTDIR_INIT, take the attitude that the existing parameters
# don't matter, we know what the release parameters should be.

echo ''
echo "===> Updating version numbers in configure.ac files"
for i in $bak_files; do
  sed -e "s|AC_INIT\(.*\),\(\[*\)[^],]*\(\]*\),\(.*\)|AC_INIT\1,\2$newVersion\3,\4|" $i > bla
  mv bla $i
  svn diff $i
done

# Find ProjConfig.h. If there's a definition for PROJ_VERSION, adjust it and
# add ProjConfig.h.bak to the list of files to be restored.

srcProjUC=`echo $srcProj | tr '[a-z]' '[A-Z]'`
configFileLoc=`find . -name .svn -prune -o -name '*Config.h'  -print`
if test -n "$configFileLoc" ; then
  versionSym=${srcProjUC}_VERSION
  echo ''
  echo "===> Updating $versionSym in $configFileLoc (if present)"
  echo ''
  mv $configFileLoc $configFileLoc.bak
  bak_files="$bak_files $configFileLoc"
  sed -e "s/# *define $versionSym.*\$/#define $versionSym \"$newVersion\"/" <$configFileLoc.bak >$configFileLoc
  svn diff $configFileLoc
fi

# Now generate a proper Dependencies file for the stable branch.  References to
# trunk will be converted to references to stable branches unless the reference
# is to a project in the trunkExternals list (in which case it'll be converted
# at the very end). References to releases are not changed. Each line in a
# Dependencies file has the format <ext_name> <ext_url>. Normally, each entry
# should be a stable branch.

srcDepFile=
for file in Dependencies Externals ; do
  if test -r $file ; then
    srcDepFile=$file
    break
  fi
done

if test -n "$srcDepFile" ; then

# Save the externals property of the source, so we can just restore it after
# the commit. Also save srcDepFile, because we may well modify it for the
# stable branch, converting trunk externals to stable externals.

  svn propget svn:externals . > .Externals.original
  bak_files="$bak_files $srcDepFile"
  cp $srcDepFile $srcDepFile.bak

  echo ''
  echo "===> Checking externals in $srcDepFile ..."
  echo ''

# We've just checked this out, no sense going back to the server for the
# BuildTools version. Because of the intermediate variable, newline has
# become space.

  srcExternals=`svn propget svn:externals .`
  srcBTURL=`echo $srcExternals | \
	    sed -n -e 's/.*BuildTools *https:\([^ ]*\) .*/https:\1/p'`
  if test -z "$srcBTURL" ; then
    srcBTURL="none"
  fi
  echo "Source BuildTools...:  $srcBTURL"

  rm -f Dependencies
  ext_name=
  ext_url=
  buildtoolsMismatch=0
  for i in `cat $srcDepFile.bak` ; do
    if test "$ext_name" = "" ; then
      ext_name=$i
    else
      ext_url=$i
      if expr "$ext_name" : '#.*' 2>&1 >/dev/null ; then
        echo "    $ext_name $ext_url ==> skipped"
        ext_name=
        continue
      fi
      ext_urltype=`extractTypeFromURL $ext_url`
      ext_proj=`extractProjFromURL $ext_url`

# See if this external should be dropped.

      if expr "$exciseExternals" : '.*'$ext_proj'.*' 2>&1 > /dev/null ; then
        echo "    $ext_name $ext_url ==> excised"
	ext_name=
	continue
      fi

      ext_isNormalURL=`isNormalURL $ext_url`

# Convert a trunk URL to stable unless it's listed in trunkExternals.

      if test $ext_urltype = trunk ; then
        if expr "$trunkExternals" : '.*'$ext_proj'.*' 2>&1 >/dev/null ; then
	  echo "    $ext_name $ext_url ==> unchanged"
	else
	  ext_oldurl=$ext_url
	  ext_url=`bestStable $ext_url -1`
	  # Normal (not BuildTools/ThirdParty/Data) need a directory name,
	  # and it may differ from the project name. Carefully preserve it.
	  if test $ext_isNormalURL = yes ; then
	    ext_tail=`extractTailFromExt $ext_oldurl`
	    ext_url=${ext_url}${ext_tail}
	  fi
	  echo "    $ext_name $ext_oldurl ==> $ext_url"
	fi
      else
        echo "    $ext_name $ext_url ==> unchanged"
      fi

# Get the BuildTools URL for the external and compare to the BuildTools URL
# for the source, assuming we have one and the external has one.

      if test $ext_isNormalURL = yes &&
         test $ext_proj != BuildTools && test $srcBTURL != none ; then
        ext_url_notail=`echo $ext_url | sed -e 's,/[^/]*$,,'`
	extBTURL=`svn propget svn:externals $ext_url_notail`
	extBTURL=`echo $extBTURL | \
	  sed -n -e 's/.*BuildTools https:\([^ ]*\) .*/https:\1/p'`
	if test -n "$extBTURL" ; then
	  testResult=`compareURLVersions "$srcBTURL" "$extBTURL"`
	  if test $testResult = no ; then
	    if test $ignoreBuildToolsMismatch = all || \
	       expr "$mismatchBTExternals" : '.*'$ext_proj'.*' 2>&1 >/dev/null ; then
	      echo "    WARNING: BuildTools mismatch: $ext_url_notail uses $extBTURL"
	    else
	      buildtoolsMismatch=1
	      echo "    ERROR: BuildTools mismatch: $ext_url_notail uses $extBTURL"
	    fi
	  fi
	fi
      fi

      echo "$ext_name  $ext_url" >>Dependencies
      ext_name=
      echo ''
    fi
  done

# If we have a BuildTools mismatch, exit.

  if test $buildtoolsMismatch = 1 ; then
    echo "Exiting due to BuildTools mismatches; use -i to ignore."
    exit 2
  fi

  $cmdDir/set_externals Dependencies

# Try three times to check out externals before conceding defeat.

  echo ''
  echo '===> Checking out externals ...'
  echo ''

  svn update ||
  { echo "Retry 1 ... " ; svn update ; } ||
  { echo "Retry 2 ... " ; svn update ; } ||
  { echo "Checkout of externals failed. Aborting." ; exit 3 ; }

# Run any scripts to acquire ThirdParty code.

  if test -d ThirdParty ; then

    echo ''
    echo '===> Downloading ThirdParty code ...'
    echo ''
    
    ext_name=
    ext_url=
    for i in `svn propget svn:externals .` ; do
      if test -z "$ext_name" ; then
        ext_name=$i
      else
        ext_url=$i
	if expr "$ext_name" : 'ThirdParty/.*' 2>&1 >/dev/null ; then
	  cd $ext_name
	  ext_proj=`extractProjFromURL $ext_url`
	  getScript=get.$ext_proj
	  if test -x "$getScript" ; then
	    ./$getScript -patch
	  fi
	  cd $coDir
	fi
	ext_name=
      fi
    done
  fi
fi


# Done processing externals. If this is a ThirdParty project, we still have
# to run the get script.

if test $isThirdParty = yes; then
  if test -x get.$srcProj ; then
    echo ''
    echo '===> Downloading third party code...'
    echo ''
    ./get.$srcProj
  fi
fi

# Run the autotools to update configure and make files

echo ''
echo '===> Running BuildTools/run_autotools ...'
echo ''

if test $isThirdParty = yes || test $isData = yes ; then
  cd ../..
  ./BuildTools/run_autotools
  cd "$coDir"
else
  ./BuildTools/run_autotools
fi

# Let's see if it works. We only run tests for non-ThirdParty, non-Data. DO NOT
# turn on --enable-maintainer-mode in the initial configure command. At the
# least, it's not needed. At the worst, as of 100526, it'll undo all the
# careful work above to set externals.

if test $isThirdParty != yes && test $isData != yes; then (
   set -e
   echo ''
   echo '===> Creating build directory and running the configuration script...'
   echo ''
   mkdir build
   cd build
   cmd="$coDir/configure -C"
   echo $cmd
   eval $cmd
   echo ''
   echo '===> Compiling code...'
   echo ''
   cmd='make install'
   echo $cmd
   eval $cmd
   echo ''
   echo '===> Running the unit test...'
   echo ''
   echo '*******************************************************************************'
   echo '***                                                                         ***'
   echo '***                       BEGIN OUTPUT OF MAKE TEST                         ***'
   echo '***                                                                         ***'
   echo '*******************************************************************************'
   echo ''
   cmd='make test'
   echo $cmd
   eval $cmd
   echo ''
   echo '*******************************************************************************'
   echo '***                                                                         ***'
   echo '***                        END OUTPUT OF MAKE TEST                          ***'
   echo '***                                                                         ***'
   echo '*******************************************************************************'
  )
  if test $? != 0; then
    echo ''
    echo 'Error during build or test'
    echo ''
    exit 3
  fi
fi

# No fatal errors. Declare victory.

echo ''
echo '===> ALL TESTS PASSED'
if test $isThirdParty != yes &&
   test $isData != yes && test $isBuildTools != yes ; then
  echo ''
  echo 'Please review the output above, particularly the one of make test'
fi
echo ''

# Do we need to plug in nonexistent stable branches for circular dependencies
# tested with the trunk? If so, generate a new Dependencies. This is the only
# reason trunk references should appear in the externals for a stable branch,
# so we don't need to check further before removing them.

if test -n "$trunkExternals" ; then
  echo ''
  echo "===> Grooming externals to remove trunk references used for testing."
  echo ''
  mv Dependencies Dependencies.temp.$$
  ext_name=
  ext_url=
  for i in `cat Dependencies.temp.$$`; do
    if test "$ext_name" = ""; then
      ext_name="$i"
    else
      ext_url=$i
      ext_urltype=`extractTypeFromURL $ext_url`
      ext_proj=`extractProjFromURL $ext_url`
      if test $ext_urltype = trunk ; then
	ext_oldurl=$ext_url
	ext_url=`bestStable $ext_url -1`
	ext_majVer=`extractMajorFromURL $ext_url`
	ext_minVer=`extractMinorFromURL $ext_url`
	ext_minVer=`expr $ext_minVer + 1`
	ext_url=`replaceVersionInURL $ext_url $ext_majVer $ext_minVer`
	ext_isNormal=`isNormalURL $ext_url`
	if test $ext_isNormal = yes ; then
	  ext_url="${ext_url}${ext_proj}"
	fi
	echo "    $ext_name $ext_oldurl ==> $ext_url"
      fi
      echo "$ext_name  $ext_url" >>Dependencies
      ext_name=
    fi
  done
  rm -f Dependencies.temp.$$
  $cmdDir/set_externals Dependencies
fi

if test -r Dependencies ; then
  echo ''
  echo 'Also, please confirm the Externals are correct:'
  svn propget svn:externals
fi

echo ''
echo 'After reviewing the output above, you can create a new stable by going into'
echo 'the directory'
echo ''
echo "          $startDir"
echo ''
echo "and run the commit_new_stable script"

cd $topBuildDir

# Record information for the commit_new_stable script.

cat >.new_stable_data <<EOF
coinURL=$coinURL
startDir=$startDir
topBuildDir=$topBuildDir
coDir=$coDir
newStableURL=$newStableURL
srcURL=$srcURL
newVersion=$newVersion
bak_files="$bak_files"
EOF
