#!/bin/bash

TOKEN=$(cat ~/.config/liberux/contacts/token)
PORT=$(cat $XDG_RUNTIME_DIR/liberux/contacts.port)

cat > /tmp/aq.xml <<'EOF'
<?xml version="1.0"?>
<C:addressbook-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
  <D:prop><D:getetag/><C:address-data/></D:prop>
</C:addressbook-query>
EOF

curl -i -u liberux:$TOKEN -X REPORT -H "Depth: 1" -H "Content-Type: application/xml" --data @/tmp/aq.xml "http://127.0.0.1:$PORT/addressbook/main/"

cat > /tmp/sync_init.xml <<'EOF'
<?xml version="1.0"?>
<D:sync-collection xmlns:D="DAV:">
  <D:sync-token/>
  <D:prop><D:getetag/></D:prop>
</D:sync-collection>
EOF

curl -i -u liberux:$TOKEN -X REPORT -H "Content-Type: application/xml" --data @/tmp/sync_init.xml "http://127.0.0.1:$PORT/addressbook/main/"

cat > /tmp/sync_delta.xml <<'EOF'
<?xml version="1.0"?>
<D:sync-collection xmlns:D="DAV:">
  <D:sync-token>data:,4</D:sync-token>
  <D:prop><D:getetag/></D:prop>
</D:sync-collection>
EOF

curl -i -u liberux:$TOKEN -X REPORT -H "Content-Type: application/xml" --data @/tmp/sync_delta.xml "http://127.0.0.1:$PORT/addressbook/main/"

cat > /tmp/multiget.xml <<'EOF'
<?xml version="1.0"?>
<C:addressbook-multiget xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
  <D:prop><D:getetag/><C:address-data/></D:prop>
  <D:href>/addressbook/main/6a9fcfb7-0464-4283-858f-7900762eae68.vcf</D:href>
  <D:href>/addressbook/main/inexistente.vcf</D:href>
<C:addressbook-multiget xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
  <D:prop><D:getetag/><C:address-data/></D:prop>
  <D:href>/addressbook/main/6a9fcfb7-0464-4283-858f-7900762eae68.vcf</D:href>
  <D:href>/addressbook/main/inexistente.vcf</D:href>
</C:addressbook-multiget>
EOF

curl -i -u liberux:$TOKEN -X REPORT -H "Content-Type: application/xml" --data @/tmp/multiget.xml "http://127.0.0.1:$PORT/addressbook/main/"

echo '<?xml version="1.0"?><D:expand-property xmlns:D="DAV:"/>' > /tmp/unknown.xml

curl -i -u liberux:$TOKEN -X REPORT -H "Content-Type: application/xml" --data @/tmp/unknown.xml "http://127.0.0.1:$PORT/addressbook/main/"
