#! /bin/sh  -x
#
# GCC release generation script.
#
# Originally written by Jeff Law <law@redhat.com>.

# We need separate variables for the tag in the CVS sources and the
# name of the release directory.  Consider if we have a packaging problem
# and need to respin the release.  The sources do not change, but the release
# name changes.
#
# [[ Not clear.  You  can purge the old tag and make a new one, too.
#    That way they continue to match.  Maximize consistency. ]]
#
# We used to use a tag to get the previous release, but it is more reliable to
# actually unpack the previous release from the ftp directory.  Consider files
# like .brik which are in the release, but not in the repository.
#
if test $# != 6 ; then
  echo "usage: release release-tag release-name release_branch last-release-name prerelease dotag"
  exit 1
fi

release_tag=$1
release_name=$2
release_suffix=`echo $release_name | sed -e s#gcc-##`
release_branch=$3
last_release_name=$4
last_release_suffix=`echo $last_release_name | sed -e s#gcc-##`
prerelease=$5
dotag=$6

case $last_release_name in
  egcs-1.0*)
    echo "unprotoize.c and README.gcc need special handling"
    exit 1;
    ;;
  egcs-1.1*)
    echo "No java or chill in egcs-1.1*"
    ;;
  
  gcc-2.95*)
    echo "No testsuites in gcc-2.95*"
    ;;
esac

STATEDIR=${STATEDIR-/home/law}
GCCBASE=${GCCBASE-${STATEDIR}/gcc}
TMPDIR=${TMPDIR-/var/tmp}
export TMPDIR STATEDIR GCCBASE

PATH=${GCCBASE}:$PATH

[ -f ${GCCBASE}/gcc.pot ] || {
  echo "'${GCCBASE}/gcc.pot' is missing"
  exit 1
}

[ -f ${STATEDIR}/bin/brik ] || {
  echo "'${STATEDIR}/bin/brik' is missing"
  exit 1
}

# We run this on the cvs server itself so we can get at the cvs
# files quickly.  It also assumes access to my home dir for some
# programs.
CVSROOT=/cvs/gcc
export CVSROOT

# Get somewhere useful with a lot of disk space.
cd $TMPDIR

# Get into a temporary directory so as not to be confused by
# any existing CVS administration files.
rm -rf $$
mkdir $$
cd $$

if test $prerelease = no -a $dotag = yes; then
  update_changelogs $release_name $release_branch
fi

# If requested, tag the branch.   Three steps, first remove the old tag from
# the repository, then check out the release branch, then retag the release
# branch.
if test $dotag = yes; then
  rm -rf gcc
  cvs -Q rtag -d $release_tag gcc
  cvs -Q co -r $release_branch gcc
  cvs -Q tag -f $release_tag
  rm -rf gcc
fi

# Get a copy of the previous release.  Get it from the ftp dir to avoid minor
# issues with files like .brik not being in the repository.
if test $prerelease = yes; then
  echo "Yow!"
  exit 1;
  zcat ~ftp/pub/gcc/snapshots/egcs-1.1.2-prerelease/$last_release_name.tar.gz | tar xf -
else
  zcat ~ftp/pub/gcc/releases/$last_release_name/$last_release_name.tar.gz | tar xf -
fi

# Files/directories which are not part of the gcc-core module.
noncore_files="gcc/cp libio libstdc++ gcc/f libf2c gcc/objc libobjc gcc/ch libchill gcc/java libjava gcc/testsuite"

# Get the gcc core.  Note that the gcc-core module doesn't work yet, so
# we get the whole directory, then prune the things we do not want.
#
# Also, get the latest copy of the installation instructions and the
# faq in html, build plaintext versions of those documents.
cvs -Q export -ko -r$release_tag gcc
cd gcc
contrib/gcc_update --touch
cp ${GCCBASE}/gcc.pot gcc/po
cd ..
cvs -Q export -ko -Dnow wwwdocs/htdocs/install
cvs -Q export -ko -Dnow wwwdocs/htdocs/faq.html
#cvs -Q export -ko -Dnow wwwdocs/htdocs/bugs.html
mv gcc $release_name
release_docs $release_name $$

# Build a list of files in this release.
cd $release_name
find . -type f -print | sort > ../files_$release_name
${STATEDIR}/bin/brik -Gb -f ../files_$release_name > ../.brik_$release_name
cp ../.brik_$release_name ./.brik

# Get back to the directory with the two releases in it.
cd ..

# Remove anything that is not part of the core
for file in $noncore_files; do
  rm -rf $last_release_name/$file
  rm -rf $release_name/$file
done

# Make the core tarball
tar cf - $release_name | gzip --best > gcc-core-$release_suffix.tar.gz

# make empty directories for directories that disappeared so that
# patch will delete any files in those directories when patching up to
# this release
case $last_release_name in
  gcc-2.95*)
    mkdir $release_name/xiberty
    mkdir $release_name/contrib/fixinc
    ;;
esac
	
# Diff the core modules
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name $release_name > gcc-core-$last_release_suffix-$release_suffix.diff

