#!/bin/bash

rootdir=${rootdir:-/magic/master-deprov-list}

for uniqname in $(cat $1) ; do
    dirname=${rootdir}/$uniqname
    [[ -e $dirname ]] || mkdir -p $dirname
    if [[ -e $dirname/uploaded ]]; then
        echo "$uniqname already uploaded and verified"
        continue
    fi
    (
        flock -n 9 || exit 1
        startts=$(date -Iseconds)
        if [[ ! -e $dirname/verified ]]; then
            echo "Fetching metadata for $uniqname"
            odm user $uniqname list-items > $dirname/metadata.json
            [[ -s $dirname/metadata.json ]] || exit
            try=0
            retval=1
            until [[ $retval -eq 0 ]] || [[ $try -gt 3 ]]; do
                echo "Downloading files for $uniqname (try $try)"
                odm list $dirname/metadata.json download --filetree $dirname/files -v 2> $dirname/stderr.${startts}.download.$try
                retval=$?
                if [[ $try -eq 0 ]] && [[ $retval -ne 0 ]]; then
                    # The API might have lied about the hashes
                    echo "Re-fetching metadata for $uniqname"
                    odm user $uniqname list-items > $dirname/metadata.json
                fi
                (( try++ ))
            done
            if [[ $retval -eq 0 ]]; then
                echo "Successfully verified $uniqname download"
                touch $dirname/verified
                # Clean up any stale files
                odm list $dirname/metadata.json clean-filetree --filetree $dirname/files -v 2> $dirname/stderr.${startts}.cleanup
                find $dirname/files -type d -empty -delete
            else
                echo "Failed to download $uniqname"
                exit
            fi
        fi

        if [[ -d $dirname/files ]]; then
            echo "Uploading files for $uniqname"
            gdm filetree $dirname/files upload --upload-user $uniqname --upload-path "Migrated from OneDrive 2019" -v 2> $dirname/stderr.${startts}.upload
            tail -1 $dirname/stderr.${startts}.upload
            gdm filetree $dirname/files verify --upload-user $uniqname --upload-path "Migrated from OneDrive 2019" -v 2> $dirname/stderr.${startts}.verify
            if [[ $? -eq 0 ]]; then
                echo "Successfully verified $uniqname upload"
                touch $dirname/uploaded
            fi
            tail -1 $dirname/stderr.${startts}.verify
        else
            echo "Nothing to upload for $uniqname"
            touch $dirname/uploaded
        fi
    ) 9>>$dirname/lockfile
    if [[ $? -ne 0 ]]; then
        echo "$uniqname already locked"
    fi
done
