lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:$PATH" |
| 2 | |
| 3 | usage() |
| 4 | { |
| 5 | cat << !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 | |
| 38 | setperm() |
| 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 | |
| 53 | file_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 | |
| 77 | file_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 | |
| 118 | sym_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 | |
| 128 | hard_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 | |
| 138 | cpp_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 | |
| 157 | v= |
| 158 | option=y |
| 159 | noption= |
| 160 | pattern= |
| 161 | perm= |
| 162 | func=file_copy |
| 163 | mdir= |
| 164 | src= |
| 165 | dst= |
| 166 | strip=1 |
| 167 | kernmod= |
| 168 | r= |
| 169 | follow=L |
| 170 | |
| 171 | while getopts 'dfSMvce:E:o:O:A:p:a:l:s:r:' opt "$@" |
| 172 | do |
| 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 |
| 216 | done |
| 217 | |
| 218 | if [ -z "$ROMFSDIR" -a -z "$r" ] |
| 219 | then |
| 220 | echo "ROMFSDIR is not set" >&2 |
| 221 | usage |
| 222 | exit 1 |
| 223 | fi |
| 224 | |
| 225 | if [ -z "$STRIPTOOL" ] |
| 226 | then |
| 227 | STRIPTOOL=strip |
| 228 | fi |
| 229 | |
| 230 | shift `expr $OPTIND - 1` |
| 231 | |
| 232 | case $# in |
| 233 | 1) |
| 234 | dst="$1" |
| 235 | if [ -z "$src" ] |
| 236 | then |
| 237 | src="`basename $dst`" |
| 238 | fi |
| 239 | ;; |
| 240 | 2) |
| 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 | ;; |
| 252 | esac |
| 253 | |
| 254 | if [ -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}" |
| 260 | fi |
| 261 | |
| 262 | if [ "$mdir" -a ! -d "`dirname ${ROMFSDIR}${dst}`/." ] |
| 263 | then |
| 264 | mkdir -p "`dirname ${ROMFSDIR}${dst}`/." || exit 1 |
| 265 | fi |
| 266 | |
| 267 | $func || exit 1 |
| 268 | |
| 269 | exit 0 |
| 270 | |
| 271 | ############################################################################# |