#!/bin/bash
#sudo rmmod dvb_usb_rtl28xxu rtl2832

set -e

SENSOR=""

# Iterations
num_iterations=4
start_num=0

if [ "$#" -ne 3 ]; then
        echo "Usage: $0 <Target Freq (Hz)> <Start time (Unix ts)> <Password>"
    exit 1
fi

# Create array for experiments
start_date="$2.0"
declare -a dates
for i in $(seq 0 $num_iterations)
do
    next_exp_time=$(((i+1)*15))
    dates[i]=`echo "$start_date+$next_exp_time" | bc`
done

sudo mount -o remount,rw / ; sudo mount -o remount, rw /boot
mkdir -p /tmp/localization/
for i in $(seq 0 $num_iterations)
do
    echo "Loop: $i"

    # Launch regular RTL-SDR to get LTE samples for LO corretion
    es_sensor 806000000 806000000 -z IQ -s 1920000 -t 2 -u BYTE:/tmp/localization/E$((i+start_num))-ltess.dat

    echo "es_sensor $1 $2 -x ${dates[i]} -z IQ -s 2000000 -t 1.5 -u E$((i+start_num))-196_208-localization.dat"
    es_sensor 196000000 $1 -g 30 -x ${dates[i]} -z IQ -s 2000000 -t 1.5 -u BYTE:/tmp/localization/E$((i+start_num))-196_208-localization.dat
done

# tar all files
tar -czf /tmp/localization/$SENSOR.tgz /tmp/localization/E* --remove-files

# Upload to Box
curl -X PUT -u yago_lizarribar:$3 "https://box.networks.imdea.org/remote.php/webdav/$SENSOR.tgz" --data-binary @"/tmp/localization/$SENSOR.tgz"

rm /tmp/localization/$SENSOR.tgz

sudo mount -o remount,ro / ; sudo mount -o remount,ro /boot

