#!/bin/bash
set -e
ataraxia-meson(){
	meson setup \
	  --prefix        /usr \
	  --libexecdir    lib \
	  --sbindir       bin \
	  --buildtype     plain \
	  --auto-features enabled \
	  --wrap-mode     nodownload \
	  -D              b_lto=true \
	  -D              b_pie=true \
	  "$@"

}
export SRC=$(pwd)/build
export PKG=$(pwd)/install
export CC=gcc
export CXX=g++
source KagamiBuild
[ -d build ] && rm -rf build
[ -d install ] && rm -rf install
[ -d inary ] && rm -rf inary
mkdir -p "$SRC"/$name-$version || true
mkdir inary || true
mkdir -p $PKG || true
cd $SRC
for s in $source
do
if basename $s | grep ".git$" &>/dev/null ; then
	git clone $s
else
	wget $s
	basename $s | grep ".tar.*$" && tar -xf $(basename $s)
fi
done
build
cd $PKG
[ -f ../install.tar.xz ] && rm -f ../install.tar.xz
tar --xz -cf ../install.tar.xz *
echo -e "<Files>" > ../files.xml
find | sed "s|^\./||g" | while read line
do
	if [ -f $line ] ; then
		echo -e "   <File>" >> ../files.xml
	        echo -e "      <Path>$line</Path>" >> ../files.xml
        	echo -e "      <Type>data</Type>" >> ../files.xml
        	echo -e "      <Size>$(du -b $line | awk '{print $1}')</Size>" >> ../files.xml
        	echo -e "      <Uid>0</Uid>" >> ../files.xml
        	echo -e "      <Gid>0</Gid>" >> ../files.xml
        	echo -e "      <Mode>0o755</Mode>" >> ../files.xml
        	echo -e "      <Hash>$(sha1sum $line | awk '{print $1}')</Hash>" >> ../files.xml
        	echo -e "   </File>" >> ../files.xml
	fi
done
echo -e "</Files>" >> ../files.xml
cd ..
mv install.tar.xz inary/install.tar.xz
mv files.xml inary/files.xml
export url="$(cat KagamiBuild | grep '# URL:'| head -n 1 | awk '{print $3}')"
export pkgdesc="$(cat KagamiBuild | grep '# Description:'| head -n 1 | sed 's/^# Description://g')"
export depends=($(cat KagamiBuild | grep '# Depends on:'| head -n 1 | sed "s/^# Depends on://g"))
export license="unknown"
export installtarhash="$(sha1sum inary/install.tar.xz | awk '{print $1}')"
{
	echo -e "<INARY>"
	echo -e "    <Source>"
	echo -e "        <Name>$name</Name>"
	echo -e "        <Homepage>$url</Homepage>"
	echo -e "        <Packager>"
	echo -e "            <Name>Auto generated</Name>"
	echo -e "            <Email>auto@generat.et</Email>"
	echo -e "        </Packager>"
	echo -e "    </Source>"
	echo -e "    <Package>"
	echo -e "    <RFP></RFP>"
	echo -e "    <PartOf>ataraxia</PartOf>"	
	echo -e "        <Name>$name</Name>"
	echo -e "        <Summary xml:lang=\"en\">$pkgdesc</Summary>"
	echo -e "        <Description xml:lang=\"en\">$pkgdesc</Description>"
	echo -e "        <IsA>app</IsA>"
	echo -e "        <License>$license</License>"
	echo -e "    <RuntimeDependencies>"
	for dep in ${depends[@]}
	do
		echo -e "        <Dependency>$dep</Dependency>"
	done
	echo -e "    </RuntimeDependencies>"
	echo -e "        <Files>"
	echo -e "            <Path fileType=\"data\">/usr</Path>"
	echo -e "            <Path fileType=\"data\">/etc</Path>"
	echo -e "            <Path fileType=\"data\">/bin</Path>"
	echo -e "            <Path fileType=\"data\">/var</Path>"
	echo -e "            <Path fileType=\"data\">/lib</Path>"
	echo -e "            <Path fileType=\"data\">/boot</Path>"
	echo -e "        </Files>"
	echo -e "        <History>"
	echo -e "            <Update release=\"1\">"
	echo -e "                <Date>2020-04-24</Date>"
	echo -e "                <Version>$version</Version>"
	echo -e "                <Comment>Auto generated.</Comment>"
	echo -e "                <Name>Auto generated</Name>"
	echo -e "                <Email>auto@generat.et</Email>"
	echo -e "            </Update>"
	echo -e "        </History>"
	echo -e "        <BuildHost>uludag</BuildHost>"
	echo -e "        <Distribution>Sulin</Distribution>"
	echo -e "        <DistributionRelease>2019</DistributionRelease>"
	echo -e "        <Architecture>x86_64</Architecture>"
	echo -e "        <InstalledSize>1</InstalledSize>"
	echo -e "        <InstallTarHash>$installtarhash</InstallTarHash>"
	echo -e "        <PackageFormat>1.2</PackageFormat>"
	echo -e "    </Package>"
	echo -e "</INARY>"
} > inary/metadata.xml
cd inary
zip -r ../$name-$version-$(uname -m).inary ./*
