#!/bin/bash
#set -x
exec > /dev/null 2>&1

action=$1
id=$2
conf_dir="/unas/run/docker/import_image/$id"
# The task conf
conf_file="$conf_dir/conf"

code=0

. $conf_file
DATA=`realpath /var/lib/docker/unas-docker/`

if [ "$USER" != "admin" ] && ! sudo -u $USER test -r $FILE;then
    exit 2 # Permission denied
fi

prepare_(){
    :
}

start_(){
    # Normal docker tar
    image=`docker load -i "$FILE" | head -n1 | grep -E "Loaded image: .*"`
    code=$?

    # Remove temp file
    realpath $FILE | grep -E "^$DATA/tmp" && rm $FILE
}

clear_(){
    rm -rf "$conf_dir"
}

case "$action" in
    start)
        prepare_
        start_
        ;;
    clear)
        clear_
        ;;
    *)
        ;;
esac

exit $code
