config() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
    rm $NEW
  fi
}

preserve_perms() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ -e $OLD ]; then
    cp -a $OLD ${NEW}.incoming
    cat $NEW > ${NEW}.incoming
    mv ${NEW}.incoming $NEW
  fi
  config $NEW
}

preserve_perms etc/rc.d/rc.kafka.new
preserve_perms etc/kafka/server.properties.new
preserve_perms etc/kafka/consumer.properties.new
preserve_perms etc/kafka/producer.properties.new
preserve_perms etc/kafka/log4j.properties.new
preserve_perms etc/kafka/test-log4j.properties.new
preserve_perms etc/kafka/tools-log4j.properties.new
preserve_perms etc/kafka/kafka-env.sh.new
preserve_perms etc/kafka/connect-console-sink.properties.new
preserve_perms etc/kafka/connect-console-source.properties.new
preserve_perms etc/kafka/connect-distributed.properties.new
preserve_perms etc/kafka/connect-file-sink.properties.new
preserve_perms etc/kafka/connect-file-source.properties.new
preserve_perms etc/kafka/connect-log4j.properties.new
preserve_perms etc/kafka/connect-standalone.properties.new
( cd usr/bin ; rm -rf kafka-replica-verification.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-replica-verification.sh kafka-replica-verification.sh )
( cd usr/bin ; rm -rf kafka-broker-api-versions.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-broker-api-versions.sh kafka-broker-api-versions.sh )
( cd usr/bin ; rm -rf kafka-console-producer.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-console-producer.sh kafka-console-producer.sh )
( cd usr/bin ; rm -rf kafka-run-class.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-run-class.sh kafka-run-class.sh )
( cd usr/bin ; rm -rf kafka-server-stop.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-server-stop.sh kafka-server-stop.sh )
( cd usr/bin ; rm -rf kafka-consumer-perf-test.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-consumer-perf-test.sh kafka-consumer-perf-test.sh )
( cd usr/bin ; rm -rf kafka-topics.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-topics.sh kafka-topics.sh )
( cd usr/bin ; rm -rf kafka-consumer-groups.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-consumer-groups.sh kafka-consumer-groups.sh )
( cd usr/bin ; rm -rf kafka-preferred-replica-election.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-preferred-replica-election.sh kafka-preferred-replica-election.sh )
( cd usr/bin ; rm -rf kafka-reassign-partitions.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-reassign-partitions.sh kafka-reassign-partitions.sh )
( cd usr/bin ; rm -rf kafka-streams-application-reset.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-streams-application-reset.sh kafka-streams-application-reset.sh )
( cd usr/bin ; rm -rf kafka-replay-log-producer.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-replay-log-producer.sh kafka-replay-log-producer.sh )
( cd usr/bin ; rm -rf kafka-verifiable-consumer.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-verifiable-consumer.sh kafka-verifiable-consumer.sh )
( cd usr/bin ; rm -rf kafka-acls.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-acls.sh kafka-acls.sh )
( cd usr/bin ; rm -rf kafka-producer-perf-test.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-producer-perf-test.sh kafka-producer-perf-test.sh )
( cd usr/bin ; rm -rf kafka-console-consumer.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-console-consumer.sh kafka-console-consumer.sh )
( cd usr/bin ; rm -rf kafka-server-start.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-server-start.sh kafka-server-start.sh )
( cd usr/bin ; rm -rf kafka-configs.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-configs.sh kafka-configs.sh )
( cd usr/bin ; rm -rf kafka-verifiable-producer.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-verifiable-producer.sh kafka-verifiable-producer.sh )
( cd usr/bin ; rm -rf kafka-mirror-maker.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-mirror-maker.sh kafka-mirror-maker.sh )
( cd usr/bin ; rm -rf kafka-simple-consumer-shell.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-simple-consumer-shell.sh kafka-simple-consumer-shell.sh )
( cd usr/bin ; rm -rf kafka-consumer-offset-checker.sh )
( cd usr/bin ; ln -sf ../lib/kafka/bin/kafka-consumer-offset-checker.sh kafka-consumer-offset-checker.sh )
( cd usr/lib/kafka ; rm -rf config )
( cd usr/lib/kafka ; ln -sf /etc/kafka config )
