#!/bin/sh


ARC_DIR=/home/inf/inf2/PUBLIC/progwsp/problems/submit/arc


if [ $# -ne 1 ]
  then
    echo "usage:" 1>&2
    echo "  submit <directory>" 1>&2
    exit 1
  fi

case "$1" in
  /* | .* )
    SUBMIT_DIR="$1"
    ;;
  * )
    SUBMIT_DIR="./$1"
    ;;
  esac


if [ ! -d "${SUBMIT_DIR}" ]
  then
    echo "FATAL ERROR: ${SUBMIT_DIR} is not a directory" 1>&2
    exit 1
  fi


USER_NAME=`echo -n ${USER_PW_GECOS} | tr -c '[:alnum:]' '[_*]'`
TIME=`date -u +%y%m%d%H%M%S`

ARC_NAME=${USER_NAME}_${USER_PW_NAME}_${TIME}

ARC_FILE=${ARC_DIR}/${ARC_NAME}.tar.gz


if [ -f ${ARC_FILE} ]
  then
    echo "FATAL ERROR: File ${ARC_NAME} already exist. Try again..." 1>&2
    exit 1;
  fi;


umask 227

cd ${SUBMIT_DIR}

find "." -follow \( \( -not -uid ${USER_PW_UID} -fprintf /dev/stderr 'WARNING: file "%p" owned by "%u", not submited\n' \) -or \( -uid ${USER_PW_UID} -not -type d -print \) \) | tar -zcf ${ARC_FILE} -T - 1>&2

echo "INFO: Submited files:" 1>&2
tar -ztf ${ARC_FILE} 1>&2

