#!/bin/sh
# $XTermId: df-install.in,v 1.17 2020/08/18 20:44:00 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 2011-2018,2020 by Thomas E. Dickey
#
#                         All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
#
# Install desktop-files, substituting the category to customize the file
# to the current system's conventions.
#
# Parameters are passed to the desktop-file-install program.  The last
# parameter is the name of the ".desktop" file to install.

failed() {
	echo "? $*" >&2
	exit 1
}

OPTS=

if test $# != 0
then
	APP_NAME=$1
	shift 1
fi

if test $# != 0
then
	ICON_NAME=$1
	shift 1
fi

TOOL=desktop-file-install

CDPATH=:
export CDPATH

while test $# != 0
do
	case .$1 in #(vi
	.DESTDIR=*) #(vi
		if test "$1" != "DESTDIR="
		then
			DESTDIR=`echo "$1" | sed -e 's,^DESTDIR=,,'`
		fi
		;;
	*.desktop) #(vi
		if test $# = 1
		then
			OLD_FILE=$1
		else
			OPTS="$OPTS $1"
		fi
		;;
	.--dir=*) #(vi
		OPTS="$OPTS $1"
		DESTDIR=
		;;
	*)
		OPTS="$OPTS $1"
		;;
	esac
	shift 1
done

# If DESTDIR is set (either as an environment variable, or command-line
# parameter), attempt to determine the full pathname (needed by the installer)
# to tell it to use the alternate location.  Because the installer has no
# useful options for this purpose, we search along the path to find the tool,
# and from that get the prefix, and assume that was the extent of customization
# when the tool was configured/installed.
if test -n "$DESTDIR"
then
	if test -d "$DESTDIR"
	then
		prefix=

		save_IFS="$IFS"
		IFS=':'
		for P in $PATH
		do
			test -z "$P" && P=.
			if test -f "$P/$TOOL"
			then
				prefix=`echo "$P" | sed -e 's,/[^/]*$,,'`
				break
			fi
		done
		IFS="$save_IFS"

		test -z "$prefix" && failed "could not find $TOOL in PATH"

		TARGET="${DESTDIR}${prefix}/share/applications"
		mkdir -p "$TARGET"

		OPTS="$OPTS --dir=${TARGET}"
	else
		failed "DESTDIR is not a directory: $DESTDIR"
	fi
fi

MY_TEMP=tempdir$$
rm -rf $MY_TEMP

if test -z "$OLD_FILE"
then
	failed "not found: $OLD_FILE"
elif ! mkdir $MY_TEMP
then
	failed "cannot mkdir: $MY_TEMP"
fi

# The newer dfi whines about deprecation of encoding, but no release provides
# --version or equivalent.  really.
if desktop-file-install --help-all >/dev/null 2>&1
then
	NO_ENCODING="#Encoding"
else
	NO_ENCODING="Encoding"
fi

NEW_FILE=`basename "$OLD_FILE"`
sed \
	-e '/^Encoding=/s%Encoding%'"$NO_ENCODING"'%' \
	-e '/^[^#]*Icon=/s%=.*%='"$ICON_NAME"'%' \
	-e '/^[^#]*Category=/s%=.*%=@DESKTOP_CATEGORY@%' \
	-e '/^[^#]*Categories=/s%=.*%=@DESKTOP_CATEGORY@%' \
	-e '/^[^#]/s%xterm%'"$APP_NAME"'%g' \
	-e '/^[^#]/s%XTerm%@APP_CLASS@%g' \
	"$OLD_FILE" >"$MY_TEMP/$NEW_FILE"
diff -u "$OLD_FILE" "$MY_TEMP/$NEW_FILE"
cd $MY_TEMP || exit 1

"$TOOL" $OPTS "$NEW_FILE"

cd ..
rm -rf $MY_TEMP

# vi:ts=4 sw=4