#!/bin/bash

# Local Joomla installation at:
JOOMLADIR=~/github/joomla-cms

UPDATESERVER="kuschel.at"
# the following host must be given in .ssh/config configuration:
UPDATESERVERSSH="kuschel"
UPDATEDIR="/var/www/kuschel.at/update"
UPDATEFILE="extensions.xml"
UPDATEURL="https://kuschel.at/update"

# this is a script file to generate a zip package from this source:
SCRIPTPATH="$( cd -- "$(dirname "$0")" > /dev/null 2>&1 ; pwd -P )"
cd ..
# now we are at the depot directory
# if there is a file named depot.xml, we ask to increase the version with

read -p "Do you want to update/increase the patch (last) version number? (y/N)" -n 1 -r
echo  # (optional) move to a new line
if [[ $REPLY =~ ^[YyJj]$ ]]
then
	echo "Updating the version"
#	patch=`xmllint -xpath 'string(//*[local-name()="version"])' depot.xml | rev | cut -f1 -d. | rev`
#	echo "$patch"
#	patch=$((patch+1))
#	echo "$patch"
	version=`xmllint -xpath 'string(//*[local-name()="version"])' depot.xml`
	major=`echo "$version" | cut -f1 -d.`
	minor=`echo "$version" | cut -f2 -d.`
	patch=`echo "$version" | cut -f3 -d.`
	echo "$version"
	patch=$((patch+1))
	minor=$((minor))
	newversion="$major.$minor.$patch"
	echo "$newversion"
	printf -v date '%(%Y-%m-%d)T\n' -1
	xmllint --shell depot.xml << EOF
cd extension/version
set $newversion
cd /extension/creationDate
set $date
save
EOF

else
	echo "Keep the version"
fi
mkdir -p zip
# Generate ZIP file without directories zip, script
zip -q -r zip/depot.zip * -x .git/\* -x script/\* -x zip/\*
# Make a copy of the ZIP file with version added:

## get the version from depot.xml, again:
version=`xmllint -xpath 'string(//*[local-name()="version"])' depot.xml`
major=`echo "$version" | cut -f1 -d.`
minor=`echo "$version" | cut -f2 -d.`
patch=`echo "$version" | cut -f3 -d.`
echo "Now the version is: ${version}"
cp -f zip/depot.zip "zip/depot_${version}.zip"
echo "Create sha512 checksum"
sha512sum zip/depot.zip | tee zip/depot.zip.sha512

read -p "Do you want to upload the zip file to the update server \"https://kuschel.at/update\"? (y/N)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[YyJj]$ ]]
then
	echo "Uploading to the update server"
	scp "zip/depot_${version}.zip" ${UPDATESERVERSSH}:${UPDATEDIR}/
	echo "Create sha512 checksum"
	# sha512sum "zip/depot_${version}.zip" | awk '{print $1}' | tee "zip/depot_${version}.zip.sha512"
	SHA512=$(sha512sum "zip/depot_${version}.zip" | awk '{print $1}' | tee "zip/depot_${version}.zip.sha512")
	echo ${SHA512}
	scp "zip/depot_${version}.zip.sha512" ${UPDATESERVERSSH}:${UPDATEDIR}/
	echo "Now you have to edit the extensions.xml on the update server manually,"

	read -p "or do you want to automatically adjust the extensions.xml file on the update server ${UPDATESERVER}? (y/N)" -n 1 -r
	echo # (optional) move to a new line
	if [[ $REPLY =~ ^[YyJy]$ ]]
	then
		## first fetch the file from the server
		scp ${UPDATESERVERSSH}:${UPDATEDIR}/${UPDATEFILE} ${SCRIPTPATH}/
		echo "Now we make a backup of this file ${UPDATEFILE}"
		cp -f "${SCRIPTPATH}/${UPDATEFILE}" "${SCRIPTPATH}/${UPDATEFILE}.bak"
		## now we change the right entries in the file:
		xmllint --shell "${SCRIPTPATH}/${UPDATEFILE}" << EOF
cd /updates/update/version
set ${version}
cd /updates/update/downloads/downloadurl
set ${UPDATEURL}/depot_${version}.zip
cd /updates/update/sha512
set ${SHA512}
save
EOF

		echo "Unified DIFF:"
		diff -u "${SCRIPTPATH}/${UPDATEFILE}.bak" "${SCRIPTPATH}/${UPDATEFILE}"
		echo "Copy updated file to server..."
		scp "${SCRIPTPATH}/${UPDATEFILE}" ${UPDATESERVERSSH}:${UPDATEDIR}/${UPDATEFILE}
	else
		echo "Please edit the extensions.xml on the update server manually."
	fi
else
	echo "nothing to do"
fi

# Install this extension to the active Joomla installation per CLI

read -p "Do you want to install this to the Joomla installation at \"$JOOMLADIR\"? (Y/n)" -n 1 -r
echo  # (optional) move to a new line
if [[ $REPLY =~ ^[Nn]$ ]]
then
	echo "Extension was not installed"
else
	# Find the CLI php procedure, file joomla.php in cli must exists.
	FILE=${JOOMLADIR}/cli/joomla.php
	if [ -f $FILE ]
	then
		echo "$FILE exists."
		php $FILE --version
		php $FILE --help
		php $FILE list
		# sudo -g http php $FILE core:update:check
		php $FILE core:update:check
		pwd
		# sudo -u http php $FILE extension:install --path zip/depot.zip -v
		php $FILE extension:install --path zip/depot.zip -v
		php $FILE extension:list --type component
	else
		echo "$FILE does not exists."
		exit 9
	fi
fi
