#!/bin/sh
#
# uhome-nest-update
#
# Copyright 2015 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[ -d "$SNAPP_APP_USER_DATA_PATH" ] && DATADIR="$SNAPP_APP_USER_DATA_PATH" || DATADIR="$HOME/.$PKG/"
if [ ! -e "$DATADIR/uhomerc" ]; then
	echo > "$DATADIR/uhomerc" <<EOF
export NEST_USERNAME=
export NEST_PASSWORD=
export NEST_EMAIL=
EOF
fi
. "$DATADIR/uhomerc"

if [ -z "$NEST_USERNAME" ] || [ -z "$NEST_PASSWORD" ]; then
	echo "ERROR: Please update [$DATADIR/uhomerc] with your NEST_USERNAME and NEST_PASSWORD" 1>&2
	exit 1
fi
date
before=$(cat "$DATADIR/state")
uhome
now=$(cat "$DATADIR/state")
if [ "$before" = "0" ] && [ "$now" = "1" ]; then
        date | mail -s "Bon voyage" $NEST_EMAIL
        echo "Bon voyage"
        nest-away
elif [ "$before" = "1" ] && [ "$now" = "0" ]; then
        date | mail -s "Welcome home" $NEST_EMAIL
        echo "Welcome home"
        nest-home
fi
date