# Remove the core gunk
rm -rf $last_release_name
rm -rf $release_name


# Go back and get the full distributions.
if test $prerelease = yes; then
  zcat ~ftp/pub/gcc/snapshots/egcs-1.1.2-prerelease/$last_release_name.tar.gz | tar xf -
else
  zcat ~ftp/pub/gcc/releases/$last_release_name/$last_release_name.tar.gz | tar xf -
fi

cvs -Q export -ko -r$release_tag gcc
cd gcc
contrib/gcc_update --touch
cp ${GCCBASE}/gcc.pot gcc/po
cd ..
cvs -Q export -ko -Dnow wwwdocs/htdocs/install
cvs -Q export -ko -Dnow wwwdocs/htdocs/faq.html
#cvs -Q export -ko -Dnow wwwdocs/htdocs/bugs.html
mv gcc $release_name
find $release_name/gcc/testsuite -type f -print | xargs rm -f
release_docs $release_name $$


# No need to regenerate the .brik file.  We didn't delete it.
cp .brik_$release_name $release_name/.brik

# We don't need this anymore.
rm .brik_$release_name
rm .brik_$last_release_name

# Make tarballs

tar cf - $release_name/.brik $release_name/gcc/f $release_name/libf2c | gzip --best > gcc-g77-$release_suffix.tar.gz
tar cf - $release_name/.brik $release_name/gcc/java | gzip --best > gcc-java-$release_suffix.tar.gz
tar cf - $release_name/.brik $release_name/gcc/ch $release_name/libchill | gzip --best > gcc-chill-$release_suffix.tar.gz
#tar cf - $release_name/.brik $release_name/gcc/testsuite/g77.f-torture | gzip --best > gcc-g77-testsuite-$release_suffix.tar.gz
tar cf - $release_name/.brik $release_name/gcc/objc $release_name/libobjc | gzip --best > gcc-objc-$release_suffix.tar.gz
tar cf - $release_name/.brik $release_name/gcc/cp $release_name/libio $release_name/libstdc++ | gzip --best > gcc-g++-$release_suffix.tar.gz
#tar cf - $release_name/.brik $release_name/gcc/testsuite/g++.old-deja | gzip --best > gcc-g++-tests-$release_suffix.tar.gz
#tar cf - $release_name/.brik $release_name/gcc/testsuite | gzip --best > gcc-tests-$release_suffix.tar.gz
tar cf - $release_name | gzip --best > $release_name.tar.gz
# make empty directories for directories that disappeared so that
# patch will delete any files in those directories when patching up to
# this release
# make empty directories for directories that disappeared so that
# patch will delete any files in those directories when patching up to
# this release
case $last_release_name in
  gcc-2.95*)
    mkdir $release_name/xiberty
    mkdir $release_name/contrib/fixinc
    ;;
esac
	
# Now make diffs for g77, g77-testsuite objc, g++, g++-testsuite, testsuite,
# then the whole bloody thing.  Add libobjc, java & chill for next major
#release.
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/f $release_name/gcc/f > gcc-g77-$last_release_suffix-$release_suffix.diff
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libf2c $release_name/libf2c >> gcc-g77-$last_release_suffix-$release_suffix.diff

#diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/testsuite/g77.f-torture  $release_name/gcc/testsuite/g77.f-torture > gcc-g77-testsuite-$last_release_suffix-$release_suffix.diff

diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/ch $release_name/gcc/ch > gcc-chill-$last_release_suffix-$release_suffix.diff
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libchill $release_name/libchill >> gcc-chill-$last_release_suffix-$release_suffix.diff

diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/java $release_name/gcc/java > gcc-java-$last_release_suffix-$release_suffix.diff
#diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libf2c $release_name/libf2c >> gcc-java-$last_release_suffix-$release_suffix.diff

diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/objc $release_name/gcc/objc > gcc-objc-$last_release_suffix-$release_suffix.diff
mkdir $last_release_name/libobjc
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libobjc $release_name/libobjc >> gcc-objc-$last_release_suffix-$release_suffix.diff

diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/cp $release_name/gcc/cp > gcc-g++-$last_release_suffix-$release_suffix.diff
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libio $release_name/libio >> gcc-g++-$last_release_suffix-$release_suffix.diff
diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/libstdc++ $release_name/libstdc++ >> gcc-g++-$last_release_suffix-$release_suffix.diff

#diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/testsuite/g++.old-deja $release_name/gcc/testsuite/g++.old-deja > gcc-g++-tests-$last_release_suffix-$release_suffix.diff

#diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name/gcc/testsuite $release_name/gcc/testsuite > gcc-tests-$last_release_suffix-$release_suffix.diff

diff -x de.gmo -x fr.gmo -Nrc3p $last_release_name $release_name > gcc-$last_release_suffix-$release_suffix.diff


# Remove the old snapshot, we're done with it.
rm -rf $last_release_name


# Compress the diff files.
gzip --best *.diff

# bzip everything too

# Copy them to the FTP server XXX

# Update links on the ftp server XXX

# Announce it XXX