#!/bin/sh

# bxauth
# Copyright (C) 2025 Joseph Rosevear, San Diego CA, USA.

# This file is part of a distribution SAM.

# SAM is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your
# option) any later version.

# SAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



# Joseph Rosevear before 200522 I wrote this script.

# Joseph Rosevear 200522 I made edits for readability.

# And I removed the un-needed quotes around ":$1".

# And I replaced $2 with $two where two is either $2 (when it is not
# "") or `uname -n` when $2 is "".  This allows execution of bxauth
# with only one argument.  Or use two arguments when you want to
# specify a hostname other than `uname -n`.  This also gives backwards
# compatibility.

# Joseph Rosevear 220112 I replaced "$1" with "$one", and I added a
# section that defines one and echos the value if it was not provided
# by the user.

# Joseph Rosevear 230522 I removed the prefixed "sh_flag=sh" from then
# invocation of begin.



# Definitions
place=`dirname $0`

if ! [[ "$1" = "" || "$1" = "-" ]]; then

   one="$1"
   
else

   one=`echo $DISPLAY | cut -d: -f2`
   echo one is $one
   pause
fi

if [ "$2" != "" ]; then

   two="$2"
   
else

   two=`uname -n`
fi

# Run xauth.

sam_batch=batch $place/begin "

   xauth list |
   grep $two |
   grep :$one |
   grab 3 | 
   tee /tmp/xauth;
   chmod ugo+r /tmp/xauth;
   pause
"
