summaryrefslogtreecommitdiff
path: root/scripts/mkc_install.in
blob: 1d38b6e60025ff2709b38f5382e1ccb94e1d2953 (plain)
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
169
#!@SH@

# install - install a program, script, or datafile
# This comes from X11R5 (mit/util/scripts/install.sh)
# and was modified by Aleksey Cheusov <vle@gmx.net>.
#
# Copyright 1991 by the Massachusetts Institute of Technology
# Copyright 2013-2019 by Aleksey Cheusov <vle@gmx.net>
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of M.I.T. not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission.  M.I.T. makes no representations about the
# suitability of this software for any purpose.  It is provided "as is"
# without express or implied warranty.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
#
# This script is compatible with the BSD install script, but was written
# from scratch.  It can only install one file at a time, a restriction
# shared with many OS's install programs.

# set DOITPROG to echo to test this script
doit="${DOITPROG-}"

# put in absolute paths if you don't have them in your path; or use env. vars.

: ${STRIP:=strip}
mvprog="${MVPROG-mv}"
cpprog="${CPPROG-cp}"
lnprog="${LNSPROG-ln -f}"
lnsprog="${LNSPROG-ln -f -s}"
chmodprog="${CHMODPROG-chmod}"
chownprog="${CHOWNPROG-chown}"
chgrpprog="${CHGRPPROG-chgrp}"
stripprog="${STRIPPROG-$STRIP}"
rmprog="${RMPROG-rm}"
mkdirprog="${MKDIRPROG-mkdir}"

instcmd="$cpprog"
chmodcmd=mkcchmod; mod=0755;
chowncmd=":"
chgrpcmd=":"
stripcmd=":"
rmcmd="$rmprog -f"
mvcmd="$mvprog"
src=""
dst=""
dir_arg=""

set -e

mkcchgrp () { $chgrpprog "$grp" "$@"; }
mkcchown () { $chownprog "$own" "$@"; }
mkcchmod () { $chmodprog "$mod" "$@"; }

last_arg() {
    while test $# -gt 1; do
	shift
    done

    printf '%s\n' "$1"
}

while getopts cdg:l:m:o:s f; do
    case "$f" in
        c)
	    true;;
	d)
	    instcmd="$mkdirprog -p"
	    dir_arg=true;;
	l)
	    case "$OPTARG" in
		s)
		    instcmd="$lnsprog";;
		h)
		    instcmd="$lnprog";;
		*)
		    echo "Incorrect linking type '$OPTARG'" 1>&2
		    exit 1;;
	    esac;;
	m)
	    mod="$OPTARG"
	    chmodcmd=mkcchmod;;
	o)
	    own="$OPTARG"
	    chowncmd=mkcchown;;
	g)
	    grp="$OPTARG"
	    chgrpcmd=mkcchgrp;;
	s)
	    stripcmd="$stripprog";;
        *)
	    exit 2;;
    esac
done
shift `expr $OPTIND - 1`

if test "$instcmd" = "$lnsprog" -o "$instcmd" = "$lnprog"; then
    chmodcmd=":"
    chowncmd=":"
    chgrpcmd=":"
    stripcmd=":"
fi

#
copy_file () {
    dsttmp="$dstdir/#inst.$$#"
    trap "rm -f ${dsttmp}" 0

    $doit $instcmd "$src" "$dsttmp"
    $doit $chowncmd "$dsttmp"
    $doit $chgrpcmd "$dsttmp"
    $doit $stripcmd "$dsttmp"
    $doit $chmodcmd "$dsttmp"

    $doit $rmcmd -f "$dst"
    $doit $mvcmd "$dsttmp" "$dst"
}

if test -n "$dir_arg"; then
    # -d dir1 dir2 ... dirN
    for f in "$@"; do
	if ! test -d "$f"; then
	    $doit $instcmd "$f"
	    $doit $chowncmd "$f"
	    $doit $chgrpcmd "$f"
	    $doit $chmodcmd "$f"
	fi
    done
elif test $# -eq 2; then
    # src_file trg_file
    if test -d "$2"; then
	dst="$2/"`basename "$1"`
	dstdir="$2"
    else
	dst="$2"
	dstdir=`dirname "$2"`
    fi

    src="$1"
    copy_file
elif test $# -gt 2; then
    # src_file1 src_file2 ... src_fileN trg_dir
    dstdir=`last_arg "$@"`

    if test -d "$dstdir"; then
	:
    else
	echo "Last argument '$dstdir' is not a directory" 1>&2
	exit 1
    fi

    while test $# -gt 1; do
	src="$1"
	dst="$dstdir"/`basename $1`
	copy_file
	shift
    done
else
    # bad usage
    echo "Missing target file or directory" 1>&2
    exit 1
fi