#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=python2
PKGVERSION=2.7.16
PKGREVISION=1
PKGLICENSE="PSF"
PKGSUMMARY="high-level scripting language"
PKGDOWNLOAD="https://www.python.org/ftp/python/$PKGVERSION/Python-$PKGVERSION.tar.xz"
PKGDEPENDS="libffi expat zlib libressl"
PKGOPTIONS="IPV6"
PKGTAGS="DEPRECATED"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CTARGET:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${IPV6:=yes}

if [ "$IPV6" = "yes" ]; then
	with_ipv6="--enable-ipv6"
else
	with_ipv6="--disable-ipv6"
fi

rm -rf Python-$PKGVERSION
tar xvf Python-$PKGVERSION.tar.xz
cd Python-$PKGVERSION

CC="$CC" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
./configure \
	--build=$CBUILD \
	--host=$CHOST \
	--target=$CTARGET \
	--prefix=/. \
	--enable-shared \
	--with-system-expat \
	--with-system-ffi \
	$with_ipv6
make
make DESTDIR=$DESTDIR altinstall maninstall
ln -sf python2.7 $DESTDIR/bin/python2

# Avoid conflicts with Python 3.
rm -f $DESTDIR/bin/2to3
mv $DESTDIR/bin/smtpd.py $DESTDIR/bin/smtpd.py2.7
mv $DESTDIR/bin/idle $DESTDIR/bin/idle2.7
mv $DESTDIR/bin/pydoc $DESTDIR/bin/pydoc2.7

cd ..
rm -rf Python-$PKGVERSION
