#!/bin/sh
#
# Create zip archive of a TopoDroid v. 5 survey.
#
# Usage:
#   [1] copy this script in the TopoDroid folder
#   [2] execute this script with argument the survey name
# 
# If run without argument the script list the available surveys.
#
# Requirements: sqlite3, zip, date
#
SQLITE=`which sqlite3`
ZIP=`which zip`
DATE=`which date`
if [ -z $SQLITE ] ; then
    echo "Missing sqlite3"
    exit 1
fi
if [ -z $ZIP ] ; then
    echo "Missing zip"
    exit 1
fi
if [ -z $DATE ] ; then
    echo "Missing date"
    exit 1
fi
echo "Found $SQLITE"
echo "      $ZIP"
echo "      $DATE\n"

td_version="5.1.40"
db_version="42"

distox14="distox14.sqlite"
device10="device10.sqlite"
if [ ! -f $distox14 ] ; then
     echo "TopoDroid database not found"
     exit 2
else 
     db_version=`echo "PRAGMA user_version;" | $SQLITE $distox14`
fi
if [ ! -f $device10 ] ; then
     td_version=`echo "select value from configs where key=\"version\";" | $SQLITE $device10`
fi
td_version_code=`echo {$td_version//\./0}`

if [ $# -eq 0 ] ; then
    echo "select name from surveys;" > __input
    echo "Usage: zip.sh <survey_name>"
    echo "Available surveys:"
    $SQLITE $distox14 < __input
    rm -rf __input
else
    rm -rf __tmp
    mkdir __tmp
    name=$1
    echo "$td_version $td_version_code" > __tmp/manifest
    echo "$db_version" >> __tmp/manifest
    echo "$name" >> __tmp/manifest
    $DATE +%Y-%m-%d >> __tmp/manifest

cat > $name.sqlite << x123end
.output __tmp/survey.sql
.mode insert surveys
select surveys.* from surveys where surveys.name="$name";
.mode insert plots
select plots.* from plots,surveys where surveys.name="$name" and surveys.id == plots.surveyId;
.mode insert shots
select shots.* from shots,surveys where surveys.name="$name" and surveys.id == shots.surveyId;
.mode insert audios
select audios.* from audios,surveys where surveys.name="$name" and surveys.id == audios.surveyId;
.mode insert photos
select photos.* from photos,surveys where surveys.name="$name" and surveys.id == photos.surveyId;
.mode insert fixeds
select fixeds.* from fixeds,surveys where surveys.name="$name" and surveys.id == fixeds.surveyId;
.mode insert stations
select stations.* from stations,surveys where surveys.name="$name" and surveys.id == stations.surveyId;
.mode insert sensors
select sensors.* from sensors,surveys where surveys.name="$name" and surveys.id == sensors.surveyId;
x123end

    $SQLITE $distox14 < $name.sqlite
    rm -f $name.sqlite

tdr="tdr"
audio="audio/$name"
photo="photo/$name"
note="note/$name.txt"

    if [ -e $tdr ] && [ -z "$(ls -A $tdr/$name-*.tdr)" ] ; then
        cp -l $tdr/$name-*.tdr __tmp
    fi
    if [ -e $audio ] && [ -z "$(ls -A $audio/*.wav)" ] ; then
        cp -l $audio/*.wav __tmp 
    fi
    if [ -e $photo ] && [ -z "$(ls -A $photo/*.wav)" ] ; then
        cp -l $photo/*.jpg __tmp
    fi
    if [ -e $note ] ; then
        cp -l $note __tmp
    fi

    cd __tmp
    $ZIP ../$name.zip ./*
    cd -
    rm -rf __tmp
fi

