# Determine which architecture should be built
if [[ "$(arch)" = "i686" ||  "$(arch)" = "x86_64" ]] ; then
	ARCH=$(arch)
else
	echo "Architecture could not be determined"
	exit 1
fi

git_pull_rebase_helper()
{
	git reset --hard HEAD
        git pull
}

if [ ! -d ssr ] ; then
  git clone -b master https://github.com/probonopd/AppImageKit.git
fi
cd AppImageKit/
git_pull_rebase_helper
VERSION=$(git rev-list --count HEAD).$(git describe --tags | cut -d - -f 3)
wget -c https://github.com/probonopd/zsync-curl/releases/download/_binaries/zsync_curl -O AppImageUpdate.AppDir/usr/bin/zsync_curl
chmod a+x AppImageUpdate.AppDir/usr/bin/zsync_curl
wget -c https://github.com/probonopd/zsync-curl/releases/download/_binaries/appimageupdategui -O AppImageUpdate.AppDir/usr/bin/appimageupdategui
chmod a+x AppImageUpdate.AppDir/usr/bin/appimageupdategui
cp -r appimageupdategui/ui AppImageUpdate.AppDir/usr/bin/
cd ..

if [ -z "$NO_DOWNLOAD" ] ; then
  wget -c https://github.com/probonopd/AppImageKit/releases/download/5/AppImageAssistant # 64-bit
  chmod a+x AppImageAssistant 
fi # if [ -z "$NO_DOWNLOAD" ] ; then

APP=AppImageUpdate
if [[ "$ARCH" = "x86_64" ]] ; then
	AI=$APP"-"$VERSION"-x86_64.AppImage"
fi
if [[ "$ARCH" = "i686" ]] ; then
	AI=$APP"-"$VERSION"-i386.AppImage"
fi

echo $AI

rm -rf out/$AI
mkdir -p out/ || true
./AppImageAssistant AppImageKit/AppImageUpdate.AppDir out/$AI
