#!/bin/bash
# Copyright (c) 2014, Daniele ViganĂ² (daniele@vigano.me)
#
# gobigps is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# gobigps is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with gobigps. If not, see <http://www.gnu.org/licenses/>.

GPSDEV=/dev/ttyUSB0

startstop() {
    DO=$1

    echo "\$GPS_${DO^^}" > $GPSDEV

    if [ $? -eq 0  ];
    then
        echo -e "[ \e[01;32mOK\e[0m ]\tGPS $DO"
    else
        echo -e "[ \e[01;31m!!\e[0m ]\tSomenthing went wrong. Cannot $DO GPS"
        exit 1
    fi
}

# MAIN

if [ ! -w $GPSDEV ];
then
    echo $"Current user $(whoami) doesn't have permission to write to ${GPSDEV}. Please add it to the 'dialout' group."
    exit 1
fi

case "$1" in

'start' | 'stop')
    startstop $1
    ;;
*)
    echo $"Usage: $0 [start|stop]"
    exit 1 ;;
esac