#!/bin/bash
########################################################################
# Add X11 forwarding to ssh without disturbing other stuff
########################################################################
TMP=`mktemp /tmp/after.rpms.sh.XXXXXX`
trap "rm $TMP* 2>/dev/null" EXIT

if [ -n "$1" ]; then
  file="$1"
else
  file="/etc/ssh/ssh_config"
fi

perl -wan -e \
'if (/^\s*Host\s+\*/) {
  $host_star = 1;
  $host_star_seen = 1;
  $host_non_star = 0;
}
if (/^\s*Host\s+[^*]/) {
  $host_star = 0;
  $host_non_star = 1;
}
next if (/^\s*ForwardX11/i and not $host_non_star);
print;
print "ForwardX11 yes\n" if $host_star; 
END {
  if (not $host_star_seen) {
    print "Host *\n";
    print "ForwardX11 yes\n";
  }
}' \
"$file" > "$TMP" && install --backup=numbered -m 644 "$TMP" "$file"

exit $?
