#!/bin/bash

# Generic python SlackBuild

# variables
TAG=${TAG:-pip3_gwh}
TMP=/tmp/$TAG
CWD=$(pwd)
OUTPUT=/tmp

PRGNAM=${PRGNAM:-$(basename $CWD)}
PKG=$TMP/pkg-$PRGNAM

VERSION=${VERSION:-"pip"}

ARCH=$(uname -m)
BUILD=1

# nettoyage préalable
rm -fr $PKG
mkdir -p $PKG

# installation
EXTRA=""
[ "x$VERSION" == "xpip" ] || EXTRA="==$VERSION"
pip3 install --ignore-installed --root $PKG --no-deps $PRGNAM$EXTRA

if [ -d $PKG/usr/share/man ]; then
    mv $PKG/usr/share/man $PKG/usr/
    find $PKG/usr/man -type f -not -name \*.gz -exec gzip -9 {} \;
fi

METADATA_FILE=$(find $PKG -name METADATA)

[ ! -e $METADATA_FILE ] && exit 1
VERSION=$(grep "^Version:" $METADATA_FILE | sed 's|^Version: ||')
SUMMARY=$(grep "^Summary:" $METADATA_FILE | sed 's|^Summary: ||')
HOMEPAGE=$(grep "^Home-page:" $METADATA_FILE | sed 's|^Home-page: ||')

# correction
cd $PKG
chown -R root:root *

# embaumement
mkdir -p $PKG/install
cat <<EOF > $PKG/install/slack-desc
$PRGNAM: $PRGNAM ($SUMMARY)
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM:
$PRGNAM: $HOMEPAGE
$PRGNAM:
EOF

# empaquetage
cd $PKG
rm -f $PKG/{,usr/}lib$(uname -m | grep -o 64)/*.la
/sbin/makepkg --linkadd y --chown n --prepend $OUTPUT/python3-$PRGNAM-$(echo $VERSION | sed 's/-//g')-$ARCH-$BUILD$TAG.txz