#!/bin/bash

threads=1
environment="./env.txt"
container="maxcentre/yambo:5.1.1_gcc9"
command_prefix=""
init="no"

### MAIN ###
while [[ $1 == -* ]]; do
    case $1 in
        -t | --threads ) threads=$2
			 shift 2
			 ;;
	-c | --container) container=$2
			  shift 2
			  ;;
	-n | -np | --nprocess ) command_prefix="mpirun --use-hwthread-cpus -np $2"
			   shift 2
			   ;;
	--env-file ) environment=$2
		     shift 2
		     ;;
	-i | --init ) init="yes"
		      shift
		      ;;
        * ) echo "Error: \"$1\" unrecognized argument."
	    exit 1
    esac
done

if [ "$init" == "yes" ]; then
    docker pull ${container}
    docker run -ti --user $(id -u):$(id -g) \
	   --mount type=bind,source="$(pwd)",target=/scratch \
	   ${container} cat /opt/spack/env.txt > ${environment}
else
    docker run -ti --user $(id -u):$(id -g) \
	   --mount type=bind,source="$(pwd)",target=/scratch \
	   -e OMP_NUM_THREADS=${threads} --env-file ${environment} \
	   ${container} ${command_prefix} $@
fi