Home | History | Annotate | Download | only in scripts
      1 #!/bin/ksh
      2 
      3 # This version of 'remove-gnome' does not prompt the user when '-q' is
      4 # present on the command line. This allows the script to be incorporated into
      5 # other scripts.
      6 #
      7 # Modified by Damien Carbery, 19 May 2003.
      8 # -f option added by Laca, 25 Feb 2004
      9 # jds support added by Laca, 10 Mar 2004
     10 
     11 PKGRM=/usr/sbin/pkgrm
     12 PKGINFO=/usr/bin/pkginfo
     13 ADMIN=/tmp/.pkgrm.$$.admin
     14 PRODREG_SCRIPT=/tmp/prodreg.$$.sed.tmpl
     15 
     16 PRODREG_SCRIPT_CREATED=no
     17 TMP_PRODREG_SCRIPT_CREATED=no
     18 TMP_PRODREG_CREATED=no
     19 ADMIN_CREATED=no
     20 MYNAME="$0"
     21 MYDIR=`dirname $0`
     22 MYDIR=`( cd $MYDIR; /usr/bin/pwd )`
     23 MYNAME=$(basename $0)
     24 MYARGS="$*"
     25 QUIET=0
     26 FORCE=0
     27 RM_EXTRA_PKGS=1
     28 
     29 # List of JDS packages without JDS category. Will be removed if specified
     30 # category is JDS and the packages are present.
     31 EXTRA_PKGS_TO_REMOVE="SUNWmozgm SUNWmoznss SUNWmozpsm SUNWgnome-l10ndocument-ja SUNWgnome-l10nmessages-ja SUNWsogm SUNWsom SUNWsoagm SUNWsoam"
     32 
     33 
     34 usage () {
     35   echo "$0 [-h|--help|-q|--quiet|-f|--force|--version 2.0|1.4|JDS|JDS<n>|-R rootdir]"
     36   echo "Remove GNOME packages."
     37   echo "   -h, --help     display this help"
     38   echo "   --version x    remove version x (default: 2.0)"
     39   echo "   --quiet, -q    don't prompt for confirmation before deletion."
     40   echo "   --force, -f    ignore any errors and continue."
     41   echo "   -R rootdir     remove packages from an alternative root directory."
     42   echo "   --no_extras, -n don't remove extra packages, only jds/gnome ones."
     43   exit 1
     44 }
     45 
     46 GNOME_CATEGORY=GNOME2
     47 GNOME_VERSION=2.0
     48 version_opt=2.0
     49 # If the script is called 'remove-jds' the category will include all JDS
     50 # components.
     51 if [ "x$MYNAME" = xremove-jds ]; then
     52   GNOME_CATEGORY="JDSosol,JDS,JDS2,JDS3,JDS3x,JDS4,JDS5,JDS6,JDS7,JDS8,JDS9,APOC,EVO146,EVO25,GLOW,JAI,JAVAAPPS,MUSCLE,FF15,FIREFOX,TB15,"
     53   version_opt=jds
     54 fi
     55 
     56 while [ $# != 0 ]; do
     57   case "$1" in
     58   --help | -h )
     59 	usage
     60 	;;
     61   --version )
     62 	shift
     63 	if [ $# = 0 ]; then
     64 	  echo "Error: argument expected after --version"
     65 	  usage
     66 	fi
     67         case "$1" in
     68 	1.4 )
     69 		GNOME_CATEGORY=GNOME
     70 		GNOME_VERSION=1.4
     71 		version_opt=1.4
     72 		;;
     73 	2.0 )
     74 		;;
     75 	JDS|jds)
     76                 GNOME_VERSION="2.x (JDS)"
     77 		GNOME_CATEGORY="JDS,JDS2,JDS3,JDS4,JDS5,JDS6,JDS7,JDS8,JDS9,JDS3x,JDSosol"
     78 		version_opt=jds
     79 		;;
     80 	JDS[0-9]|jds[0-9])
     81 	        jdsrel=`echo $1 | cut -c4-`
     82 	        GNOME_VERSION="2.x (JDS Release $jdsrel)"
     83 		GNOME_CATEGORY="JDS$jdsrel"
     84 		version_opt=$1
     85 		;;
     86 	opensolaris)
     87 	        GNOME_VERSION="OpenSolaris Desktop"
     88 		GNOME_CATEGORY="JDS3x,JDSosol"
     89 		version_opt=$1
     90 		;;
     91 	* )
     92 		echo "Error: version should be one of 1.4, 2.0, opensolaris or JDS"
     93 		usage
     94 		;;
     95 	esac
     96 	;;
     97   --quiet | -q )
     98 	QUIET=1
     99 	;;
    100   --force | -f )
    101 	FORCE=1
    102 	;;
    103   -R )
    104 	shift
    105 	ROOTDIR=$1
    106 	if [ "x$ROOTDIR" = x ]; then
    107 	    echo "Option -R requires an argument"
    108 	    usage
    109 	fi
    110 	;;
    111   --no_extras | -n )
    112 	RM_EXTRA_PKGS=0
    113 	;;
    114   * )
    115 	echo "Error: $1: invalid argument"
    116 	usage
    117 	;;
    118   esac
    119   shift
    120 done
    121 
    122 backup () {
    123   if [ -e "$1" ]; then
    124     backup "$1~"
    125     echo "Saving file $1 as $1~"
    126     mv "$1" "$1~" || msg_error "Failed to back up file $1"
    127   fi
    128 }
    129 
    130 clean_up () {
    131   if [ "x$PRODREG_SCRIPT_CREATED" = xyes ]; then
    132     rm -f $PRODREG_SCRIPT
    133   fi
    134   if [ "x$TMP_PRODREG_SCRIPT_CREATED" = xyes ]; then
    135     rm -f $TMP_PRODREG_SCRIPT
    136   fi
    137   if [ "x$TMP_PRODREG_CREATED" = xyes ]; then
    138     rm -f $TMP_PRODREG
    139   fi
    140   if [ "x$ADMIN_CREATED" = xyes ]; then
    141     rm -f $ADMIN
    142   fi
    143   case "$MYNAME" in
    144   /tmp/remove-gnome.copy.* )
    145 	rm -f $MYNAME
    146 	;;
    147   esac
    148 }
    149 
    150 clean_up_and_abort () {
    151   clean_up
    152   echo "Interrupted."
    153   exit 1
    154 }
    155 
    156 msg_error () {
    157   echo $*
    158   if [ $FORCE == 0 ]; then
    159     echo "Use for -f or --force option to ignore errors."
    160     echo "Exiting..."
    161     exit 1
    162   else
    163     WARNINGS=yes
    164   fi
    165 }
    166 
    167 msg_noerror () {
    168   echo $*
    169   exit 0
    170 }
    171 
    172 WARNINGS=no
    173 msg_warning () {
    174   echo $*
    175   WARNINGS=yes
    176 }
    177 
    178 /usr/bin/id | /usr/bin/grep '^uid=0(' > /dev/null 2>&1
    179 USER_IS_ROOT=$?
    180 
    181 profiles | fgrep -sx "Software Installation"
    182 haveinstallprofile=$?
    183 
    184 if [ $USER_IS_ROOT != 0 -a $haveinstallprofile != 0 ]; then
    185     echo "WARNING: Run this script as root or make sure you have been assigned"
    186     echo "the 'Software Installation' profile to be able to uninstall packages."
    187     echo "See the user_attr(4) and profiles(1) man pages for more details"
    188 fi
    189 
    190 if [ $FORCE -gt 0 ]; then
    191   trap clean_up_and_abort  HUP INT TERM
    192 else
    193   trap clean_up_and_abort  HUP INT TERM ERR
    194 fi
    195 
    196 trap clean_up  QUIT EXIT
    197 
    198 case "$MYDIR" in
    199 */sbin )
    200 	cp $MYNAME /tmp/remove-gnome.copy.$$
    201 	chmod 755 /tmp/remove-gnome.copy.$$
    202 	exec /tmp/remove-gnome.copy.$$ ${MYARGS} --version $version_opt
    203 	;;
    204 esac
    205 
    206 backup "$PRODREG_SCRIPT"
    207 
    208 cat > $PRODREG_SCRIPT << EOF
    209 :compid
    210 /^[ 	]*<compid>/{
    211 	N
    212 	/<\/compid>/!b compid
    213 	/.*<uniquename>@PACKAGE_TO_REMOVE@\n[ 	]*<\/uniquename>.*$/d
    214 }
    215 EOF
    216 PRODREG_SCRIPT_CREATED=yes
    217 
    218 TMP_PRODREG_SCRIPT=/tmp/prodreg.$$.sed
    219 backup $TMP_PRODREG_SCRIPT
    220 
    221 backup "$ADMIN"
    222 cat > $ADMIN << EOF
    223 mail=
    224 runlevel=nocheck
    225 conflict=nocheck
    226 setuid=nocheck
    227 action=nocheck
    228 partial=nocheck
    229 idepend=nocheck
    230 rdepend=nocheck
    231 space=quit
    232 EOF
    233 
    234 ADMIN_CREATED=yes
    235 
    236 echo "This script will remove packages belonging to GNOME $GNOME_VERSION"
    237 echo
    238 echo "Looking for packages..."
    239 if [ "x$ROOTDIR" != x ]; then
    240     TMP_PKGS=`$PKGINFO -R "$ROOTDIR" -c $GNOME_CATEGORY | grep -v '^JDS[ 	]*CBE'`|| msg_noerror "No packages found."
    241 else
    242     TMP_PKGS=`$PKGINFO -c $GNOME_CATEGORY | grep -v '^JDS[ 	]*CBE'`|| msg_noerror "No packages found."
    243 fi
    244 
    245 # Add JDS extra packages that do not have the JDS category.
    246 if [ $RM_EXTRA_PKGS -eq 1 ]
    247 then
    248     if [ $version_opt == "jds" ]
    249     then
    250         # Turn off traps - '$PKGINFO -q $pkg' triggers ERR if $pkg not installed.
    251         trap " " HUP INT TERM ERR
    252 
    253         for pkg in $EXTRA_PKGS_TO_REMOVE
    254         do
    255             # If package is present then add to list to be removed.
    256             $PKGINFO -q $pkg
    257             if [ $? -eq 0 ]
    258             then
    259                 # Later code expects $PKGINFO style output which contains the
    260                 # "Category package_name Description"
    261                 TMP_PKGS="$TMP_PKGS
    262 GNOME2      $pkg               Dummy description"
    263             fi
    264         done
    265 
    266         # Restore the original trap triggers.
    267         if [ $FORCE -gt 0 ]; then
    268           trap clean_up_and_abort  HUP INT TERM
    269         else
    270           trap clean_up_and_abort  HUP INT TERM ERR
    271         fi
    272     fi
    273 fi
    274 
    275 if [ $QUIET -eq 0 ]; then
    276   ( echo "The following packages were found:"
    277     echo "$TMP_PKGS" ) | more
    278   echo
    279 
    280   answer=
    281 
    282   while [ "x$answer" = x ]; do
    283     echo "Would you like to remove the above packages? (y/n)"
    284     read answer
    285   done
    286 
    287   if [ "x$answer" != "xy" -a "x$answer" != "xY" ]; then
    288     msg_error "Cancelled."
    289   fi
    290 else
    291   # Simply list the packages.
    292   echo "The following packages were found:"
    293   echo "$TMP_PKGS"
    294 fi
    295 
    296 
    297 # Packages need to be removed and we must be root to do this.
    298 if [ $USER_IS_ROOT != 0 -a $haveinstallprofile != 0 ]; then
    299   msg_error "ERROR: You must be root or have the 'Software Installation' profile to uninstall packages."
    300 fi
    301 
    302 PRODREG=
    303 # find the productregistry file
    304 if [ $version_opt = "1.4" ]; then
    305     IFS=' 	
    306 '
    307     for prodreg in	/var/sadm/install/productregistry \
    308 	/var/sadm/install/productregistry.xml; do
    309       if [ -s $prodreg ]; then
    310 	  PRODREG=$prodreg
    311       fi
    312     done
    313 
    314     if [ -n "$PRODREG" ]; then
    315 	TMP_PRODREG=/tmp/productregistry.tmp.$$
    316 
    317 	backup $TMP_PRODREG
    318 
    319 	cp $PRODREG $TMP_PRODREG || \
    320 		msg_error "Error copying $PRODREG to $TMP_PRODREG"
    321 	TMP_PRODREG_CREATED=yes
    322 
    323 	backup $PRODREG
    324 	cp $TMP_PRODREG $PRODREG || \
    325 		msg_error "Error copying $TMP_PRODREG to $PRODREG"
    326     fi
    327 fi
    328 
    329 echo "Removing packages..."
    330 
    331 ALL_PKGS=`echo "$TMP_PKGS" | /usr/bin/tr '\t' ' ' | \
    332 	/usr/bin/sed -e 's/  */ /g' | /usr/bin/cut -f2 -d' ' | sort -r`
    333 ALL_PKGS=`echo $ALL_PKGS`
    334 
    335 IFS=' '
    336 for pkg in $ALL_PKGS; do
    337   if [ "x$ROOTDIR" != x ]; then
    338     /usr/bin/pfexec $PKGRM -R "$ROOTDIR" -a $ADMIN -n $pkg || \
    339 	msg_error "ERROR: Failed to remove package $pkg"
    340   else
    341     /usr/bin/pfexec $PKGRM -a $ADMIN -n $pkg || \
    342 	msg_error "ERROR: Failed to remove package $pkg"
    343   fi
    344   if [ -n "$PRODREG" ]; then
    345     cp $PRODREG $TMP_PRODREG && \
    346       /bin/sed -e "s/@PACKAGE_TO_REMOVE@/$pkg/g" $PRODREG_SCRIPT > $TMP_PRODREG_SCRIPT && \
    347       TMP_PRODREG_SCRIPT_CREATED=yes && \
    348       /bin/sed -f $TMP_PRODREG_SCRIPT $TMP_PRODREG > $PRODREG ||
    349       msg_warning "Warning: Failed to update the product registry file: $PRODREG"
    350   fi
    351 done
    352 
    353 if [ "x$WARNINGS" = xyes ]; then
    354 	echo "Completed with warnings. Some packages could not be removed."
    355 else
    356 	echo "Successfully removed all packages."
    357 fi
    358 
    359 if [ -n "$PRODREG" ]; then
    360   # Delete backup product registry if unchanged.
    361   `/usr/bin/cmp -s $PRODREG $PRODREG~`
    362   if [ $? -eq 0 ]; then
    363     echo "Product registry unchanged, removing backup file."
    364     rm $PRODREG~
    365   fi
    366 
    367   /bin/grep '<compid>' $PRODREG > /dev/null 2>&1 || rm -f $PRODREG
    368 fi
    369