###
### The shell script: import_local.sh
###
### The script copies the source file (param 1) 
### to the destination file (param 2, which is galaxy's dataset_NNNNN.dat).
###
### The extra code tries to make it as safe as possible, allowing imports only
### from limited areas.
###
### The script can be changed from copying to linking - would be even faster.
###
### cpx is a C executable that copies a file from a source to a destination
### only if the source file is readable by the user given in the third arg.
### cpx must be setuid root, so be careful who has access to it!  It can also
### be allowed using sudo, if you know how to do it.
#!/bin/sh

INPUT="$1"
OUTPUT="$2"
EMAIL="$3"
USER=`echo $EMAIL | sed -e 's#@.*##'`

if [ -z "$OUTPUT" ]; then
	echo "Usage: $0 [INPUT] [OUTPUT]" >&2
	exit 1
fi

# Filter any unwanted directories

if [[ `echo $INPUT | grep -c ^/data/` -lt 1 ]] && [[ `echo $INPUT | grep -c ^/home/` -lt 1 ]] ; then
  echo "Error: \"$INPUT\" is not allowed" >&2
  exit 1
fi

# Do the deed
cpx $INPUT $OUTPUT "$USER"
if [ $? != 0  ]; then
  echo "Error: $USER failed to copy \"$INPUT\"" >&2
  exit 1
fi

echo "File \"$INPUT\" imported"

exit 0

###
###
###

