#!/bin/bash

# ----------------------------------------------------------------------
# File: eos-fusex-functonal-test
# Author: Manuel Reis
# ----------------------------------------------------------------------

usage() { echo '''Usage: eos-https-func-tests --samba <localdir> <fsname>
                           [-h|--help] - usage & exit

                           <localdir>   - Directory to be created and on which the mount will take place.
                                          By default /eos/dockertest/test
                           <fsname>     - Base fusex configuration name. By default eosdockertest for
                                          /etc/eos/fuse.eosdockertest.conf
'''; }

# Parser from: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
usage_error () { echo >&2 "$(basename "$0"):  $1"; exit 2; }
assert_argument () { test "$1" != "$EOL" || usage_error "$2 requires an argument"; }

samba=0
# One loop, nothing more.
if [ "$#" != 0 ]; then
  EOL=$(printf '\1\3\3\7')
  set -- "$@" "$EOL"
  while [ "$1" != "$EOL" ]; do
    opt="$1"; shift
    case "$opt" in

      # Your options go here.
      --samba)    samba=1;;
      -h|--help) usage; exit 0;;

      # Arguments processing. You may remove any unneeded line after the 1st.
      -|''|[!-]*) set -- "$@" "$opt";;                                          # positional argument, rotate to the end
      --*=*)      set -- "${opt%%=*}" "${opt#*=}" "$@";;                        # convert '--name=arg' to '--name' 'arg'
      -[!-]?*)    set -- "$(echo "${opt#-}" | sed 's    /g')" "$@";;     # convert '-abc' to '-a' '-b' '-c'
      --)         while [ "$1" != "$EOL" ]; do set -- "$@" "$1"; shift; done;;  # process remaining arguments as positional
      -*)         usage_error "unknown option: '$opt'";;                        # catch misspelled options
      *)          usage_error "this should NEVER happen ($opt)";;               # sanity test for previous patterns

    esac
  done
  shift  # $EOL
fi

FUSEDIR=${1:-"/eos-samba"}
FSNAME=${2:-"eosdockertest"}

umountfuse () {
    umount -f ${FUSEDIR} &> /dev/null || true
}
trap umountfuse EXIT

# umount whatever is mounted on this local
umountfuse

test-samba-config(){
    umountfuse
    # Samba relies on Unix (for users) SSS (for root, which maps to eossambabot/eosdev)

    # merge eosxd json config with auth field used in samba
    jq '.*{"auth":{"krb5":0, "unix":1, "sss":1, "ssskeytab":"/etc/sss.samba.keytab", "oauth2":0}}' /etc/eos/fuse.${FSNAME}.conf > /etc/eos/fuse.${FSNAME}samba.conf
    grep eosdev /etc/eos.keytab > /etc/sss.samba.keytab
    chmod 400 /etc/sss.samba.keytab

    # prepare mountpoints
    mkdir -m 777 -vp "${FUSEDIR}";
    cat /etc/eos/fuse.${FSNAME}samba.conf
    eosxd -ofsname="${FSNAME}"samba "${FUSEDIR}/";

    # Root should create a folder with the
    mkdir -m 777 -vp "${FUSEDIR}/dockertest/sambatests/"
    touch "${FUSEDIR}/dockertest/sambatests/filefromroot"
    test "$(stat -c "%U:%G" ${FUSEDIR}/dockertest/sambatests/filefromroot)" == "eosdev:eosdev"
    rc=$?
    runuser eos-user -c "touch ${FUSEDIR}/dockertest/sambatests/filefromeos-user"
    test "$(stat -c "%U:%G"  ${FUSEDIR}/dockertest/sambatests/filefromeos-user)" == "eos-user:eos-user"
    rc=$(( $rc<<1  | $? ))
    runuser eos-user -c "ls -la ${FUSEDIR}/dockertest/sambatests/"
    return $rc
}

if [ $samba == 1 ]; then
  test-samba-config
  exit $?
fi
