#!/bin/bash

################################################################
#
#  Mail Studio Installation Script
#
#  Run this script with bash and follow the instructions:
#  $ bash "Mail Studio - Install (1.1.0).sh"
#
################################################################

set -e

version="1.1.0"
launcher=~/.local/share/applications/mailstudio.desktop
association=~/.local/share/mime/packages/mailstudio.xml

if [[ $EUID == 0 ]]; then
	echo "Please don't run this script with sudo. Run it like this:"
	echo "bash $0"
	exit 1
fi

echo
echo '====================================================================='
echo 'Mail Studio Linux Install'
echo '====================================================================='
echo
echo 'This script will download Mail Studio and set up desktop integration.'
echo
echo 'It will create a launcher icon which shows up in the list of your'
echo 'installed programs, and will register *.msdesign and *.mscomp files'
echo 'to open in the app.'
echo
echo 'The program will be downloaded to the `~/.Mail Studio` folder. There'
echo 'you will also find a script called uninstall.sh which can remove any'
echo 'trace of Mail Studio from your system.'
echo
echo '====================================================================='
echo
echo 'Proceed? (y/n)'

read choice

if [ "$choice" != "y" ]; then 
	echo 'Exiting'
	exit 0
fi

mkdir -p ~/.Mail\ Studio
cd ~/.Mail\ Studio

if ! [ -f "Mail Studio.AppImage" ]; then
	echo
	echo 'Downloading Mail Studio.AppImage ...'
	echo
	wget -O Mail\ Studio.AppImage -nv --show-progress "https://mailstudio.app/linux-download?v=$version"
fi

# Creating the uninstall.sh script

cat > uninstall.sh <<- EOM
#!/bin/bash
set -e

if [[ \$EUID == 0 ]]; then
	echo "Please don't run this script with sudo. Run it like this:"
	echo "bash uninstall.sh"
	exit 1
fi

rm -f "$launcher" "$association"
rm -f ~/.local/share/icons/hicolor/*/{apps,mimetypes}/mailstudio*

if [ -x "\$(command -v update-mime-database)" ]; then
	update-mime-database ~/.local/share/mime
fi

if [ -x "\$(command -v update-desktop-database)" ]; then
	update-desktop-database ~/.local/share/applications
fi

echo 'Mail Studio was uninstalled. You can delete the current folder and AppImage.'
echo
echo 'Your distro may still show Mail Studio in some places, but this will go away after a restart.'
EOM

# Create a desktop launcher

mkdir -p ~/.local/share/applications/

cat > $launcher <<- EOM
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=Mail Studio
Path=$(pwd)
Icon=mailstudio
Exec="$(pwd)/Mail Studio.AppImage" --no-sandbox %F
Comment=A Powerful Email Design Tool
Categories=Development
StartupNotify=false
StartupWMClass=Mail Studio
Terminal=false
MimeType=application/x-mstudio-design;application/x-mstudio-component
EOM

# Set the launcher as trusted

chmod u+x $launcher
chmod u+x "Mail Studio.AppImage"

if [ -x "$(command -v gio)" ]; then
	gio set $launcher "metadata::trusted" true 2> /dev/null || true 
fi

if [ -x "$(command -v update-desktop-database)" ]; then
	update-desktop-database ~/.local/share/applications
fi

# Create app and file type icons

function writeIcon() {
	mkdir -p ~/.local/share/icons/hicolor/$1/{apps,mimetypes}
	cd ~/.local/share/icons/hicolor/$1
	echo $2 | base64 -d > apps/mailstudio.png
	cp apps/mailstudio.png mimetypes/mailstudio-component.png
	cp apps/mailstudio.png mimetypes/mailstudio-design.png
}

writeIcon 16x16 [payload removed: 1808 characters]
writeIcon 32x32 [payload removed: 2360 characters]
writeIcon 64x64 [payload removed: 3220 characters]
writeIcon 128x128 [payload removed: 5472 characters]
writeIcon 256x256 [payload removed: 10132 characters]
writeIcon 512x512 [payload removed: 19680 characters]

touch ~/.local/share/icons/hicolor

if [ -x "$(command -v update-icon-caches)" ]; then
	update-icon-caches ~/.local/share/icons/hicolor
fi

# File associations

mkdir -p ~/.local/share/mime/packages

cat > $association <<- EOM
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
   <mime-type type="application/x-mstudio-design">
     <comment>Mail Studio Design</comment>
     <glob pattern="*.msdesign"/>
     <icon name="mailstudio-design"/>
   </mime-type>
   <mime-type type="application/x-mstudio-component">
     <comment>Mail Studio Component</comment>
     <glob pattern="*.mscomp"/>
     <icon name="mailstudio-component"/>
   </mime-type>
</mime-info>
EOM

if [ -x "$(command -v update-mime-database)" ]; then
	update-mime-database ~/.local/share/mime
fi

echo
echo 'Mail Studio was installed.'
