#!/bin/bash

# This script makes and installs a Gentoo ebuild for this package.

# Make a tar ball, `make dist', before running this.  You need to run
# this as root.

# Then you can run:  emerge quickplot


########################################################################
# This block was taken from Daniel Elstner's autogen.sh.
########################################################################
dir=`echo "$0" | sed 's,[^/]*$,,'`
test "x${dir}" = "x" && dir='.'
if test "x`cd "${dir}" 2>/dev/null && pwd`" != "x`pwd`"
then
    echo "This script ($0) must be executed directly \
from the top $0 source directory."
    exit 1
fi
# end taken from Daniel Elstner
########################################################################

if [ `id -u` != "0" ]
then
  echo "you must run $0 as user root"
  exit 1
fi

if [ ! -x configure ]
then
  echo "run \`./bootstrap', \`./configure' and \`make dist' first."
  exit 1
fi

if [ ! -e VERSION ]
then
  echo "run \`./configure' and \`make dist' first."
  exit 1
fi

. /etc/make.conf || exit 1

VERSION="`cat VERSION`"
TARNAME=quickplot
DISTDIR=/usr/portage/distfiles

if [ "${PORTDIR_OVERLAY}" != "" ]
then
  PORTAGE_DIR=/usr/local/portage
else
  PORTAGE_DIR="${PORTDIR_OVERLAY}"
fi

EBUILD_DIR=${PORTAGE_DIR}/sci-visualization/quickplot

if [ ! -f ${TARNAME}-${VERSION}.tar.bz2 ]
then
  echo "run \`make dist' first."
  exit 1
fi

script="$0"
error=0

# This function, run(), just helps by adding more spew for debugging
# when running programs.  If any thing fails to run it stops running
# programs and spews what it would like to run.
run()
{
  if [ $error = 0 ]
  then
    echo "$script RUNNING: $@"
    if ! $@
    then
      echo
      echo "-------------------------- ERROR ---------------------------"
      echo "$script had an error while running: $@"
      echo "------------------------------------------------------------"
      error=1
    fi
  else
    echo "+++++ $script DID NOT RUN: $@"
  fi
}


run mkdir -p ${EBUILD_DIR}
run rm -f ${EBUILD_DIR}/files/digest-quickplot-${VERSION}\
 ${EBUILD_DIR}/MANIFEST ${EBUILD_DIR}/ChangeLog ${EBUILD_DIR}/${TARNAME}-${VERSION}.ebuild
run cp ${TARNAME}.ebuild ${EBUILD_DIR}/${TARNAME}-${VERSION}.ebuild
run cp ChangeLog ${EBUILD_DIR}/ChangeLog
run cp ${TARNAME}-${VERSION}.tar.bz2 ${DISTDIR}/
run ebuild ${EBUILD_DIR}/${TARNAME}-${VERSION}.ebuild manifest


# final spew
if [ $error = 0 ]
then
   echo "$script ran successfully"
else
   echo "$script did not run successfully"
fi

echo "You can now run: emerge quickplot"

exit $error


