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

unas_pack=".unas_tgz"

action=$1
id=$2
conf_dir="/unas/run/docker/export/$id"
# The task conf
conf_file="$conf_dir/conf"
# The exported file name for user to download
name_file="$conf_dir/name"
# The actual exported file name
filename_origin_file="$conf_dir/name_ori"
# The path to the exported file
filepath_file="$conf_dir/filepath"

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

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

filename=
filename_origin=
filepath=

prepare_(){
    docker_path=`realpath /var/lib/docker`
    chmod 711 "$docker_path"
    tmp_path="$DATA/tmp/$id"
    share_path="/unas/shares/docker/export/$id"

    [ -d "/unas/shares/docker/export" ] || mkdir "/unas/shares/docker/export" ; chown www-data:www-data "/unas/shares/docker/export"
    [ -d "$tmp_path" ] || mkdir -p "$tmp_path" ; chown www-data:www-data "$tmp_path"
    ln -s "$tmp_path" "$share_path"
}

start_(){
    if [ z"$PROFILE" == z"1" ] && [ z"$TAR" == z"1" ];then
        export_profile
        export_container

        filename="$CONTAINER$unas_pack"
        filename_origin="image$unas_pack"
        filepath="$tmp_path/$filename_origin"
        tar --remove-files -czf $filepath -C $tmp_path config.json image.tar
    elif [ z"$TAR" == z"1" ];then
        export_container gzip
    elif [ z"$PROFILE" == z"1" ];then
        export_profile
    fi

    if [ z"$YML" == z"1" ];then
        export_yml
    fi

    if [ -d "$TARGET_DIR" ];then
        filepath_origin="$filepath"
        filepath="$TARGET_DIR/$filename"
        mv "$filepath_origin" "$filepath"
        ln -s "$filepath" "$filepath_origin"
    fi

    chown $USER:`groups $USER | awk '{print $3}'` $filepath

    echo $filename > $name_file
    echo $filepath > $filepath_file
    echo $filename_origin > $filename_origin_file
}

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

export_profile(){
    filename="$CONTAINER.json"
    filename_origin="config.json"
    filepath="$tmp_path/$filename_origin"
    docker inspect $CONTAINER | jq .[0] > $filepath
}

export_container(){
    docker commit $CONTAINER unas_export_tmp:$id
    if [ z"$1" == z"gzip" ];then
        filename="$CONTAINER.tar.gz"
        filename_origin="image.tar.gz"
        filepath="$tmp_path/$filename_origin"
        docker save unas_export_tmp:$id | gzip - > $filepath
    else
        filename="$CONTAINER.tar"
        filename_origin="image.tar"
        filepath="$tmp_path/$filename_origin"
        docker save unas_export_tmp:$id > $filepath
    fi
    docker rmi unas_export_tmp:$id
}

export_yml(){
    filename="$CONTAINER.yml"
    filename_origin="docker-compose.yml"
    filepath="$tmp_path/$filename_origin"
    compose=$(dirname $(realpath $0))/dockercompose_yml
    $compose $CONTAINER > $filepath
}

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

exit 0
