#!/bin/sh

# AM INSTALL SCRIPT VERSION 3. 
set -u
APP=libreoffice
SITE="https://www.libreoffice.org"

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/icons" "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > "/opt/$APP/remove"
printf '\n%s' "rm -f /usr/local/share/applications/$APP*-AM.desktop" >> "/opt/$APP/remove"
chmod a+x "/opt/$APP/remove"

_release_still() {
	chooseone=" tail -1 "
	release=$(wget -q https://www.libreoffice.org/download/download-libreoffice -O - | tr '<' '\n' | grep dl_version | sed 's:.*>::' | tail -1)
	version=$(wget -q https://appimages.libreitalia.org/ -O - | tr '"' '\n' | grep -oi "libreoffice-.*appimage$" | grep -v ">\|LibreOffice-fresh\|LibreOffice-still" | sort -t- -k1,1V -k2,2V -k3,3V -k4,4V | grep "$edition" | grep "$release" | tail -1 )
}

_release_fresh() {
	chooseone=" head -1 "
	release=$(wget -q https://www.libreoffice.org/download/download-libreoffice -O - | tr '<' '\n' | grep dl_version | sed 's:.*>::' | head -1)
	version=$(wget -q https://appimages.libreitalia.org/ -O - | tr '"' '\n' | grep -oi "libreoffice-.*appimage$" | grep -v ">\|LibreOffice-fresh\|LibreOffice-still" | sort -t- -k1,1V -k2,2V -k3,3V -k4,4V | grep "$edition" | grep "$release" | tail -1 )
}

# CHOOSE A VERSION

read -r -p " Please choose a LibreOffice edition:

 - STILL is slightly older but tested longer.
 - FRESH includes all the latest features.
 - BASIC only supports English (US and GB).
 - STANDARD includes English (US/GB), Arabic, Chinese (Simplified and 
   Traditional), French, German, Italian, Japanese, Korean, Portuguese 
   (BR/PT), Russian, and Spanish.
 - FULL includes all supported languages.

 1) Still-basic			2) Still-basic with manuals
 3) Still-standard		4) Still-standard with manuals
 5) Still-full			6) Still-full with manuals
 7) Fresh-basic			8) Fresh-basic with manuals
 9) Fresh-standard		10) Fresh-standard with manuals
 11) Fresh-full			12) Fresh-full with manuals

 Type a number from the above list and press ENTER: " response
case "$response" in
	1)	rm -f /opt/"$APP"/version
		edition="basic-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	2)	rm -f /opt/"$APP"/version
		edition="basic.help-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	3)	rm -f /opt/"$APP"/version
		edition="standard-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	4)	rm -f /opt/"$APP"/version
		edition="standard.help-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	5)	rm -f /opt/"$APP"/version
		edition="full-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	6)	rm -f /opt/"$APP"/version
		edition="full.help-x86_64"
		_release_still 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	7)	rm -f /opt/"$APP"/version
		edition="basic-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	8)	rm -f /opt/"$APP"/version
		edition="basic.help-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	9)	rm -f /opt/"$APP"/version
		edition="standard-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	10)	rm -f /opt/"$APP"/version
		edition="standard.help-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	11)	rm -f /opt/"$APP"/version
		edition="full-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	12)	rm -f /opt/"$APP"/version
		edition="full.help-x86_64"
		_release_fresh 2> /dev/null 
		wget "https://appimages.libreitalia.org/$version" -O "$APP"
		wget "https://appimages.libreitalia.org/$version".zsync -O "$APP".zsync 2> /dev/null
		echo "$version" >> "/opt/$APP/version";;
	''|*)	mkdir -p "/opt/$APP/tmp";;
esac
cd ..
mv "/opt/$APP/tmp/$APP" "/opt/$APP/$APP" 2> /dev/null || exit 1
mv "/opt/$APP/tmp/$APP.zsync" "/opt/$APP/$APP.zsync" 2> /dev/null || exit 1
if test -f "/opt/$APP/$APP"; then rmdir ./tmp  || exit 1; fi 
chmod a+x "/opt/$APP/$APP" 2> /dev/null || exit 1

# LINK
ln -s "/opt/$APP/$APP" "/usr/local/bin/$APP"

