#! /bin/sh
# $Id: xauth-propagate,v 1.3 2007/07/19 21:00:13 friedman Exp $

run()
{
  echo + ${1+"$@"} 1>&2
  ${1+"$@"}
}

run_xauth()
{
   XAUTH=${XAUTH-xauth}
  RXAUTH=${RXAUTH-$XAUTH}
   REMSH=${REMSH-ssh}

  file=$1
  shift

  case $file in
    *:* )
      host=`echo "$file" | sed -e 's/^\([^:]*\):.*/\1/'`
      file=`echo "$file" | sed -e 's/^[^:]*://'`
      run "$REMSH" "$host" "$RXAUTH" -f "$file" ${1+"$@"} ;;
    * )
      run "$XAUTH" -f "$file" ${1+"$@"} ;;
  esac
}

main()
{
   key=${1-$DISPLAY}
  from=${2-$HOME/.Xauthority}
    to=${3-$HOME/etc/misc/.Xauthority}

  case $key in
    localhost:* | localhost.*:* ) key=`echo "$key" | sed -e 's/.*:/unix:/'` ;;
  esac

  data=`run_xauth "$from" nextract - "$key"`
  case $data in
    '' ) exit 1 ;;
  esac
  echo "$data" | run_xauth "$to" nmerge -
}

main ${1+"$@"}

# eof
