#!/usr/bin/env bash
# Description:
# 	Script to ask Google for the location of a Cellular Base Station
# Authors: 	
#		Jose Pico (jose _AT_ taddong _DOT_ com)
#		David Perez (david _AT_ taddong _DOT_ com)
#		Taddong (www.taddong.com)
# Date:		2011-08-04
# Version:	1.0
#
# Requires: 
# - wget
# - conection to internet
#
# Credits: Based on the analysis of traffic generated by 
#          signal application (from PlanetBeing) available on Cydia
# 
# /**************************************************************************
# *   Copyright 2011 by Taddong (Jose Pico & David Perez)
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 3 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# *   This program 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 General Public License     *
# *   along with this program. If not, see <http://www.gnu.org/licenses/>.  *
# *                                                                         *
# **************************************************************************/

###### CONFIG SECTION
WEB_BROWSER="/usr/bin/firefox"

###### INIT SECTION
LAUNCH_BROWSER="NO"
typeset -i MCC=0
typeset -i MNC=0
typeset -i LAC=0
typeset -i CI=0

###### FUNCTIONS SECTION
function usage {
	printf "tadbsl.sh --MCC=<MCC> --MNC=<MNC> --LAC=<LAC> --CI=<CI> [-s | --show_in_browser]\n"
	printf "tadbsl.sh [-h | --help]\n"
	printf "\tDescription: this script asks Google for a particular\n"
	printf "\tCellular Base Station Location and shows the Google's answer.\n"
	printf "\t(Based on the traffic analysis of Signal Cydia application\n"
	printf "\t from PlanetBeing)\n"
	printf "\tArguments:\n"
	printf "\t\tMCC: Mobile Country Code of the carrier owning the Base Station\n"
	printf "\t\tMNC: Mobile Network Code of the carrier owning the Base Staion\n"
	printf "\t\tLAC: Location Area Code of the Base Station\n"
	printf "\t\tCI:  Cell Identificator of the Base Station\n"

	printf "\tOptions:\n"
	printf "\t\t-h | --help\n"
	printf "\t\t\t Shows this help.\n"
	printf "\t\t-s | --show_in_browser\n"
	printf "\t\t\t If you specify this options the script will launch\n"
	printf "\t\t\t your browser with the obtained coordinates.\n"
	printf "\t\t\t You can configure your browser location in a\n"
	printf "\t\t\t configuration variable inside the script.\n"
}

function check_requisites {
	if ! which wget 1>/dev/null 2>&1 
	then
		printf "\nERROR! Impossible to find wget.\n"
	fi
}

function parse_arguments {
	while [[ $# -gt 0 ]] 
	do
		case $1 in
		-h | --help)
			usage
			exit
			;;
		-s | --show_in_browser)
			LAUNCH_BROWSER="YES"
			;;
		--MCC*)
			if [[ ${1:5:1} != "=" ]]
			then
				printf "ERROR! '=' expected after --MCC.\n\n"
				usage
				exit
			fi
			MCC=${1:6}
			if [[ $MCC -lt 1 || $MCC -gt 16777215 ]]
			then
				printf "ERROR! MCC must be a number between 1 and 16777215.\n\n"
				usage
				exit
			fi
			;;
		--MNC*)
			if [[ ${1:5:1} != "=" ]]
			then
				printf "ERROR! '=' expected after --MNC.\n\n"
				usage
				exit
			fi
			MNC=${1:6}
			if [[ $MNC -lt 1 || $MNC -gt 16777215 ]]
			then
				printf "ERROR! MNC must be a number between 1 and 65535.\n\n"
				usage
				exit
			fi
			;;
		--LAC*)
			if [[ ${1:5:1} != "=" ]]
			then
				printf "ERROR! '=' expected after --LAC.\n\n"
				usage
				exit
			fi
			LAC=${1:6}
			if [[ $LAC -lt 1 || $LAC -gt 16777215 ]]
			then
				printf "ERROR! LAC must be a number between 1 and 65535.\n\n"
				usage
				exit
			fi
			;;
		--CI*)
			if [[ ${1:4:1} != "=" ]]
			then
				printf "ERROR! '=' expected after --CI.\n\n"
				usage
				exit
			fi
			CI=${1:5}
			if [[ $CI -lt 1 || $CI -gt 16777215 ]]
			then
				printf "ERROR! CI must be a number between 1 and 65535.\n\n"
				usage
				exit
			fi
			;;
		*)
			printf "\nERROR! Unknown argument: $1.\n\n"
			usage
			exit
			;;
		esac
		shift 1
	done
	if [[ $MNC -eq 0 ]]
	then
		printf "\nERROR! MNC is a mandatory parameter.\n\n"
		usage
		exit
	fi
	if [[ $MCC -eq 0 ]]
	then
		printf "\nERROR! MCC is a mandatory parameter.\n\n"
		usage
		exit
	fi
	if [[ $LAC -eq 0 ]]
	then
		printf "\nERROR! LAC is a mandatory parameter.\n\n"
		usage
		exit
	fi
	if [[ $CI -eq 0 ]]
	then
		printf "\nERROR! CI is a mandatory parameter.\n\n"
		usage
		exit
	fi

}

###### MAIN SECTION
check_requisites
parse_arguments $@

POST_DATA="{'cell_towers': [{'location_area_code': $LAC, 'mobile_network_code': $MNC, 'cell_id': $CI, 'mobile_country_code': $MCC}], 'version':'1.1.0'}"
POST_DATA_LENGTH=${#POST_DATA}

RESPONSE=`wget --timeout=10 --read-timeout=10 -q -O - --header="Accept: */*" --header="Accept-Language: en-us" --header="Accept-Encoding: gzip, deflate" --header="Content-Type: application/x-www-form-urlencoded" --header="Content-Length: $POST_DATA_LENGTH" --post-data="$POST_DATA" http://www.google.com/loc/json`

echo $RESPONSE | grep longitude | grep latitude 1>/dev/null 2>&1
if [[ $? -ne 0 ]]
then
	printf "WARNING! Base station not found.\n"
	exit
fi

latitude=`echo ${RESPONSE/\"latitude\"\:/#} | sed "s/,/#/g" | cut -d "#" -f 2`
longitude=`echo ${RESPONSE/\"longitude\"\:/#} | sed "s/,/#/g" | cut -d "#" -f 3`


printf "LOCATION: $latitude , $longitude\n"

if [[ $LAUNCH_BROWSER == "YES" ]]
then
	$WEB_BROWSER http://maps.google.com/maps?q=${latitude},${longitude}&hl=en &
fi

