#!/bin/bash
# This script builds a maintainance LyX distribution according to 
# the procedure outlined at:
#   http://wiki.lyx.org/Devel/ReleaseProcedure
# It also includes several other tests, to make sure the packages
# works as it should.

# A few variables need to be set, here at the top. 
#
# Where we will do our work
BASE="/cvs/lyx/lyx-release";
# Where our git repository lives
SRCDIR="/cvs/lyx/lyx-20";
# editor 
if [ -z "$EDITOR" ]; then EDITOR=vi; fi
# Options to make, when we compile
MAKEOPTS="-j4";

# Determine LyX version
cd $SRCDIR/
VERSION=$(head configure.ac | grep AC_INIT | perl -e 'while (<>) {m/AC_INIT\(LyX,([^,]+)/; print $1;}');

echo "This is version $VERSION.";
echo -n "Ready to build source packages...";
read

echo "Exporting clean tree...";
rm -Rf $BASE/lyx-export/
git checkout-index -a -f --prefix=$BASE/lyx-export/
cd $BASE/lyx-export/
./autogen.sh
rm -Rf $BASE/lyx-build/
mkdir $BASE/lyx-build/
cd $BASE/lyx-build/

echo "Building distribution...";
$BASE/lyx-export/configure --enable-build-type=rel
if ! make lyxdist; then
  echo "Couldn't make distribution!";
  exit 1;
fi

echo "Packages created:";
cp -v lyx-$VERSION.tar.{gz,xz} $BASE;

echo -n "Ready to build signatures...";
read

gpg -b lyx-$VERSION.tar.gz
gpg -b lyx-$VERSION.tar.xz

echo "Signatures created:"
cp -v lyx-$VERSION.tar.*.sig $BASE;

echo -n "Ready to test compilation...";
read

rm -Rf $BASE/lyx-test/
mkdir $BASE/lyx-test/
cd $BASE/lyx-test/
tar -zxvf $BASE/lyx-build/lyx-$VERSION.tar.gz >/dev/null
if ! cd lyx-$VERSION; then
  echo "Unable to enter build directory!";
  exit 1;
fi

./configure --enable-build-type=rel

if make $MAKEOPTS; then
  echo "Compilation complete.";
  echo -n "Ready to run LyX...";
  read
  src/lyx -userdir /tmp/lyx-test
else
  echo "Compilation errors!!";
  exit 1;
fi

LASTNUM=$(echo $VERSION | sed -e 's/.*\.//');
LAST=$(($LASTNUM - 1));
FIRST=$(echo $VERSION | sed -e 's/[0-9]*$//');
ORIGINAL=${FIRST}0;
LAST=$FIRST$LAST;

if [ ! -d "$BASE/lyx-patch/" ]; then
	mkdir "$BASE/lyx-patch/" || exit 1;
fi

if [ ! -d $BASE/lyx-patch/lyx-$LAST ]; then 
  echo "Can't find directory for last version $LAST.";
  echo "See if you can fix this in $BASE/lyx-patch/.";
  echo "Try that, if you like, and then we'll continue.";
  echo "We'll try to download from the LyX site if that does not work.";
  read;

  if [ ! -d $BASE/lyx-patch/lyx-$LAST ]; then 
    echo "Will try to download from LyX site....";
    pushd $BASE/lyx-patch/;
    wget ftp://ftp.lyx.org/pub/lyx/stable/${FIRST}x/lyx-$LAST.tar.gz;
    tar -zxvf lyx-$LAST.tar.gz;
    if [ ! -f lyx-$LAST.tar.gz ]; then
      echo "Still unable to find directory for last version $LAST.";
      exit 1;
    fi
    popd;
  fi
fi

echo -n "Ready to make patch against $LAST...";
read

cd $BASE/lyx-patch/;
tar -zxvf $BASE/lyx-build/lyx-$VERSION.tar.gz >/dev/null;

diff -urN -x .svn -x version.cpp lyx-$LAST lyx-$VERSION > patch

echo -n "Please check the patch...";
read
$EDITOR patch;

NUMFIX="th";
if [ "$LASTNUM" = "1" ]; then
  NUMFIX="st";
elif [ "$LASTNUM" = "2" ]; then
  NUMFIX="nd";
fi
NUM="$LASTNUM$NUMFIX";
cat $BASE/lyx-export/development/tools/patch-preamble | \
sed -e "s/VERSION/$VERSION/; s/ORIGINAL/$ORIGINAL/; s/LAST/$LAST/; s/NUM/$NUM/;" >patch-preamble;
echo -n "Please verify the patch preamble...";
read
$EDITOR patch-preamble;

PATCH="patch-$VERSION";
cat patch-preamble $BASE/lyx-export/ANNOUNCE patch >$PATCH;
gzip -c $PATCH > $PATCH.gz
if [ -f $PATCH.gz.sig ]; then
  rm $PATCH.gz.sig;
fi
gpg -b $PATCH.gz
xz -zc $PATCH > $PATCH.xz
if [ -f $PATCH.xz.sig ]; then
	rm $PATCH.xz.sig;
fi
gpg -b $PATCH.xz

echo -n "Patch and signatures created...";
cp -v $PATCH.gz $PATCH.gz.sig $PATCH.xz $PATCH.xz.sig $BASE;