#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

GPGKEY_COMMIT='ebak'
REMOTE="s3://${GPGKEY_COMMIT}-$(date +%F)"
REMOTE="file://integration-test-temp-files/${GPGKEY_COMMIT}-repo" # Comment this line for AWS testing
QUIET="true"
export BINARY_REPOSITORY_USE_DEV_THRESHOLDS=1 # Warning, only for testing

GPGKEY_FILEPART1='ebak-fp1'

npm test

set +e
COMMIT_SECRET_KEY_COUNT=$(gpg --list-keys | grep "$GPGKEY_COMMIT\$" | wc -l)
set -e

if [ $COMMIT_SECRET_KEY_COUNT -ne "1" ]; then
    gpg --delete-secret-keys "$GPGKEY_COMMIT" || true
    gpg --delete-keys "$GPGKEY_COMMIT" || true
    gpg --batch ---passphrase '' --quick-gen-key "$GPGKEY_COMMIT"
fi

set +e
FILEPART1_SECRET_KEY_COUNT=$(gpg --list-keys | grep "$GPGKEY_FILEPART1\$" | wc -l)
set -e

if [ $FILEPART1_SECRET_KEY_COUNT -ne "1" ]; then
    gpg --delete-secret-keys "$GPGKEY_FILEPART1" || true
    gpg --delete-keys "$GPGKEY_FILEPART1" || true
    gpg --batch --passphrase '' --quick-gen-key "$GPGKEY_FILEPART1"
fi

rm -rf   integration-test-temp-files
mkdir -p integration-test-temp-files/ebak-data/matt_first
mkdir -p integration-test-temp-files/ebak-data/jim_leach


if [ $(echo "$REMOTE" | grep '^s3' | wc -c) -gt 0 ]; then
    set +e
    aws s3 ls "$REMOTE" && aws s3 rb --force "$REMOTE" # Delete bucket if does exist
    set -e
    aws s3 mb "$REMOTE"
else
    rm -rf $( echo "$REMOTE" | sed 's%^[a-z0-9]\+\://%%' )
    mkdir -p integration-test-temp-files/ebak-repo
fi

./bin/binary-repository init --quiet=$QUIET --filepart-gpg-key "$GPGKEY_FILEPART1" --local-config=integration-test-temp-files/ebak-config/matt_first --client-id mattfirst --gpg-key "$GPGKEY_COMMIT"  --remote "$REMOTE" integration-test-temp-files/ebak-data/matt_first

./bin/binary-repository upload --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first

if [ $(find integration-test-temp-files/ebak-data/matt_first -type f | wc -l) -ne 0 ]; then
    echo "There should be no files in matt_first data directory"
    exit 1
fi

if [ $(find integration-test-temp-files/ebak-config/matt_first -type f | grep "\/config$" | wc -l) -ne 1 ] ; then
    echo "$MATTS_CONFIG should have been one line"
    exit 1
fi

cp -R test/data/my-projects integration-test-temp-files/ebak-data/matt_first

find  integration-test-temp-files/ebak-data/matt_first/ -type f | sed sI.*matt_first/II | grep -v '^$' | sort > integration-test-temp-files/the_files

./bin/binary-repository list-upload --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first | sort > integration-test-temp-files/will_upload

if [ $(diff integration-test-temp-files/the_files integration-test-temp-files/will_upload | wc -l) -ne 0 ]; then
    echo "Upload list does not equal file list?..."
    exit 1
fi

./bin/binary-repository upload --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first


./bin/binary-repository push --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first

./bin/binary-repository list-existing --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first > integration-test-temp-files/existing

if [ $(diff integration-test-temp-files/the_files integration-test-temp-files/existing | wc -l) -ne 0 ]; then
    echo "Items existing should be same as files"
    exit 1
fi

./bin/binary-repository list-existing --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first --file-filter 'my-projects/t-' integration-test-temp-files/ebak-data/matt_first > integration-test-temp-files/existing-filtered

if [ $(cat integration-test-temp-files/existing-filtered | wc -l) -ne $(cat integration-test-temp-files/existing-filtered | grep '^my-projects/t-' | wc -l) ]; then
    echo "List should have been filtered"
    exit 1
fi

if [ $(find integration-test-temp-files/ebak-config/matt_first/pending-commit -type f | wc -l) -ne 0 ]; then
    echo "Was expecting zero matt_first pending-commit"
    exit 1
