aboutsummaryrefslogtreecommitdiff
path: root/tools/bison++/smart-install
blob: 5bfddeff80435c8e004b7489ab67f6a22f9439be (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /bin/sh 
# @(#) smart-install.sh ShScript env 1.5 94/09/13 14:03:30 (/u/icdc/rdt/tools/mgmake/SCCS/s.smart-install.sh)
#
# Nom du Fichier : 	smart-install.sh
# Titre : 		|Titre|
# Auteur:		coetmeur		
# Date de creation :	01 Sep 94
#
# Description :
#	Document de reference : |doc|
#	Objet : |objet|
#
#
# 
# historique :
# |date|	coetmeur	|objet|
#
mycmdname=smart-install

usage() {
echo "usage: $mycmdname [<options>] [<file1> <file2>| <files...> <directory>/" 
echo "	<file1>		: source file"
echo "	<file2>		: destination file"
echo "	<files...>	: possibly empty list of source files"
echo "	<directory>/	: destination directory with '/' appended"
echo "trace options:  "
echo "	-trace		: activate trace"
echo "	-traceonly	: activate trace only, disactivate actions"
echo "	-echo		: echo actions"
echo "	-quiet		: reduce comment messages"
echo "	-usage		: show this usage"
echo "user options:  "
echo "	-compare	: don't copy if files are equals"
echo "	-used		: return error code 1 if nothing is done"
echo "	-preserve	: preserve date and flags in copy"
echo "	-apply <command>: apply a command on each file"
echo "	-owner <value>	: change owner"
echo "	-group <value>	: change group"
echo "	-mode <value>	: change protection mode"
echo "	-rename <sed-subst-patterns> : change name of destination "
echo "		in destination directory. "
echo "	<sed-s-pattern>	: pattern and replace parts "
echo "		of sed(1) 's' command. can add subdirectory."
echo "examples: "
echo " $mycmdname *.h -compare ../include/"
echo " $mycmdname  ../include/"
echo " $mycmdname -preserve -group kmem -mode 2555  a.out  ../bin/"
echo " $mycmdname -mode 444 -apply 'ranlib -t' *.a ../lib/"
echo " $mycmdname -rename '#^\\(.*\\)\\.\\([1-9l-p]\\)\\([a-z]*\\)\\.man\$#man\\2/\\1.\\2\\3#' \\"
echo "	*.man ../doc/man/"
}

badparameters() {
echo "$mycmdname: $*" >&2
usage
exit 2
}

traceandshow() {
echo "	$@"
"$@"
}

doit=""
traceit=":"
commentit="echo"
dest=""
sources=""
compareopt=""
modeopt=""
groupopt=""
owneropt=""
destdir=""
destisdir=""
builddirlist=""
preserveopt=""
applyopt=""
donestatus=0


while [ "$1" ] ; do
 case "$1" in
  -mode)	
	if [ "$modeopt" -a "$modeopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	modeopt="$2"
	shift
	;;
  -owner)	
	if [ "$owneropt" -a "$owneropt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	owneropt="$2"
	shift
	;;
  -group)	
	if [ "$groupopt" -a "$groupopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	groupopt="$2"
	shift
	;;
  -apply)	
	if [ "$applyopt" -a "$applyopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	applyopt="$2"
	shift
	;;
  -rename)
	if [ "$renameopt" -a "$renameopt" != "$2" ] ; 
	then  badparameters "Ambiguous $1 options"; fi
	renameopt="$2"
	shift
	;;
  -traceonly)	
	doit="echo"
	traceit="echo"
	;;
  -trace)	
	doit="traceandshow"
	traceit="echo"
	;;
  -echo)	
	doit="traceandshow"
	traceit=":"
	;;
  -quiet)	
	commentit=":"
	doit=""
	traceit=":"
	;;
  -preserve)	
	preserveopt="true"
	;;
  -used)
	donestatus=1
	;;
  -compare)	
	compareopt="true"
	;;
  -usage)
	usage
	exit 0
	;;
  -*) 
	badparameters Unknown flag "$1"
	;;
  */) 	sources="$sources $dest"
	destdir=`dirname "$1."`
	dest=""
	destisdir="1$destisdir"
	;;
  *) 	
	sources="$sources $dest"
	dest="$1"
	;;
 esac
 shift
