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