fi


if [ $(echo "$REMOTE" | grep '^file' | wc -c) -gt 0 ]; then
    if [ $(gpg --batch --list-packets $(ls integration-test-temp-files/ebak-repo/c-* | head -n 1) 2>&1 | grep '^ \+"' | sed 's/ \+"//' | sed 's/"$//') != "$GPGKEY_COMMIT" ]; then
        echo "Commit should have been encrypted with $GPGKEY_COMMIT"
        exit 1
    fi
    if [ $(gpg --batch --list-packets $(ls integration-test-temp-files/ebak-repo/f-* | head -n 1) 2>&1 | grep '^ \+"' | sed 's/ \+"//' | sed 's/"$//') != $GPGKEY_FILEPART1 ]; then
        echo "Filepart should have been encrypted with $GPGKEY_FILEPART1"
        exit 1
    fi
fi

./bin/binary-repository init --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach --client-id jimleach --gpg-key "$GPGKEY_COMMIT" --remote "$REMOTE" integration-test-temp-files/ebak-data/jim_leach

if [ $(find integration-test-temp-files/ebak-config/jim_leach -type f | wc -l) -ne 1 ]; then
    echo "There should be one files in jim_leach config directory"
    exit 1
fi

./bin/binary-repository fetch --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach

if [ $(diff integration-test-temp-files/ebak-config/jim_leach/remote-pending-commit integration-test-temp-files/ebak-config/matt_first/commit | wc -l) -ne 0 ]; then
    echo "The remote-pending-commit did not match the commit"
    exit 1
fi

./bin/binary-repository list-download --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach | sort > integration-test-temp-files/will_download

if [ $(diff integration-test-temp-files/will_upload integration-test-temp-files/will_download | wc -l) -ne 0 ]; then
    echo "The download list was not the same as the upload list"
    exit 1
fi

./bin/binary-repository download --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach

if [ $(diff integration-test-temp-files/ebak-config/jim_leach/remote-commit integration-test-temp-files/ebak-config/matt_first/commit | wc -l) -ne 0 ]; then
    echo "The remote-pending-commit should now be in remote-commit"
    exit 1
fi

if [ $(diff -bur integration-test-temp-files/ebak-data/jim_leach integration-test-temp-files/ebak-data/matt_first | wc -l) -ne 0 ]; then
    echo "The contents of the data should be identical"
    exit 1
fi

ls -l integration-test-temp-files/ebak-data/matt_first/my-projects/ | sort | sed 's/^[^ ]* //' > integration-test-temp-files/ebak-config/matt_first/ls
ls -l integration-test-temp-files/ebak-data/jim_leach/my-projects/ | sort | sed 's/^[^ ]* //' > integration-test-temp-files/ebak-config/jim_leach/ls

if [ $(diff integration-test-temp-files/ebak-config/jim_leach/ls integration-test-temp-files/ebak-config/matt_first/ls | wc -l) -ne 0 ]; then
    echo "Timestamps or something of synched files (but not permissions) is not the same"
    exit 1
fi


rm -rf integration-test-temp-files/ebak-full/edward_elephant
mkdir -p integration-test-temp-files/ebak-full/edward_elephant
./bin/binary-repository init --quiet=$QUIET --client-id edward_elephant --gpg-key "$GPGKEY_COMMIT" --remote "$REMOTE" integration-test-temp-files/ebak-full/edward_elephant
./bin/binary-repository fetch --quiet=$QUIET integration-test-temp-files/ebak-full/edward_elephant
./bin/binary-repository download --quiet=$QUIET integration-test-temp-files/ebak-full/edward_elephant
if [ $(./bin/binary-repository list-upload --quiet=$QUIET integration-test-temp-files/ebak-full/edward_elephant | wc -l) -ne 0 ]; then
    echo "Edward Elephant would upload something??? why"
    exit 1
fi

cp -R test/data/other-cool-projects integration-test-temp-files/ebak-data/matt_first

if [ $(./bin/binary-repository list-upload --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first | wc -l) -ne "3" ]; then
    echo "Second upload should only be three files"
    exit 1
fi

./bin/binary-repository upload --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first
./bin/binary-repository push --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first
./bin/binary-repository fetch --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach

