#!/bin/sh
#
# Usage:
#   remote-proxy [-fexists file]
#                [-dexists directory]
#                [-freadable file]
#                [-fwritable file]
#                [-listdir directory]
#                [-listdirF directory]
#                [-pwd]
#                [-write file]
#                [-append file]
#                [-execute shell_script]
#
# Return Codes: 0 if all is well, 1 if error
#
# Stdout Output:
#   -fexists, -desists		: Outputs "1" if file or dir
#                                 exists, "0" otherwise.
#   -freadable, -fwritable      : Outputs "1" if the file is readable or
#                               : writable, "0" otherwise
#   -listdir			: Outputs a list of files in the dir
#   -listdirF			: Outputs a list of files in the dir
#   -pwd                        : Outputs the user's home directory (always)
#   -write                      : Outputs nothing to stdout
#   -append                     : Outputs nothing to stdout
#   -execute                    : Outputs the stdout and stderr for 
#                                 the command
#


# Check arguments
if [ $# -eq 0 ]
then
  echo "Usage:"
  echo "  remote-proxy [-fexists file]"
  echo "               [-dexists directory]"
  echo "               [-freadable file]"
  echo "               [-fwritable file]"
  echo "               [-listdir directory]"
  echo "               [-listdir directory]"
  echo "               [-pwd]"
  echo "               [-write file]"
  echo "               [-append file]"
  echo "               [-execute shell_script]"
  exit 1
fi

# Parse arguments
case $1 in

  -fexists)
    if [ -f $2 ]
    then 
      echo "1"
    else
      echo "0"
    fi
    ;;

  -dexists)
    if [ -d $2 ]
    then 
      echo "1"
    else
      echo "0"
    fi
    ;;

  -freadable)
    if [ -r $2 ]
    then 
      echo "1"
    else
      echo "0"
    fi
    ;;

  -fwritable)
    if [ -w $2 ]
    then 
      echo "1"
    else
      echo "0"
    fi
    ;;

  -pwd)
    pwd
    ;;

  -listdir)
    ls -1 $2
    ;;

  -listdir)
    ls -1 -F $2
    ;;

  -write)
    cat > $2
    ;;

  -append)
    cat >> $2
    ;;

  -execute)
    sh -c "$2 2>&1"
    ;;

  *)
    echo "OPTION --NO MATCH--"
    exit 1
    ;;

esac

exit 0


