# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

setvar _luaversions = ""5.1 5.2 5.3""
setvar pkgname = 'lua-cjson'
setvar pkgver = '2.1.0'
setvar pkgrel = '7'
setvar pkgdesc = ""fast JSON parsing and encoding support for Lua""
setvar url = ""http://www.kyne.com.au/~mark/software/lua-cjson.php""
setvar arch = ""all""
setvar license = ""MIT""
setvar depends = """"
setvar install = """"
setvar makedepends = """"
setvar subpackages = """"
for _i in $_luaversions {
	setvar makedepends = ""$makedepends lua$_i-dev""
	setvar subpackages = ""$subpackages lua$_i-cjson:split_${_i/./_}""
}
setvar source = ""http://www.kyne.com.au/~mark/software/download/lua-cjson-$pkgver.tar.gz
	0001-Use-pkg-config-to-detect-cflags-or-fallback-to-LUA_I.patch
	0002-lua53-integers.patch
	0003-empty-array-metadata.patch
	0004-Option-for-sorting-object-keys.patch
	""


setvar _sdir = ""$srcdir"/lua-cjson-$pkgver"

proc prepare {
	local i
	cd $_sdir
	for i in $source {
		case (i) {
		*.patch { msg $i; patch -p1 -i "$srcdir"/$i || return 1}
		}
	}
	for _i in $_luaversions {
		cp -r $_sdir "$srcdir"/build-$_i
		sed -i -e "/^LUA_VERSION\s*=/s/=.*/= $_i/" \
			"$srcdir"/build-$_i/Makefile || return 1
	}
}

proc build {
	for _i in $_luaversions {
		cd "$srcdir"/build-$_i
		make PREFIX=/usr || return 1
	}
}

proc package {
	for _i in $_luaversions {
		cd "$srcdir"/build-$_i
		make PREFIX=/usr DESTDIR="$pkgdir" install || return 1
	}
}

proc _split {
	local d= _ver=$1
	setvar pkgdesc = ""fast JSON parsing and encoding support for Lua $_ver""
	setvar replaces = "$pkgname"
	setvar depends = ''
	setvar install_if = ""lua$_ver $pkgname=$pkgver-r$pkgrel""
	for d in usr/lib/lua usr/share/lua {
		if test -d "$pkgdir"/$d/$_ver {
			mkdir -p "$subpkgdir"/$d
			mv "$pkgdir"/$d/$_ver "$subpkgdir"/$d/ || return 1
		}
	}
}

for _i in $_luaversions {
	eval "split_${_i/./_}() { _split $_i; }"
}

setvar sha512sums = ""4343513b942f2ed98dc1ea6d7e852303bc1263f8acc5c70c8d674aec7adfc8279c5af7d5173a417b6f9a74543b8f6c8f98742d002f4f75832bbf23bb5a30ad2f  lua-cjson-2.1.0.tar.gz
5137cf25ca125b03ff2e95cedd8396d63b9a4919b1aba4ef333fd8945a4fd20f423c1e8d36b954a15969bd344cd43bb6c50df8b878cd74a706d6065455fbb003  0001-Use-pkg-config-to-detect-cflags-or-fallback-to-LUA_I.patch
2cf4d4fa116850a6a8fb74f27779383a339ca08ed81cdcb03be985c731b91234e7552a26913b609f2c9b02516fc74171d4e5fbb446bcdaf0eddddac3af08865a  0002-lua53-integers.patch
77a05166a88b7ec7d1c7fa2cd7e22fdf75dd2882d67529f8c924e8408c8bf3d210b8e2cd803cffc6d6460a63e079e331237811f1dbfeb03051090f4d6278a350  0003-empty-array-metadata.patch
d76c5ad8edd9f7ad017254ad1d1e9a822a42a12b9783a8217ab74e8afcc0aa58967dce52405e081ebd3ea63a63e8b0d0383c19112bfa579e10b47cd58debb7ee  0004-Option-for-sorting-object-keys.patch""