# SCRIPT TO UPDATE THE PROGRAM
cat >> "/opt/$APP/AM-updater" << 'EOF'
#!/bin/sh
set -u
APP=libreoffice
SITE="https://www.libreoffice.org"
version0=$(cat "/opt/$APP/version")
edition="EDITION"
release=$(wget -q https://www.libreoffice.org/download/release-notes/ -O - | tr '>' '\n' | grep -i "^LibreOffice [0-9]*" | grep " Release" | tr ' ' '\n' | grep "^[0-9]" | RELEASE)
version=$(wget -q https://appimages.libreitalia.org/ -O - | tr '"' '\n' | grep -oi "libreoffice-.*appimage$" | grep -v ">\|LibreOffice-fresh\|LibreOffice-still" | sort -t- -k1,1V -k2,2V -k3,3V -k4,4V | grep "$edition" | grep "$release" | tail -1 )
[ -n "$version" ] || { echo "Error getting link"; exit 1; }
if command -v appimageupdatetool >/dev/null 2>&1; then
	cd "/opt/$APP" || exit 1
	appimageupdatetool -Or ./"$APP" && chmod a+x ./"$APP" && echo "$version" > ./version && exit 0
fi
if [ "$version" != "$version0" ] || [ -e /opt/"$APP"/*.zsync ]; then
	mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	[ -e /opt/"$APP"/*.zsync ] || notify-send "A new version of $APP is available, please wait"
	[ -e /opt/"$APP"/*.zsync ] && wget "https://appimages.libreitalia.org/$version.zsync" 2>/dev/null || { wget "https://appimages.libreitalia.org/$version" || exit 1; }
	# Use tar fx ./*tar* here for example in this line in case a compressed file is downloaded.
	cd ..
	mv ./tmp/*.zsync ./"$APP".zsync 2>/dev/null || mv --backup=t ./tmp/*mage ./"$APP"
	[ -e /opt/"$APP"/*.zsync ] && { zsync "/opt/$APP/$APP.zsync" || notify-send -u critical "zsync failed to update $APP"; }
	chmod a+x "/opt/$APP/$APP" || exit 1
	echo "$version" > ./version
	rm -R -f ./*zs-old ./*.part ./tmp ./*~
	[ "$version" != "$version0" ] && notify-send "$APP is updated!"
else
	echo "Update not needed!"
fi
EOF
sed -i 's#EDITION#'"$edition"'#g; s#RELEASE#'"$chooseone"'#g; s###g' "/opt/$APP/AM-updater"
chmod a+x "/opt/$APP/AM-updater"

# ICONS
cd "/opt/$APP" || exit 1
mv $(./libreoffice --appimage-extract usr/share/icons/hicolor/scalable/apps/*.svg) ./icons 2> /dev/null
RELEASE=$(ls ./icons | head -1 | rev | cut -c 10- | rev)
mv ./icons/"$RELEASE"-base* ./icons/"$APP"-base 2> /dev/null
mv ./icons/"$RELEASE"-basic* ./icons/"$APP"-basic 2> /dev/null
mv ./icons/"$RELEASE"-calc* ./icons/"$APP"-calc 2> /dev/null
mv ./icons/"$RELEASE"-chart* ./icons/"$APP"-chart 2> /dev/null
mv ./icons/"$RELEASE"-draw* ./icons/"$APP"-draw 2> /dev/null
mv ./icons/"$RELEASE"-extension* ./icons/"$APP"-extension 2> /dev/null
mv ./icons/"$RELEASE"-impress* ./icons/"$APP"-impress 2> /dev/null
mv ./icons/"$RELEASE"-main* ./icons/"$APP"-main 2> /dev/null
mv ./icons/"$RELEASE"-math* ./icons/"$APP"-math 2> /dev/null
mv ./icons/"$RELEASE"-startcenter* ./icons/"$APP"-startcenter 2> /dev/null
mv ./icons/"$RELEASE"-writer* ./icons/"$APP"-writer 2> /dev/null

# LAUNCHERS
mv $(./libreoffice --appimage-extract opt/libreoffice*/share/xdg) . 2> /dev/null
cd ./xdg || exit 1
for f in *.desktop ; do mv "$f" "$APP-${f%.desktop}-AM.desktop"; done
sed -i "s# $(echo $RELEASE | cut -c 12-) # #g; s#Icon=$APP#Icon=/opt/$APP/icons/$APP#g; s#$RELEASE#$APP#g" *
sed -i '/Name=LibreOffice/c\Name=LibreOffice' ./*startcenter-AM.desktop
cd ..
mv ./xdg/* /usr/local/share/applications/ 2> /dev/null
rm -R -f ./squashfs-root ./xdg