Home | History | Annotate | Download | only in common_files
      1 #!/bin/sh
      2 # CDDL HEADER START
      3 #
      4 # The contents of this file are subject to the terms of the
      5 # Common Development and Distribution License (the "License").
      6 # You may not use this file except in compliance with the License.
      7 #
      8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9 # or http://www.opensolaris.org/os/licensing.
     10 # See the License for the specific language governing permissions
     11 # and limitations under the License.
     12 #
     13 # When distributing Covered Code, include this CDDL HEADER in each
     14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15 # If applicable, add the following below this CDDL HEADER, with the
     16 # fields enclosed by brackets "[]" replaced with your own identifying
     17 # information: Portions Copyright [yyyy] [name of copyright owner]
     18 #
     19 # CDDL HEADER END
     20 #
     21 #
     22 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     23 #
     24 #
     25 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     26 # Use is subject to license terms.
     27 #
     28 # Note that this script imposes restriction on the format of the new file
     29 # that is delivered by the package.
     30 # The format has to be:
     31 # selection=enable idvendor=16 idproduct=32 cfgndx=1;
     32 # Each record is a separate line, all single spaces, and all values in
     33 # decimal
     34 #
     35 
     36 hex2dec()
     37 {
     38 	printf "ibase=16\n %s\n" $* | bc
     39 }
     40 
     41 CLEANUP_FILE=/tmp/CLEANUP
     42 TMP_FILE=/tmp/configmap.$$
     43 TMP_FILE1=/tmp/configmap1.$$
     44 export TMP_FILE
     45 
     46 while read src dst
     47 do
     48 
     49 # Now merge the contents of the old and the new CONF_MAP file.
     50 # src is the new file that this package is installing, and dst is the
     51 # file present on the system.
     52 # The algorithm to merge them is simple. We skim and filter out those entries
     53 # from dst file that we don't want from the src file. These entries are the
     54 # disabled entries and the ones that have idvendor and idproduct only.
     55 # This list of unwanted items is constructed as a grep string. Finally, we
     56 # do a "grep -v" of these items from src file, and add the remaining entries
     57 # to the dst file.
     58 #
     59 
     60 	CONF_MAP=$dst
     61 	CONF_MAP_OLD=${dst}.old
     62 	CONF_MAP_NEW=$src
     63 
     64 	if [ -f ${CONF_MAP} ]
     65 	then
     66 		# There is an existing file on the system.
     67 
     68 		# build a string of items that we don't want from the new file.
     69 		grepstr="^\*|^\#"
     70 		echo $grepstr > $TMP_FILE
     71 		# preprocess the old file
     72 		sed -e  \
     73 		'
     74 		# delete comments
     75 		/^#.*/d
     76 		/^\*.*/d
     77 		# change mulitple tabs to a single space
     78 		s/[	][	]*/ /g
     79 		# change multiple spaces to a single space
     80 		s/[ ][ ]*/ /g
     81 		# get rid of leading spaces
     82 		s/^ //g
     83 		# get rid of trailing spaces
     84 		s/[ ][ ]*$//g
     85 		# strip spaces around =
     86 		s/[ ]*=[ ]*/=/g
     87 		# put everything to lower case
     88 		y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
     89 		$CONF_MAP | 
     90 		# get rid of newlines
     91 		tr -s "\n" " " |
     92 		# make each record into a single line
     93 		tr ";" "\n" | 
     94 		# fish out entries that are enabled or disabled but have no 
     95 		# pathname/srno specified
     96 		egrep -v 'pathname|srno|^[ ]*$' |
     97 		while read line
     98 		do
     99 			vendorstr=""
    100 			productstr=""
    101 			for arg in $line
    102 			do
    103 				# var=${arg%=*}
    104 				# value=${arg#*=}
    105 				var=`expr $arg : '\(.*\)=.*' \| $arg`
    106 				value=`expr $arg : '.*=\(.*\)' \| $arg`
    107 				case $var in
    108 					idvendor)
    109 						# val=${value#0x}
    110 						val=`expr $value : \
    111 							'[0][xX]\(.*\)' \
    112 							\| $value`
    113 						if [ "$value" != "$val" ]
    114 						then
    115 							# hex -> dec
    116 							val=`hex2dec $val`
    117 						fi
    118 						vendorstr="idvendor=$val"
    119 					;;
    120 					idproduct)
    121 						# val=${value#0x}
    122 						val=`expr $value : \
    123 							'[0][xX]\(.*\)' \
    124 							\| $value`
    125 						if [ "$value" != "$val" ]
    126 						then
    127 							# hex -> dec
    128 							val=`hex2dec $val`
    129 						fi
    130 						productstr="idproduct=$val"
    131 					;;
    132 				esac
    133 			done
    134 			# build the grep str
    135 			if [ -n "$vendorstr" ] || [ -n "$productstr" ]
    136 			then
    137 				grepstr="$grepstr|$vendorstr $productstr"
    138 			fi
    139 			echo $grepstr > $TMP_FILE
    140 		done
    141 
    142 		# grepstr now contains all the entries that we don't want
    143 		# from new CONF_MAP
    144 		cp $CONF_MAP $TMP_FILE1
    145 		egrep -vi -f $TMP_FILE  $CONF_MAP_NEW >> $TMP_FILE1
    146 		rm $TMP_FILE
    147 
    148 		# Now, TMP_FILE1 is the real new config file to be stored.
    149 
    150 		# If the existing file and the new one are identical,
    151 		# nothing will be done.
    152 		cmp -s ${CONF_MAP} ${TMP_FILE1}
    153 		if [ $? = 0 ]
    154 		then
    155 			rm ${TMP_FILE1}
    156 			continue
    157 		fi
    158 
    159 		# The existing file is different with the new one. Save it as .old
    160 		# if .old exists, move it to .old.xxx
    161 		if [ -f ${CONF_MAP_OLD} ]
    162 		then
    163 			# suffix is day, hr, min, sec
    164 			# - it should be unique enough
    165 			suffix=`date '+%d%H''%M''%S'`
    166 			mv ${CONF_MAP_OLD} ${CONF_MAP_OLD}.$suffix
    167 			echo "EXISTING_FILE_RENAMED: ${CONF_MAP_OLD} "\
    168 			    "${CONF_MAP_OLD}.$suffix" >>  ${CLEANUP_FILE}
    169 		fi
    170 		cp $CONF_MAP $CONF_MAP_OLD
    171 		echo "EXISTING_FILE_RENAMED: ${CONF_MAP} ${CONF_MAP_OLD}"\
    172 		    >> ${CLEANUP_FILE}
    173 
    174 		# Replace the config file with the new one.
    175 		mv $TMP_FILE1 $CONF_MAP
    176 
    177 	else
    178 		# we are newly installing this file
    179 		cp $CONF_MAP_NEW $CONF_MAP
    180 	fi
    181 done
    182 exit 0
    183