ls integration-test-temp-files/ebak-config/jim_leach/remote-pending-commit/ > integration-test-temp-files/new-commit
ls integration-test-temp-files/ebak-config/jim_leach/remote-commit/ > integration-test-temp-files/old-commit

if [ $(cat integration-test-temp-files/new-commit | wc -l) -lt 1 ]; then
    echo "Should probably have fetched remote commits"
    exit 1
fi


if [ $(cat integration-test-temp-files/new-commit integration-test-temp-files/old-commit | sort | uniq | wc -l) -ne $(cat integration-test-temp-files/new-commit integration-test-temp-files/old-commit | wc -l) ]; then
    echo "Second upload probably should not have had anything in from first"
    exit 1
fi


if [ $(echo "$REMOTE" | grep '^file' | wc -c) -gt 0 ]; then
    if [ $(cat $(ls integration-test-temp-files/ebak-repo/f-806666aa36f2d65362a85fb19578babfc271ddcff6ef36265223dc0562d53f14-1-*-ebak--fp1.ebak) | gpg -d | head -n1) != '# t-fp-merge' ]; then
        echo "t-fp-merge README.md was expected to be in f-806666aa36f2d65362a85fb19578babfc271ddcff6ef36265223dc0562d53f14-1-64MB-ebak--fp1.ebak but it was not"
        exit 1
    fi
fi

rm integration-test-temp-files/ebak-data/matt_first/my-projects/t-fp-dissoc.md
./bin/binary-repository list-mark-deleted --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first  integration-test-temp-files/ebak-data/matt_first > integration-test-temp-files/list-mark-deleted

if [ $(cat integration-test-temp-files/list-mark-deleted | wc -l) -ne 1 ]; then
    echo "Should have one list-mark-deleted file"
    exit 1
fi

if [ $(cat integration-test-temp-files/list-mark-deleted | head -n1) != 'my-projects/t-fp-dissoc.md' ]; then
    echo "list-mark-deleted file listed incorrect data"
    exit 1
fi

./bin/binary-repository mark-deleted --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first  integration-test-temp-files/ebak-data/matt_first

# For a while it would adding delete commits
./bin/binary-repository mark-deleted --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first  integration-test-temp-files/ebak-data/matt_first

if [ $(find integration-test-temp-files/ebak-config/matt_first/pending-commit -type f | wc -l) -ne 1 ]; then
    echo "Was expecting one matt_first pending-commit for mark-deleted"
    exit 1
fi


if [ $(find  integration-test-temp-files/ebak-config/*/tmp/ -type f | wc -l) -ne 0 ]; then
    echo "Mess left in tmp files"
    exit 1
fi

./bin/binary-repository push --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/matt_first integration-test-temp-files/ebak-data/matt_first

./bin/binary-repository fetch --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach

./bin/binary-repository download --quiet=$QUIET --local-config=integration-test-temp-files/ebak-config/jim_leach integration-test-temp-files/ebak-data/jim_leach

if [ $(find integration-test-temp-files/ebak-data/jim_leach/my-projects -type f | wc -l | grep dissoc | wc -l ) -ne 0 ]; then
    echo "t-fp-dissoc.md should have not been here"
    exit 1
fi

mkdir -p integration-test-temp-files/ebak-full/sam_snail

./bin/binary-repository init --quiet=$QUIET --client-id sam_snail --gpg-key "$GPGKEY_COMMIT" --remote "$REMOTE" integration-test-temp-files/ebak-full/sam_snail

./bin/binary-repository  --quiet=$QUIET fetch integration-test-temp-files/ebak-full/sam_snail

./bin/binary-repository get-file  --quiet=$QUIET --file-filter my-projects/g integration-test-temp-files/ebak-full/sam_snail

if [ $(ls integration-test-temp-files/ebak-full/sam_snail/my-projects | wc -l) -ne 1 ]; then
    echo "More files than expected!"
    exit 1
fi

SHA=$(cat integration-test-temp-files/ebak-full/sam_snail/my-projects/getTLIdEncoderDecoder.md | sha256sum | sed 's/ .*//')
if [ "$SHA" != 'def8c702e06f7f6ac6576e0d4bbd830303aaa7d6857ee6c81c6d6a1b0a6c3bdf' ]; then
    echo "File contents wrong"
    exit 1
fi

echo "Tests Passed."

