blob: 98341039a8c2bf03dc07389150cf82d31d3ba033 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:$PATH"
2
3usage()
4{
5cat << !EOF >&2
6$0: [options] [src] dst
7 -v : output actions performed.
8 -e env-var : only take action if env-var is set to "y".
9 -E env-var : only take action if env-var is not set to "y".
10 -o option : only take action if option is set to "y".
11 -O option : only take action if option is not set to "y".
12 -c : process with cpp+cflags
13 -p perms : chmod style permissions for dst.
14 -S : don't strip after installing
15 -a text : append text to dst.
16 -A pattern : only append text if pattern doesn't exist in file
17 -d : make dst directory if it doesn't exist
18 -l link : dst is a hard link to 'link'.
19 -s sym-link : dst is a sym-link to 'sym-link'.
20 -M : install kernel module into dst subdir of module dir
21 -r : root directory of install (default ROMFSDIR)
22 -f : do not follow symlinks
23
24 if "src" is not provided, basename is run on dst to determine the
25 source in the current directory.
26
27 multiple -e and -o options are ANDed together. To achieve an OR affect
28 use a single -e/-o with 1 or more y/n/"" chars in the condition.
29
30 if src is a directory, everything in it is copied recursively to dst
31 with special files removed (currently CVS and Subversion dirs).
32!EOF
33 exit 1
34}
35
36#############################################################################
37
38setperm()
39{
40 rc=0
41 chmod u+w ${ROMFSDIR}${dst}
42 if [ "$perm" ]
43 then
44 [ "$v" ] && echo "chmod ${perm} ${ROMFSDIR}${dst}"
45 chmod ${perm} ${ROMFSDIR}${dst}
46 rc=$?
47 fi
48 return $rc
49}
50
51#############################################################################
52
53file_append()
54{
55 touch ${ROMFSDIR}${dst} || return 1
56 if [ -z "${pattern}" ] && grep -F "${src}" ${ROMFSDIR}${dst} > /dev/null
57 then
58 [ "$v" ] && echo "File entry already installed."
59 elif [ "${pattern}" ] && egrep "${pattern}" ${ROMFSDIR}${dst} > /dev/null
60 then
61 [ "$v" ] && echo "File pattern already installed."
62 else
63 [ "$v" ] && echo "Installing entry into ${ROMFSDIR}${dst}."
64 if [ -s ${ROMFSDIR}${dst} ] ; then
65 if [ $(tail -n1 ${ROMFSDIR}${dst} | tr -d '\n' | wc -c) = $(tail -n1 ${ROMFSDIR}${dst} | wc -c) ] ; then
66 echo "" >> ${ROMFSDIR}${dst} || return 1
67 fi
68 fi
69 echo "${src}" >> ${ROMFSDIR}${dst} || return 1
70 fi
71 setperm ${ROMFSDIR}${dst}
72 return $?
73}
74
75#############################################################################
76
77file_copy()
78{
79 rc=0
80 if [ -d "${src}" ]
81 then
82 [ "$v" ] && echo "CopyDir ${src} ${ROMFSDIR}${dst}"
83 (
84 cd ${src} || return 1
85 V=
86 [ "$v" ] && V=v
87 find . -print | grep -E -v '/CVS|/\.svn' | cpio -p${V}dum${follow} ${ROMFSDIR}${dst}
88 rc=$?
89 find . -print | grep -E -v '/CVS|/\.svn' | ( cd ${ROMFSDIR}${dst}; xargs chmod u+w )
90 setperm ${ROMFSDIR}${dst}
91 find . -type f | grep -E -v '/CVS|/\.svn|\.ko$' | while read t; do
92 if [ -n "$strip" ]; then
93 ${STRIPTOOL} ${ROMFSDIR}${dst}/$t 2>/dev/null
94 ${STRIPTOOL} -R .comment -R .note ${ROMFSDIR}${dst}/$t 2>/dev/null
95 fi
96 done
97 )
98 else
99 if [ -d ${ROMFSDIR}${dst} ]; then
100 dstfile=${ROMFSDIR}${dst}/`basename ${src}`
101 else
102 dstfile=${ROMFSDIR}${dst}
103 fi
104 rm -f ${dstfile}
105 [ "$v" ] && echo "cp ${src} ${dstfile}"
106 cp ${src} ${dstfile} && setperm ${dstfile}
107 rc=$?
108 if [ $rc -eq 0 -a -n "$strip" ]; then
109 ${STRIPTOOL} ${dstfile} 2>/dev/null
110 ${STRIPTOOL} -R .comment -R .note ${dstfile} 2>/dev/null
111 fi
112 fi
113 return $rc
114}
115
116#############################################################################
117
118sym_link()
119{
120 rm -f ${ROMFSDIR}${dst}
121 [ "$v" ] && echo "ln -s ${src} ${ROMFSDIR}${dst}"
122 ln -sf ${src} ${ROMFSDIR}${dst}
123 return $?
124}
125
126#############################################################################
127
128hard_link()
129{
130 rm -f ${ROMFSDIR}${dst}
131 [ "$v" ] && echo "ln ${src} ${ROMFSDIR}${dst}"
132 ln ${ROMFSDIR}${src} ${ROMFSDIR}${dst}
133 return $?
134}
135
136#############################################################################
137
138cpp_file()
139{
140 set -x
141 if [ -d ${ROMFSDIR}${dst} ]; then
142 dstfile=${ROMFSDIR}${dst}/`basename ${src}`
143 else
144 dstfile=${ROMFSDIR}${dst}
145 fi
146 rm -f ${dstfile}
147 [ "$v" ] && echo "${CROSS_COMPILE}cpp ${CFLAGS} -P < ${src} > ${dstfile}"
148 ${CROSS_COMPILE}cpp ${CFLAGS} -P < ${src} > ${dstfile}
149 return $?
150}
151
152#############################################################################
153#
154# main program entry point
155#
156
157v=
158option=y
159noption=
160pattern=
161perm=
162func=file_copy
163mdir=
164src=
165dst=
166strip=1
167kernmod=
168r=
169follow=L
170
171while getopts 'dfSMvce:E:o:O:A:p:a:l:s:r:' opt "$@"
172do
173 case "$opt" in
174 v) v="1"; ;;
175 d) mdir="1"; ;;
176 f) follow=; ;;
177 S) strip=; ;;
178 M) kernmod="1"; ;;
179 o) option="$OPTARG"; ;;
180 O) noption="$OPTARG"; ;;
181 e) eval option=\"\$$OPTARG\"; ;;
182 E) eval noption=\"\$$OPTARG\"; ;;
183 p) perm="$OPTARG"; ;;
184 a) src="$OPTARG"; func=file_append; ;;
185 A) pattern="$OPTARG"; ;;
186 l) src="$OPTARG"; func=hard_link; ;;
187 s) src="$OPTARG"; func=sym_link; ;;
188 r) ROMFSDIR="$OPTARG"; r=1; ;;
189 c) func=cpp_file; ;;
190
191 *) break ;;
192 esac
193#
194# process option here to get an ANDing effect
195#
196 case "$option" in
197 *[mMyY]*) # this gives OR effect, ie., nYn
198 ;;
199 *)
200 [ "$v" ] && echo "Condition not satisfied."
201 exit 0
202 ;;
203 esac
204
205#
206# process negative options here to get an ANDing effect
207#
208 case "${noption:-n}" in
209 *[nN]*) # this gives OR effect, ie., yNy
210 ;;
211 *)
212 [ "$v" ] && echo "Condition not satisfied."
213 exit 0
214 ;;
215 esac
216done
217
218if [ -z "$ROMFSDIR" -a -z "$r" ]
219then
220 echo "ROMFSDIR is not set" >&2
221 usage
222 exit 1
223fi
224
225if [ -z "$STRIPTOOL" ]
226then
227 STRIPTOOL=strip
228fi
229
230shift `expr $OPTIND - 1`
231
232case $# in
2331)
234 dst="$1"
235 if [ -z "$src" ]
236 then
237 src="`basename $dst`"
238 fi
239 ;;
2402)
241 if [ ! -z "$src" ]
242 then
243 echo "Source file already provided" >&2
244 exit 1
245 fi
246 src="$1"
247 dst="$2"
248 ;;
249*)
250 usage
251 ;;
252esac
253
254if [ -n "$kernmod" ]; then
255 strip=
256 kerndir=${ROOTDIR}/${LINUXDIR}
257 # could prob take from UTS headers as well ...
258 kernver=$(cat ${kerndir}/include/config/kernel.release)
259 dst="/lib/modules/${kernver}/${dst}"
260fi
261
262if [ "$mdir" -a ! -d "`dirname ${ROMFSDIR}${dst}`/." ]
263then
264 mkdir -p "`dirname ${ROMFSDIR}${dst}`/." || exit 1
265fi
266
267$func || exit 1
268
269exit 0
270
271#############################################################################