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