# SliTaz package receipt.

PACKED_SIZE="172.0K"
UNPACKED_SIZE="1.1M"
PACKAGE="tazpkg"
VERSION="5.6"
_realver="979"
CATEGORY="base-system"
SHORT_DESC="SliTaz packages manager"
MAINTAINER="pankso@slitaz.org"
LICENSE="GPL3"
WEB_SITE="http://www.slitaz.org/"
TARBALL="$PACKAGE-$_realver.tar.gz"
WGET_URL="http://hg.slitaz.org/tazpkg/archive/$_realver.tar.gz"
TAGS="slitaz package-manager"
HOST_ARCH="i486 arm x86_64"

DEPENDS="busybox gettext-base"
SUGGESTED="tazpanel"
BUILD_DEPENDS="gettext"

# Gettext is part of cross-chroot
case "$ARCH" in
	arm)  BUILD_DEPENDS="" ;;
esac

current_version()
{
	wget -O - http://hg.slitaz.org/$PACKAGE/ 2>/dev/null | \
	sed '/^Changeset/!d;s|.*">|http://hg.slitaz.org/'$PACKAGE'/rev/|;s|<.*||' | \
	xargs wget -O - 2>/dev/null | sed '/rev /!d;s|.*rev ||;s| .*||'
}

# Rules to gen a SliTaz package suitable for TazPkg.
#
# TazPkg is SliTaz packages manager. On an other GNU/Linux install
# can be do with `make install` from the sources directory.
#

# Rules to configure and make the package.
compile_rules()
{
        # Thanks Rantanplan, fix miss translate in tazpkg.fr.html
        patch -p1 < $stuff/tazpkg-doc-fr.patch

        # Patch for upgrade core system package first
        patch -p1 < $stuff/upgrade-core-pkg.patch

	# Add getall, checkspace feature
	cp -a $stuff/checkspace $stuff/getall modules/
	patch -p1 < $stuff/checkspace-module.patch
	patch -p1 < $stuff/getall-module.patch

	# Patch for upgrade tazpkg first
	patch -p1 < $stuff/upgrade-tazpkg-first.patch

	# Patch to improve package managment
	patch -p1 < $stuff/tazpkg-upgradelock.patch

	# Ensure we don't add .orig / .rej
	rm -rf modules/*.orig modules/*.rej

	# Disable libnotify dependency
	sed -i "s|^all: msgfmt notify$|all: msgfmt|" Makefile
	sed -i "/tazpkg-box tazpkg-notify/s/ tazpkg-notify//" Makefile
	sed -i "s|install -m 0755 build/tazpkg-notify|#install -m 0755 build/tazpkg-notify|" Makefile
	sed -i "s|install -m 0755 tazpkg-notification|#install -m 0755 tazpkg-notification|" Makefile

	# Enable it locale
	sed -i 's/fr ja/fr it ja/' Makefile
	cp $stuff/it.po po

	sed -i 's|\. receipt|. ./receipt|' */*
	make && make DESTDIR=$DESTDIR VERSION=$VERSION install
}

genpkg_rules()
{
	mkdir -p $fs/etc/init.d
	cp -a $stuff/check_tazpkgupg.sh $fs/etc/init.d

	cp -a $install/* $fs
	chown -R root:root $fs

	# A tiny bug fix
	sed 's|= "\$NEWVER|= "${NEWVER:-$VERSION}|;s|\$NEWMD5|${NEWMD5:-$OLDMD5}|' \
		-i $fs/usr/libexec/tazpkg/info
	# Default icon for mimetype (freedesktop standard compliant file
	# manager will diplay a tazpkg icon for SliTaz packages).
	icons="$fs/usr/share/icons/hicolor/32x32/mimetypes"
	mkdir -p $icons
	ln -s ../apps/tazpkg.png $icons/application-x-tazpkg.png
}

post_install()
{
	# Remove lockfile if exist, now tazpkg upgraded
	if [ -f /var/lock/tazpkgup.lock ]; then
		rm /var/lock/tazpkgup.lock
	fi

	# Ensure symlink for locale doc always exist
	if [ -f $1/etc/locale.conf ]; then
		locale=$(cat $1/etc/locale.conf | awk -F'LANG=' '{print $2}' \
		| sed 's/.UTF-8//')
		chroot "$1/" /sbin/tazlocale $locale 
	fi

	# Apply a fix to config file /etc/slitaz/slitaz.conf
	[ -f $1/etc/slitaz/slitaz.conf ] && \
		sed -i 's/LOCAL_STATE/LOCALSTATE/' $1/etc/slitaz/slitaz.conf
	grep -q ^Icon= $1/usr/share/applications/tazpkg-url.desktop ||
	  echo "Icon=tazpkg" >> $1/usr/share/applications/tazpkg-url.desktop

	# Add variables missing from tazpkg.conf (non-destructive merge from reference)
	default="$1/usr/share/tazpkg/tazpkg.conf.default"
	sysconf="$1/etc/slitaz/tazpkg.conf"
	if [ -f "$default" ] && [ -f "$sysconf" ]; then
		while IFS= read -r line; do
			case "$line" in ''|'#'*) continue ;; esac
			var="${line#export }"
			var="${var%%=*}"
			grep -q "^${var}=\|^export ${var}=" "$sysconf" || echo "$line" >> "$sysconf"
		done < "$default"
	fi
}
