#!/bin/bash
#======================================================================
# this script supports installation of Seventh String Transcribe! 
# on gtk based linux systems. It has been verified on xubuntu-18.04
# and mint-cinnamon-19.03.
#----------------------------------------------------------------------
# Copyright 2020 © J.Jørgen von Bargen <jjvb@bassklampfe.de>
# Provided for free to seventhstring.com as addon for xsc64setup.tar.gz 
#======================================================================
set -e

#
# define source dir
#
SCRIPT=$(realpath "$0")
SCIPTDIR=$(dirname "${SCRIPT}")

#
# define destination dirs
#
BIN_DST=${HOME}/bin

#
# check if all required system commands are available
#
function check_required_software
{
	for command in \
		xdg-icon-resource \
		xdg-mime \
		xdg-desktop-menu 
	do
		if [ "$(which ${command})" == "" ]
		then
			echo "no programm for command '${command}' found"
			exit 1
		fi
	done
}

#
# steps to install transcribe
#
function install_transcribe
{
	check_required_software
	cd "${SCIPTDIR}"

	#----------------------------------
	# install binary, lib and help
	#----------------------------------
	echo "-- install software --"
	mkdir -p ${BIN_DST}/transcribe
	# Edit by AMR Sept 2020: I think it's nice to have everything (except the install
	# script) copied - user might want to see the readme, or the icons.
	#cp libgstvideosection.so transcribe xschelp.htb ${BIN_DST}/transcribe/
	cp -r * ${BIN_DST}/transcribe/
	rm ${BIN_DST}/transcribe/install-linux.sh

	#----------------------------------
	# copy icons to appropriate place
	#----------------------------------
	echo "-- install icons --"
	for size in 16 32 48 128 
	do
		xdg-icon-resource install --mode user --size ${size} gtkicons/xsc${size}x${size}.png seventhstring-transcribe
	done

	#----------------------------------
	# define mime type with icon
	#----------------------------------
	echo "-- create mime type --"
	cat <<"-EOF-" > /tmp/seventhstring-transcribe.xml
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/seventhstring-transcribe">
	<comment>Transcribe! File</comment>
	<glob pattern="*.xsc"/>
	<icon name="seventhstring-transcribe"/>
  </mime-type>
</mime-info>
-EOF-
	xdg-mime install --mode user /tmp/seventhstring-transcribe.xml

	#----------------------------------
	# define desktop file
	#----------------------------------
	# Edit by AMR June 2020: I don't think it's a good idea to have Transcribe! be
	# the default application to open when you double-click a media file because
	# it causes confusion if the user has Saved their transcription (an xsc file)
	# and then wants to continue work on it, but opens the media file again by mistake.
	# MimeType=application/seventhstring-transcribe;audio/aac;audio/ac3;audio/mp4;audio/x-mp3;audio/x-wav;video/mp4;audio/x-ms-wma;video/mpeg;audio/flac;audio/x-flac;audio/mpeg;audio/basic;audio/x-aiff;audio/x-wav;application/ogg;audio/x-vorbis+ogg;
	echo "-- create application type --"
	cat  << -EOF- > /tmp/seventhstring-transcribe.desktop
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
MimeType=application/seventhstring-transcribe;
Exec=${BIN_DST}/transcribe/transcribe %f
Categories=Audio;
Terminal=false
Icon=seventhstring-transcribe
Name=Transcribe!
-EOF-
	xdg-desktop-menu install --mode user /tmp/seventhstring-transcribe.desktop
	
	#----------------------------------
	# set default application
	#----------------------------------
	echo "-- set default application --"
	xdg-mime default seventhstring-transcribe.desktop application/seventhstring-transcribe
	exit
}

#
# steps to uninstall transcribe
#
function uninstall_transcribe
{
	for size in 16 32 48 128 
	do
		xdg-icon-resource uninstall --mode user --size ${size} seventhstring-transcribe
	done
	find ${HOME}/.local/share/applications -name "seventhstring-transcribe.desktop" | \
	xargs --no-run-if-empty -d '\n' xdg-desktop-menu uninstall --mode user
	find ${HOME}/.local/share/mime/packages -name "seventhstring-transcribe.xml" | \
	xargs --no-run-if-empty -d '\n' xdg-mime uninstall --mode user
	# remove binaries
	rm -vrf ${BIN_DST}/transcribe
	exit
}

#
# help about this script
#
function show_usage
{
	bold=$(tput bold)
	norm=$(tput sgr0)
	echo "Usage:"
	echo "${bold}bash install-linux.sh -i${norm}      to install transcribe"
	echo "${bold}bash install-linux.sh -u${norm}      to uninstall transcribe"
}

#
# arguments
#
if [ "$*" == "-i" ] ; then install_transcribe ; fi
if [ "$*" == "-u" ] ; then uninstall_transcribe ; fi;
show_usage

