Blob Blame History Raw
#!/bin/sh

# Copyright (C) 2008-2009 Red Hat, Inc
# Written by Jens Petersen <petersen@redhat.com>, 2008.
#
# 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

[ $# -ne 1 -o ! -r "$1" ] && echo "Usage: $(basename $0) [hackage.tar.gz|hackage.cabal]" && exit 1

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 ;;
esac

NAME=$(grep -i ^name: $CABAL | sed -e "s/[Nn]ame:[ \t]*//")
if [ -n "$TARNAME" -a "$TARNAME" != "$NAME" ]; then
  echo "Warning: tarball name ($TARNAME) and cabal name ($NAME) differ!"
fi

VERSION=$(grep -i ^version: $CABAL | sed -e "s/[Vv]ersion:[ \t]*//")
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


if grep -qi exposed-modules: $CABAL; then
  HAS_LIB=yes
fi

if grep -qi executable $CABAL; then
  HAS_BIN=yes
fi

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

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

SPECFILE=$PREFIX$NAME.spec

[ -r "$SPECFILE" ] && echo "$SPECFILE already exists!" && exit 1

cp /usr/share/ghc/cabal-${HAS_BIN:+bin}${HAS_LIB:+lib}-template.spec.in $SPECFILE

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

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

sed -i -e "s/@PACKAGE@/$NAME/" -e "s/@GHC_VERSION@/$(ghc --numeric-version)/" -e "s/@VERSION@/$VERSION/" -e "s/@DATE@/$DATE/" $SPECFILE