File: install.sh

package info (click to toggle)
libt3window 0.4.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 636 kB
  • sloc: ansic: 6,176; sh: 878; xml: 172; python: 171; makefile: 62
file content (168 lines) | stat: -rwxr-xr-x 3,114 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/sh
# Copyright (C) 2009 G.P. Halkes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Set zsh to emulate sh (otherwise all kinds of eval's wont work)
[ -n "${ZSH_VERSION}" ] && emulate sh

unset directory sources dest mode owner group strip

# Provide an alternative to !
not() {
	if "$@" ; then
		false
	else
		true
	fi
}

# Default mode for install is rwxr-xr-x
mode="0755"

while [ $# -gt 0 ]
do
	case "$1" in
		-d)
			directory="1"
			;;
		-m)
			mode="$2"
			shift
			;;
		-m*)
			mode=`echo "$1" | sed 's/^-m//'`
			;;
		-o)
			owner="$2"
			shift
			;;
		-o*)
			owner=`echo "$1" | sed 's/^-o//'`
			;;
		-g)
			group="$2"
			shift
			;;
		-g*)
			group=`echo "$1" | sed 's/^-g//'`
			;;
		-s)
			strip=1
			;;
		*)
			break
			;;
	esac
	shift
done

if [ "1" = "${directory}" ] ; then
	if [ $# -eq 0 ] ; then
		echo "No directory names supplied" >&2
		exit 1
	fi

	# Create directories
	while [ $# -gt 0 ]
	do
		if [ ! -d "$1" ] ; then
			# Find name of dir to apply chown/chgrp -R to, i.e., the first part
			# of the path that does not yet exist.
			firstDir="$1"
			testDir=`dirname "$1"`
			while [ ! -d "${testDir}" ] ; do
				firstDir="${testDir}"
				testDir=`dirname "${firstDir}"`
			done

			# Make the directory
			if not mkdir -p "$1" ; then
				exit 1
			fi

			if not chmod -R "${mode}" "${firstDir}" ; then
				exit 1
			fi

			# Set owner and group, if specified
			if [ -n "${owner}" ] ; then
				if not chown -R "${owner}" "${firstDir}" ; then
					exit 1
				fi
			fi
			if [ -n "${group}" ] ; then
				if not chgrp -R "${group}" "${firstDir}" ; then
					exit 1
				fi
			fi
		fi
		shift
	done
	exit
fi

if [ $# -lt 2 ] ; then
	echo "No destination and/or source(s) supplied" >&2
	exit 1
fi

install_file() {
	if [ -d "$2" ] ; then
		echo "Cannot install $2: directory in the way" >&2
		exit 1
	fi

	# Copy file
	if not cp "$1" "$2" ; then
		exit 1
	fi

	# Set attributes
	if [ -n "${mode}" ] ; then
		if not chmod "${mode}" "$2" ; then
			exit 1
		fi
	fi
	if [ -n "${owner}" ] ; then
		if not chown "${owner}" "$2" ; then
			exit 1
		fi
	fi
	if [ -n "${group}" ] ; then
		if not chgrp "${group}" "$2" ; then
			exit 1
		fi
	fi
	if [ -n "${strip}" ] ; then
		if not strip "$2" ; then
			exit 1
		fi
	fi
}

eval dest="\"\${$#}\""

if [ -d "${dest}" ] ; then

	# Loop over all sources, but do not process destination as source
	while [ $# -gt 1 ]
	do
		install_file "$1" "${dest}/`basename \"$1\"`"
		shift
	done
elif [ $# -eq 2 ] ; then
	install_file "$1" "${dest}"
else
	echo "Multiple sources specified, but destination is not a directory" >&2
	exit 1
fi