1e39b573cSmrg#!/bin/sh
204b94745Smrg# $XTermId: df-install.in,v 1.17 2020/08/18 20:44:00 tom Exp $
3e39b573cSmrg# -----------------------------------------------------------------------------
4e39b573cSmrg# this file is part of xterm
5e39b573cSmrg#
6f2e35a3aSmrg# Copyright 2011-2018,2020 by Thomas E. Dickey
7e39b573cSmrg#
8e39b573cSmrg#                         All Rights Reserved
9e39b573cSmrg#
10e39b573cSmrg# Permission is hereby granted, free of charge, to any person obtaining a
11e39b573cSmrg# copy of this software and associated documentation files (the
12e39b573cSmrg# "Software"), to deal in the Software without restriction, including
13e39b573cSmrg# without limitation the rights to use, copy, modify, merge, publish,
14e39b573cSmrg# distribute, sublicense, and/or sell copies of the Software, and to
15e39b573cSmrg# permit persons to whom the Software is furnished to do so, subject to
16e39b573cSmrg# the following conditions:
17e39b573cSmrg#
18e39b573cSmrg# The above copyright notice and this permission notice shall be included
19e39b573cSmrg# in all copies or substantial portions of the Software.
20e39b573cSmrg#
21e39b573cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22e39b573cSmrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23e39b573cSmrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24e39b573cSmrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25e39b573cSmrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26e39b573cSmrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27e39b573cSmrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e39b573cSmrg#
29e39b573cSmrg# Except as contained in this notice, the name(s) of the above copyright
30e39b573cSmrg# holders shall not be used in advertising or otherwise to promote the
31e39b573cSmrg# sale, use or other dealings in this Software without prior written
32e39b573cSmrg# authorization.
33e39b573cSmrg# -----------------------------------------------------------------------------
34e39b573cSmrg#
35e39b573cSmrg# Install desktop-files, substituting the category to customize the file
36e39b573cSmrg# to the current system's conventions.
37e39b573cSmrg#
38e39b573cSmrg# Parameters are passed to the desktop-file-install program.  The last
39e39b573cSmrg# parameter is the name of the ".desktop" file to install.
40f2e35a3aSmrg
41f2e35a3aSmrgfailed() {
42f2e35a3aSmrg	echo "? $*" >&2
43f2e35a3aSmrg	exit 1
44f2e35a3aSmrg}
45f2e35a3aSmrg
46e39b573cSmrgOPTS=
47e39b573cSmrg
48e39b573cSmrgif test $# != 0
49e39b573cSmrgthen
50e39b573cSmrg	APP_NAME=$1
51e39b573cSmrg	shift 1
52e39b573cSmrgfi
53e39b573cSmrg
54f2e35a3aSmrgif test $# != 0
55f2e35a3aSmrgthen
56f2e35a3aSmrg	ICON_NAME=$1
57f2e35a3aSmrg	shift 1
58f2e35a3aSmrgfi
59f2e35a3aSmrg
60f2e35a3aSmrgTOOL=desktop-file-install
61f2e35a3aSmrg
62e39b573cSmrgCDPATH=:
63e39b573cSmrgexport CDPATH
64e39b573cSmrg
65e39b573cSmrgwhile test $# != 0
66e39b573cSmrgdo
67e39b573cSmrg	case .$1 in #(vi
68f2e35a3aSmrg	.DESTDIR=*) #(vi
69f2e35a3aSmrg		if test "$1" != "DESTDIR="
70f2e35a3aSmrg		then
71f2e35a3aSmrg			DESTDIR=`echo "$1" | sed -e 's,^DESTDIR=,,'`
72f2e35a3aSmrg		fi
73f2e35a3aSmrg		;;
74e39b573cSmrg	*.desktop) #(vi
75e39b573cSmrg		if test $# = 1
76e39b573cSmrg		then
77e39b573cSmrg			OLD_FILE=$1
78e39b573cSmrg		else
79e39b573cSmrg			OPTS="$OPTS $1"
80e39b573cSmrg		fi
81e39b573cSmrg		;;
82f2e35a3aSmrg	.--dir=*) #(vi
83f2e35a3aSmrg		OPTS="$OPTS $1"
84f2e35a3aSmrg		DESTDIR=
85f2e35a3aSmrg		;;
86e39b573cSmrg	*)
87e39b573cSmrg		OPTS="$OPTS $1"
88e39b573cSmrg		;;
89e39b573cSmrg	esac
90e39b573cSmrg	shift 1
91e39b573cSmrgdone
92e39b573cSmrg
93f2e35a3aSmrg# If DESTDIR is set (either as an environment variable, or command-line
94f2e35a3aSmrg# parameter), attempt to determine the full pathname (needed by the installer)
95f2e35a3aSmrg# to tell it to use the alternate location.  Because the installer has no
96f2e35a3aSmrg# useful options for this purpose, we search along the path to find the tool,
97f2e35a3aSmrg# and from that get the prefix, and assume that was the extent of customization
98f2e35a3aSmrg# when the tool was configured/installed.
99f2e35a3aSmrgif test -n "$DESTDIR"
100f2e35a3aSmrgthen
101f2e35a3aSmrg	if test -d "$DESTDIR"
102f2e35a3aSmrg	then
103f2e35a3aSmrg		prefix=
104f2e35a3aSmrg
105f2e35a3aSmrg		save_IFS="$IFS"
106f2e35a3aSmrg		IFS=':'
107f2e35a3aSmrg		for P in $PATH
108f2e35a3aSmrg		do
109f2e35a3aSmrg			test -z "$P" && P=.
110f2e35a3aSmrg			if test -f "$P/$TOOL"
111f2e35a3aSmrg			then
112f2e35a3aSmrg				prefix=`echo "$P" | sed -e 's,/[^/]*$,,'`
113f2e35a3aSmrg				break
114f2e35a3aSmrg			fi
115f2e35a3aSmrg		done
116f2e35a3aSmrg		IFS="$save_IFS"
117f2e35a3aSmrg
118f2e35a3aSmrg		test -z "$prefix" && failed "could not find $TOOL in PATH"
119f2e35a3aSmrg
120f2e35a3aSmrg		TARGET="${DESTDIR}${prefix}/share/applications"
121f2e35a3aSmrg		mkdir -p "$TARGET"
122f2e35a3aSmrg
123f2e35a3aSmrg		OPTS="$OPTS --dir=${TARGET}"
124f2e35a3aSmrg	else
125f2e35a3aSmrg		failed "DESTDIR is not a directory: $DESTDIR"
126f2e35a3aSmrg	fi
127f2e35a3aSmrgfi
128f2e35a3aSmrg
129e39b573cSmrgMY_TEMP=tempdir$$
130e39b573cSmrgrm -rf $MY_TEMP
131e39b573cSmrg
132e39b573cSmrgif test -z "$OLD_FILE"
133e39b573cSmrgthen
134f2e35a3aSmrg	failed "not found: $OLD_FILE"
135e39b573cSmrgelif ! mkdir $MY_TEMP
136e39b573cSmrgthen
137f2e35a3aSmrg	failed "cannot mkdir: $MY_TEMP"
138e39b573cSmrgfi
139e39b573cSmrg
1400bd37d32Smrg# The newer dfi whines about deprecation of encoding, but no release provides
1410bd37d32Smrg# --version or equivalent.  really.
1420bd37d32Smrgif desktop-file-install --help-all >/dev/null 2>&1
1430bd37d32Smrgthen
1440bd37d32Smrg	NO_ENCODING="#Encoding"
1450bd37d32Smrgelse
1460bd37d32Smrg	NO_ENCODING="Encoding"
1470bd37d32Smrgfi
1480bd37d32Smrg
149f2e35a3aSmrgNEW_FILE=`basename "$OLD_FILE"`
150e39b573cSmrgsed \
151f2e35a3aSmrg	-e '/^Encoding=/s%Encoding%'"$NO_ENCODING"'%' \
152f2e35a3aSmrg	-e '/^[^#]*Icon=/s%=.*%='"$ICON_NAME"'%' \
1530bd37d32Smrg	-e '/^[^#]*Category=/s%=.*%=@DESKTOP_CATEGORY@%' \
1540bd37d32Smrg	-e '/^[^#]*Categories=/s%=.*%=@DESKTOP_CATEGORY@%' \
155f2e35a3aSmrg	-e '/^[^#]/s%xterm%'"$APP_NAME"'%g' \
156e39b573cSmrg	-e '/^[^#]/s%XTerm%@APP_CLASS@%g' \
157f2e35a3aSmrg	"$OLD_FILE" >"$MY_TEMP/$NEW_FILE"
158f2e35a3aSmrgdiff -u "$OLD_FILE" "$MY_TEMP/$NEW_FILE"
159f2e35a3aSmrgcd $MY_TEMP || exit 1
160e39b573cSmrg
161f2e35a3aSmrg"$TOOL" $OPTS "$NEW_FILE"
162e39b573cSmrg
163e39b573cSmrgcd ..
164e39b573cSmrgrm -rf $MY_TEMP
165e39b573cSmrg
166e39b573cSmrg# vi:ts=4 sw=4
167