3f0890
#!/bin/bash
3f0890
config=$1
3f0890
target=$2
3f0890
10d287
debug(){
3f0890
  if [ "x$debug" == "xtrue" ] ; then
10d287
    echo "$@"
3f0890
  fi
10d287
}
3f0890
10d287
debug "cjc: bash debug is on"
10d287
10d287
cmdvDebug() {
3f0890
  if [ "x$debug" == "xtrue" ] ; then
10d287
    "$@" -v
10d287
  else
10d287
    "$@" 1>/dev/null 2>&1
3f0890
  fi
3f0890
}
3f0890
10d287
mvDebug() {
10d287
  cmdvDebug mv "$@"
10d287
}
10d287
10d287
rmDebug() {
10d287
  cmdvDebug rm "$@"
10d287
}
10d287
10d287
rmdirDebug() {
10d287
 cmdvDebug rmdir "$@"
10d287
}
10d287
3f0890
#we should be pretty strict about removing once used (even "used" [with fail]) config file, as it may corrupt another installation
3f0890
clean(){
3f0890
  debug "cleanup: removing $config"
10d287
  rmDebug -rf $config
3f0890
}
3f0890
3f0890
if [ "x" == "x$config" ] ; then
3f0890
  debug "no config file specified"
3f0890
  exit 1
3f0890
fi
3f0890
3f0890
if [ ! -f  "$config" ] ; then
3f0890
  debug "$config file do not exists"
3f0890
  # expected case, when no migration happened
3f0890
  exit 0
3f0890
fi 
3f0890
3f0890
if [ "x" == "x$target" ] ; then
3f0890
  debug "no target dir specified"
3f0890
  clean
3f0890
  exit 2
3f0890
fi
3f0890
3f0890
if [ ! -d  "$target" ] ; then
3f0890
  debug "$target is not directory"
3f0890
  clean
3f0890
  exit 22
3f0890
fi 
3f0890
3f0890
source=`cat $config` 
3f0890
3f0890
if [ "x" == "x$source" ] ; then
3f0890
  debug "no information in $config"
3f0890
  clean
3f0890
  exit 3
3f0890
fi
3f0890
3f0890
if [ ! -d  "$source" ] ; then
3f0890
  debug "$source from $config is not directory"
3f0890
  clean
3f0890
  exit 33
3f0890
fi 
3f0890
3f0890
3f0890
listLinks(){
3f0890
  find $1 -type l -print0 | xargs -0 ls -ld | sed "s; \+-> \+;_->_;g" | sed "s;.* $1;$1;"
3f0890
}
3f0890
3f0890
printIfExists(){
3f0890
  if [ -e $ffileCandidate ] ; then
3f0890
    echo $1
3f0890
  else
3f0890
    # stdout can be captured, therefore stderr
3f0890
    debug "skipping not-existing link-target-dir $1" 1>&2
3f0890
  fi
3f0890
}
3f0890
3f0890
createListOfLinksTargetsDirectories(){
3f0890
  pushd $source >/dev/null 2>&1 
3f0890
    local links=`listLinks $1`
3f0890
    for x in $links ; do 
3f0890
      echo "$x" | grep "jre-abrt" > /dev/null
3f0890
      if [ $? -eq 0 ] ; then
3f0890
        continue
3f0890
      fi
3f0890
      local ffileCandidate=$(echo $x | sed "s/.*_->_//") ;
3f0890
# ignoring relative paths as they may lead who know where later   
3f0890
# there can be simlink relative to position, so push is not catching all
3f0890
      if [ "$ffileCandidate" != "${ffileCandidate#/}" ] ; then
3f0890
        if [ -d $ffileCandidate ] ; then
3f0890
# should we accept the links to directories themselves?
3f0890
          printIfExists $ffileCandidate
3f0890
        else
3f0890
          printIfExists `dirname $ffileCandidate`
3f0890
        fi
3f0890
      fi
3f0890
    done | sort | uniq
3f0890
  popd >/dev/null 2>&1 
3f0890
}
3f0890
3f0890
sourceLinks=`listLinks $source`
3f0890
targetLinks=`listLinks $target`
3f0890
sourceLinksDirsTarget=`createListOfLinksTargetsDirectories  $source`
3f0890
targetLinksDirsTarget=`createListOfLinksTargetsDirectories  $target`
3f0890
3f0890
debug "source: $source"
3f0890
debug "target: $target"
3f0890
3f0890
debug "sourceLinks:
3f0890
$sourceLinks"
3f0890
debug "targetLinks:
3f0890
$targetLinks"
3f0890
3f0890
debug "sourceLinksDirsTarget:
3f0890
$sourceLinksDirsTarget"
3f0890
debug "targetLinksDirsTarget:
3f0890
$targetLinksDirsTarget"
3f0890
3f0890
sourceSearchPath="$source $sourceLinksDirsTarget"
3f0890
targetSearchPath="$target $targetLinksDirsTarget"
3f0890
3f0890
work(){
3f0890
  if [ "X$1" == "Xrpmnew" -o "X$1" == "Xrpmorig" ] ; then
3f0890
    debug "Working with $1 (1)"
3f0890
  else
3f0890
    debug "unknown parameter: $1"
3f0890
    return 1
3f0890
  fi
3f0890
3f0890
  local files=`find $targetSearchPath | grep "\\.$1$"`
3f0890
  for file in $files ; do
3f0890
    local sf1=`echo $file | sed "s/\\.$1$//"`
3f0890
    local sf2=`echo $sf1 | sed "s/$targetName/$srcName/"`
3f0890
    # was file modified in origianl installation?
3f0890
    rpm -Vf $source | grep -q $sf2
3f0890
    if [ $? -gt 0 ] ; then
3f0890
     if [ "X$1" == "Xrpmnew" ] ; then
3f0890
       debug "$sf2 was NOT modified, removing possibly corrupted $sf1 and renaming from $file"
10d287
       mvDebug -f $file $sf1
3f0890
       if [ $? -eq 0 ] ; then
3f0890
         echo "restored $file to $sf1"
3f0890
       else
10d287
         debug "FAILED to restore $file to $sf1"
3f0890
       fi
3f0890
    fi
3f0890
     if [ "X$1" == "Xrpmorig" ] ; then
3f0890
       debug "$sf2 was NOT modified, removing possibly corrupted $file"
10d287
       rmDebug $file
3f0890
    fi
3f0890
    else
3f0890
     debug "$sf2 was modified, keeping $file, and removing the duplicated original"
3f0890
     # information is now backuped, in new directory anyway. Removing future rpmsave to allow rpm -e
10d287
     rmDebug -f $sf2
3f0890
     # or its corresponding backup
10d287
     rmDebug -f $sf2.$1
3f0890
    fi
3f0890
done
3f0890
}
3f0890
3f0890
3f0890
srcName=`basename $source`
3f0890
targetName=`basename $target`
3f0890
3f0890
work rpmnew
3f0890
work rpmorig
3f0890
3f0890
debug "Working with rpmorig (2)"
3f0890
# simply moving old rpmsaves to new dir
3f0890
# fix for config (replace) leftovers
3f0890
files=`find $sourceSearchPath | grep "\\.rpmorig$"`
3f0890
  for file in $files ; do
