#!/bin/sh

# Copyright (C) 2008-2011 Red Hat, Inc
# Written by Jens Petersen <petersen@redhat.com>
#
# This program 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 2, or (at your option)
# any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

set -e +x

# use -l to force lib package
if getopts l opt; then
  shift
  case $opt in
      l) FORCE_LIB=yes
	  ;;
  esac
fi

if [ $# -ne 1 -o ! -r "$1" ]; then
    echo "Usage: $(basename $0) [-l] [hackage.tar.gz|hackage.cabal]"
    echo "  -l      assume BinLib package is Lib"
    exit 1
fi

FILE=$1

case $FILE in
    *.tar.gz)
	TARNAME_VER=$(basename $FILE .tar.gz)
	TARVERSION=$(echo $TARNAME_VER | sed -e "s/.*-//")
	TARNAME=$(echo $TARNAME_VER | sed -e "s/-$TARVERSION//")
	WORKDIR=$(mktemp -d)
	tar zxf $FILE -C $WORKDIR "*.cabal" 
	CABAL="$WORKDIR/*/*.cabal" ;;
    *.cabal)
	CABAL=$FILE ;;
    *)
	echo "unsupported file format"
	exit 1
esac

cabal_field () {
    sed -n -e "s/^$1[ \t]*:[ \t]*\([^ \t]*\)[\r$ \t]\+/\1/Ip" $2
}

NAME=$(cabal_field name $CABAL)
if [ -n "$TARNAME" -a "$TARNAME" != "$NAME" ]; then
  echo "Warning: tarball name ($TARNAME) and cabal name ($NAME) differ!"
fi

#VERSION=$(grep -E -i "^version[ \t]*:" $CABAL | sed -e "s/[Vv]ersion[ \t]*:[ \t]*//" -e "s/
//")
VERSION=$(cabal_field version $CABAL)
if [ -n "$TARVERSION" -a "$TARVERSION" != "$VERSION" ]; then
  echo "Warning: tarball version ($TARVERSION) and cabal version ($VERSION) differ!"
fi

CABALFILENAME=$(basename $CABAL .cabal)
if [ "$CABALFILENAME" != "$NAME" ]; then
    echo "Warning: .cabal filename ($CABALFILENAME) and cabal Name field ($NAME) differ!"
fi

CABAL_LICENSE=$(cabal_field license $CABAL)

if grep -v ^-- $CABAL | grep -qi exposed-modules; then
    HAS_LIB=yes
fi

if [ "$FORCE_LIB" -a ! "$HAS_LIB" ]; then
    echo "Cannot force Bin package to Lib!"
    exit 1
fi

if [ ! "$FORCE_LIB" ] && grep -v ^-- $CABAL | grep -i ^executable | grep -qi -v test; then
    HAS_BIN=yes
fi

#CABAL_DEPENDS=$(cabal_field "[ \t]*build-depends" $CABAL | sed -e "s/&& \+//g")

[ -d "$WORKDIR" ] && rm -r $WORKDIR

if [ "$HAS_LIB" -a ! "$HAS_BIN" ]; then
    PREFIX=ghc-
fi

SPECFILE=$PREFIX$NAME.spec

[ -r "$SPECFILE" ] && SPECFILE=$SPECFILE.cabal2spec

TEMPLATE=spectemplate-ghc-${HAS_BIN:+bin}${HAS_LIB:+lib}.spec

RUNDIR=$(dirname $0)
if [ -r "$RUNDIR/$TEMPLATE" ]; then
  TEMPLATEDIR=$RUNDIR
  CABAL2SPEC_VERSION=$(make -s -C $RUNDIR version)
  MAJOR_VERSION=$(make -s -C $RUNDIR majorver)
else
  TEMPLATEDIR=/etc/rpmdevtools
  CABAL2SPEC_VERSION="0.25.5"
  MAJOR_VERSION="0.25"
fi

DATE=$(env TZ=UTC LANG=C date +"%a %b %e %Y")

case $CABAL_LICENSE in
    BSD3) LICENSE=BSD ;;
    *) LICENSE=$CABAL_LICENSE ;;
esac

sed -e "
s/@PACKAGE@/$NAME/g
s/@VERSION@/$VERSION/g
s/@LICENSE@/$LICENSE/
s/@DATE@/$DATE/
s/@CABAL2SPEC_VERSION@/$CABAL2SPEC_VERSION/
s/@MAJOR_VERSION@/$MAJOR_VERSION/
" ${TEMPLATEDIR}/${TEMPLATE} > $SPECFILE

#echo "created $SPECFILE (${$FORCE_LIB:-${HAS_BIN:+bin}}${HAS_LIB:+lib}) for $NAME-$VERSION"
