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