0682b80
#!/bin/sh
0682b80
e74c429
# Copyright (C) 2008-2009 Red Hat, Inc
e74c429
# Written by Jens Petersen <petersen@redhat.com>, 2008.
e74c429
#
e74c429
# This program is free software; you can redistribute it and/or modify
e74c429
# it under the terms of the GNU General Public License as published by
e74c429
# the Free Software Foundation; either version 2, or (at your option)
e74c429
# any later version.
e74c429
#
e74c429
# This program is distributed in the hope that it will be useful,
e74c429
# but WITHOUT ANY WARRANTY; without even the implied warranty of
e74c429
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e74c429
# GNU General Public License for more details.
e74c429
#
e74c429
# You should have received a copy of the GNU General Public License
e74c429
# along with this program; if not, write to the Free Software Foundation,
e74c429
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
e74c429
b17255a
set -e
0682b80
74a3f25
[ $# -ne 1 -o ! -r "$1" ] && echo "Usage: $(basename $0) [hackage.tar.gz|hackage.cabal]" && exit 1
74a3f25
74a3f25
FILE=$1
74a3f25
74a3f25
case $FILE in
74a3f25
    *.tar.gz)
74a3f25
	TARNAME_VER=$(basename $FILE .tar.gz)
74a3f25
	TARVERSION=$(echo $TARNAME_VER | sed -e "s/.*-//")
74a3f25
	TARNAME=$(echo $TARNAME_VER | sed -e "s/-$TARVERSION//")
74a3f25
	WORKDIR=$(mktemp -d)
74a3f25
	tar zxf $FILE -C $WORKDIR "*.cabal" 
74a3f25
	CABAL="$WORKDIR/*/*.cabal" ;;
74a3f25
    *.cabal)
74a3f25
	CABAL=$FILE ;;
74a3f25
esac
74a3f25
74a3f25
NAME=$(grep -i ^name: $CABAL | sed -e "s/[Nn]ame:[ \t]*//")
74a3f25
if [ -n "$TARNAME" -a "$TARNAME" != "$NAME" ]; then
74a3f25
  echo "Warning: tarball name ($TARNAME) and cabal name ($NAME) differ!"
74a3f25
fi
0682b80
74a3f25
VERSION=$(grep -i ^version: $CABAL | sed -e "s/[Vv]ersion:[ \t]*//")
74a3f25
if [ -n "$TARVERSION" -a "$TARVERSION" != "$VERSION" ]; then
74a3f25
  echo "Warning: tarball version ($TARVERSION) and cabal version ($VERSION) differ!"
74a3f25
fi
0682b80
74a3f25
CABALFILENAME=$(basename $CABAL .cabal)
74a3f25
if [ "$CABALFILENAME" != "$NAME" ]; then
74a3f25
  echo "Warning: .cabal filename ($CABALFILENAME) and cabal Name field ($NAME) differ!"
74a3f25
fi
0682b80
b17255a
74a3f25
if grep -qi exposed-modules: $CABAL; then
b17255a
  HAS_LIB=yes
b17255a
fi
b17255a
74a3f25
if grep -qi executable $CABAL; then
b17255a
  HAS_BIN=yes
b17255a
fi
74a3f25
74a3f25
[ -d "$WORKDIR" ] && rm -r $WORKDIR
b17255a
b17255a
if [ "$HAS_LIB" -a ! "$HAS_BIN" ]; then
b17255a
  PREFIX=ghc-
b17255a
fi
b17255a
b17255a
SPECFILE=$PREFIX$NAME.spec
b17255a
9c20304
[ -r "$SPECFILE" ] && echo "$SPECFILE already exists!" && exit 1
b17255a
b17255a
cp /usr/share/ghc/cabal-${HAS_BIN:+bin}${HAS_LIB:+lib}-template.spec.in $SPECFILE
0682b80
74a3f25
echo "created $SPECFILE (${HAS_BIN:+bin}${HAS_LIB:+lib}) for $NAME-$VERSION"
0682b80
b17255a
DATE=$(env LANG=C date +"%a %b %e %Y")
0682b80
b17255a
sed -i -e "s/@PACKAGE@/$NAME/" -e "s/@GHC_VERSION@/$(ghc --numeric-version)/" -e "s/@VERSION@/$VERSION/" -e "s/@DATE@/$DATE/" $SPECFILE