3f0890
    rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"`
3f0890
    debug "relocating $file to $rpmsaveTarget"
3f0890
    if [ -e $rpmsaveTarget ] ; then
10d287
      rmDebug $file
3f0890
    else
10d287
      mvDebug $file $rpmsaveTarget
3f0890
    fi
3f0890
  done
3f0890
3f0890
debug "Working with rpmsave (1)"
3f0890
files=`find $sourceSearchPath | grep "\\.rpmsave$"`
3f0890
  for file in $files ; do
3f0890
    rpmsaveTarget=`echo $file | sed "s/$srcName/$targetName/"`
3f0890
    debug "relocating $file to $rpmsaveTarget"
3f0890
    if [ -e $rpmsaveTarget ] ; then
10d287
      rmDebug $file
3f0890
    else
10d287
      mvDebug $file $rpmsaveTarget
3f0890
    fi
3f0890
  done
3f0890
3f0890
3f0890
#warning: file /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64-debug/jre/lib/applet: remove failed: No such file or directory
3f0890
#warning: file /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64-debug/jre/lib/amd64/client: remove failed: No such file or directory
3f0890
#warning: file /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64/jre/lib/amd64/xawt: remove failed: No such file or directory
3f0890
#those dirs might be mepty by installtion, filling to not be rmeoved later
3f0890
#use exported CJC_BLACKDIRS_ADD to extend it in runtime/spec file
3f0890
blackdirs=""
3f0890
internal_blackdirs="jre/lib/applet jre/lib/*/client jre/lib/locale/*/LC_MESSAGES jre/lib/*/xawt jre/javaws properties/version properties jre/lib/endorsed jre/lib/boot lib/missioncontrol/p2/org.eclipse.equinox.p2.engine/profileRegistry/JMC.profile/.data"
3f0890
for x in $internal_blackdirs $CJC_BLACKDIRS_ADD ; do 
3f0890
  blackdirs="$blackdirs $source/$x"
3f0890
done
3f0890
3f0890
for blackdir in $blackdirs; do
3f0890
  if [ -e $blackdir ] ; then
3f0890
    debug "nasty $blackdir  exists, filling"
3f0890
    touch $blackdir/C-J-C_placeholder
3f0890
  else
3f0890
    debug "nasty $blackdir  DONT exists, ignoring"
3f0890
  fi
3f0890
done
3f0890
3f0890
debug "cleaning legacy leftowers"
3f0890
if [ "x$debug" == "xtrue" ] ; then
3f0890
  find $sourceSearchPath -empty -type d -delete
3f0890
else
3f0890
  find $sourceSearchPath -empty -type d -delete 2>/dev/null >/dev/null
3f0890
fi
10d287
rmdirDebug $sourceSearchPath
3f0890
3f0890
# and remove placeholders
3f0890
for blackdir in $blackdirs; do
3f0890
  if [ -e $blackdir ] ; then
3f0890
    debug "nasty $blackdir  exists, cleaning placeholder"
10d287
    rmDebug $blackdir/C-J-C_placeholder
3f0890
  else
3f0890
    debug "nasty $blackdir  DONT exists, ignoring again"
3f0890
  fi
3f0890
done
3f0890
3f0890
clean
10d287
10d287
exit 0