#!/usr/bin/env bash
#
# Script to prepare a PARI spkg for Sage.  This script is only for the
# package maintainer, not for building PARI during a Sage install.
# WARNING: This script will delete/overwrite files in this directory
# and its subdirectories!
#
# HOW TO MAKE THE SPKG:
# 1) ./spkg-src
# 2) rm -rf parigit    (For final distribution, not necessary for testing)
# 3) cd ..; sage --spkg pari-2.5.1.whatever_the_name_of_the_package_is
#
# If something goes wrong, try rm -rf parigit and try again.
#
# This script does the following:
# * If the directory parigit/ exists, it is assumed to be a git
#   repository of the PARI sources.  If it does not exist, the git
#   sources are automatically downloaded.  In any case, a specific
#   version is checked out.
# * If the file parigit/galdata.tgz does not exist, it is downloaded.
# * The same for the file parigit/seadata-small.tgz
# * PARI is configured and built in the parigit directory.  The built
#   executables are not needed, but some files generated in the
#   process (using bison for example) are needed.
# * The necessary files are copied from parigit/ to src/ using a
#   mechanism very similar to "make snapshot" from PARI.
# * The files parigit/galdata.tgz and parigit/seadata-small.tgz are
#   extracted.
#
# AUTHOR:
#
# - Jeroen Demeyer (July 2010): initial version
#
# - Jeroen Demeyer (February 2012): use git instead of svn, upgrade to PARI 2.5.1

# Automatically exit on errors
set -e


# Sanity check: must be run from current directory
if ! [ -f spkg-src ]; then
    echo >&2 "This script must be run from its own source directory!"
    exit 1
fi


# Clean an existing src/ directory
rm -rf src
mkdir src


# Download or update git sources in parigit/
if [ -d parigit ]; then
    cd parigit
    git fetch
else
    git clone http://pari.math.u-bordeaux.fr/git/pari.git parigit
    cd parigit
fi

# Download galdata.tgz and seadata-small.tgz
wget --timestamping http://pari.math.u-bordeaux.fr/pub/pari/packages/galdata.tgz
wget --timestamping http://pari.math.u-bordeaux.fr/pub/pari/packages/seadata-small.tgz


# Use the specified git version
git checkout pari-2.5.5


# make pari (must run bison, etc...) but disable optimization to speed
# up compilation (the output of the compiling is not used anyway).
env CFLAGS="-O0" ./Configure
make gp doc


# Copy the needed files to src using a tar pipe.  This is based on code
# from config/settar.
#
# Add src/desc/pari.desc to remove build-time dependency on perl
# -- Jeroen Demeyer
tar -c -T <( config/get_MANIFEST && echo src/desc/pari.desc ) | ( cd ../src && tar xv )

# Hardcode version number in config/version.
vcsversion=git-`git log -1 --pretty=format:%h`
sed "s/^vcsversion=$/vcsversion=$vcsversion    # hardcoded by spkg-src/" <config/version >../src/config/version


# Extract the tgz files
for tgzfile in galdata.tgz seadata-small.tgz; do
    ( cd ../src && tar xvz ) < "$tgzfile"
done


# We're done!
cd ..
echo "======================================================"
echo "Done downloading PARI and creating the src/ directory."
echo "If everything looks okay, proceed as follows:"
echo "  rm -rf parigit"
echo "  cd .."
echo -n "  sage --spkg "; basename `pwd`