done
set - $sources
if [   0$destisdir -gt 1 \
	-o "$destisdir" -a "$dest"  \
	-o ! "$destisdir" -a "$renameopt" \
	-o ! "$destisdir" -a  $# -ne 1  ] ; then
	 badparameters Bad parameters 
fi
if [ "$dest" ] ; then destdir=`dirname $dest` ; fi
if [ "$preserveopt" ] ; then cpcmd="cp -p" ; else cpcmd="cp" ; fi


$traceit "	0=<$0>"
$traceit "	mycmdname=<$mycmdname>"
$traceit "	modeopt=<$modeopt>"
$traceit "	owneropt=<$owneropt>"
$traceit "	groupopt=<$groupopt>"
$traceit "	compareopt=<$compareopt>"
$traceit "	preserveopt=<$preserveopt>"
$traceit "	applyopt=<$applyopt>"
$traceit "	cpcmd=<$cpcmd>"
$traceit "	sources=<$sources>"
$traceit "	dest=<$dest>"
$traceit "	destisdir=<$destisdir>"
$traceit "	destdir=<$destdir>"


while [ "$1" ] ; do
 sourcefile="$1"
 shift 
 sourcename=`basename "$sourcefile"`
 $traceit "	sourcefile=<$sourcefile>"
 $traceit "	sourcename=<$sourcename>"
 if [ ! -r $sourcefile ] ; then
   echo "cannot read '$sourcefile'"  >&2
   exit 3
 fi
 if [ "$destisdir" ] ; then
  if [ "$renameopt" ] ; then
    if destsubname=`echo $sourcename | sed -e "s$renameopt"` &&
       [ "$destsubname" ] ; 
    then 
     dest="$destdir/$destsubname"
     destsubdir=`dirname $dest`
     destname=`basename $dest`
     $traceit "	destsubname=<$destsubname>"
    else 
     badparameters "Cannot rename '$sourcename' with: sed -e 's$renameopt'"
    fi
  else
    destname="$sourcename"
    destsubdir="$destdir"
    dest="$destdir/$destname"
  fi
 else
  destname=`basename $dest`
  destsubdir=`dirname $dest`
 fi
 $traceit "	dest=<$dest>"
 $traceit "	destname=<$destname>"
 $traceit "	destsubdir=<$destsubdir>"
 builddirlist=""
 subdir="$destsubdir"
 while [ ! -d "$subdir" ] ; do
   builddirlist="$subdir $builddirlist"
   subdir=`dirname "$subdir"`
 done
 $traceit "	builddirlist=<$builddirlist>"
 [ ! "$builddirlist" ] || $doit mkdir $builddirlist || exit 3 ;
 
 if [ "$compareopt" -a -r "$dest" ] && cmp -s $sourcefile $dest  ; then
 #  $commentit "${mycmdname}: $sourcefile == $dest"
   continue;
 fi
 donestatus=0
 desttmp="$destsubdir/#$$#$destname"
 $traceit "	desttmp=<$desttmp>"
 $commentit "${mycmdname}: $sourcefile -> $dest"
 if $doit $cpcmd "$sourcefile" "$desttmp" &&
    trap "$doit rm -f $desttmp" 0 && 
    if [ "$applyopt" -a ! -w "$desttmp" ] ; 
       then $doit chmod u+w "$desttmp" 2>/dev/null || true ; fi && 
    if [ "$applyopt" ] ; then $doit $applyopt "$desttmp" ; fi && 
    if [ "$owneropt" ] ; then $doit chown $owneropt "$desttmp" ; fi &&
    if [ "$groupopt" ] ; then $doit chgrp $groupopt "$desttmp" ; fi &&
    if [ "$modeopt" ] ; then $doit chmod $modeopt "$desttmp" ; fi &&
    $doit mv -f  "$desttmp" "$dest" &&
    trap 0 ; then : ; 
 else 
    echo "problem installing $sourcefile into $dest" >&2
    exit 3; fi
done

exit $donestatus