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