#! /bin/sh
# $Id: make-current 7266 2022-08-04 00:27:03Z eager $
# Create DDD release-of-the-day.  

# Copyright (C) 1997-1999 Technische Universitaet Braunschweig, Germany.
# Copyright (C) 2000-2001 Universitaet Passau, Germany.
# Written by Andreas Zeller <zeller@gnu.org>.
# 
# This file is part of DDD.
# 
# DDD is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# 
# DDD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with DDD -- see the file COPYING.
# If not, see <http://www.gnu.org/licenses/>.
# 
# DDD is the data display debugger.
# For details, see the DDD World-Wide-Web page, 
# `http://www.gnu.org/software/ddd/',
# or send a mail to the DDD developers <ddd@gnu.org>.

# This script checks out the current DDD development tree, as
# stored in SVN, and creates a DDD release-of-the-day (`ROTD').

# Usage: `make-current [-final][-keep] 
# -final: make an official release (else: release-of-the-day).
# -keep: retain temporary build directory

# Some Bash versions issue the name of the directory they're changing into :-(
unset CDPATH

# Check args
final=false
keep=false

for arg in "$@"
do
  case "$arg" in
    *final*) final=true;;
    *keep*) keep=true;;
  esac
done

# Fetch the date in ISO 8601 YYYY-MM-DD format.
year=`date +"%Y"`
month=`date +"%m"`
day=`date +"%d"`
date=$year-$month-$day

# Setup environment
tmpdir=$HOME/tmp/ddd$$
target=$HOME/ddd-release

# SVN repository
SVNREPO="svn://svn.savannah.gnu.org/ddd/trunk"

echo make-current[$$]: start at `date`

echo "Target is $target"
echo "Working directory is $tmpdir"
mkdir -p $target
mkdir -p $tmpdir
cd $tmpdir

# We are working in `$HOME/tmp/ddd'.  In case of trouble, clean up.
trap "exit 1" 1 2 15
trap "set +x; echo -n 'Cleaning up...'; if [ "$keep" == "false" ]; then rm -fr $tmpdir; fi; echo done." 0

# Check out sources to $tmpdir
echo "svn co $SVNREPO ."
svn co $SVNREPO . > svn.log

# Extract VERSION, EXPIRES, NICKNAME
VERSION=$(grep AC_INIT configure.ac | cut -d ',' -f 2 | sed 's/\[\(.*\)\]/\1/')
EXPIRES=$(grep EXPIRES= configure.ac | cut -d '"' -f 2)
NICKNAME=$(grep NICKNAME= configure.ac | cut -d '"' -f 2)

if $final; then
  # This is a final release; use the expiration date from the file.
  expires=$EXPIRES
  version=$VERSION
  nickname=$NICKNAME
else
  # Let this release expire in 30-60 days.  It is unlikely we won't make
  # any changes until then.
  next_year=`expr $year + 1`
  case $month in
    01) expires=$year-03-01;;
    02) expires=$year-04-01;;
    03) expires=$year-05-01;;
    04) expires=$year-06-01;;
    05) expires=$year-07-01;;
    06) expires=$year-08-01;;
    07) expires=$year-09-01;;
    08) expires=$year-10-01;;
    09) expires=$year-11-01;;
    10) expires=$year-12-01;;
    11) expires=$next_year-01-01;;
    12) expires=$next_year-02-01;;
  esac
  nickname="daily"
  version="$VERSION-$date"
fi

echo "Creating ddd-$version.tar.gz$expires_msg..."

# Go ahead...
if [ "$expires" = "" ]; then
  expires_msg=""
else
  expires_msg=" (expires $expires)"
fi

if $final; then
  :
else
# Override the ROTD version.
sedscript="s/AC_INIT(\[ddd\],\[.*\]/AC_INIT([ddd],[$version]/;\
s/EXPIRES=.*$/EXPIRES=$expires/;\
s/NICKNAME=\".*\"/NICKNAME=\"$nickname\"/"

sed "$sedscript" configure.ac > configure.ac~
mv configure.ac~ configure.ac
fi

# Set up configure scripts.
echo "autoreconf -ifv"
autoreconf -ifv &> autoreconf.log

# Configure the package.
echo "./configure -v"
sh ./configure -v &> configure.log

# Create distribution.
echo "make dist"
make dist &> make.log 

if [ -f ddd-$version.tar.gz ]; then

  # Move dist file into target area
  rm -f $target/ddd-*.tar.gz
  mv ddd-*.tar.gz $target

  echo "Creating ddd-$version.tar.gz$expires_msg...done."
  echo "make-current[$$]: done at `date`"
else
  echo "make-current[$$]: failed at `date`"
fi

# That's all, folks!
exit 0
