#!/bin/sh

# Meiga - Lightweight and easy to use web file server for your desktop
#
# Copyright (C) 2009 Igalia, S.L.
#
# Authors:
#
# Igalia, S.L. <info@igalia.com>
# Enrique Ocaña González <eocanha@igalia.com>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Gets IP addr of the interface having the default gateway


# We can't rely on the PATH being properly set if this tool is executed
# from a DBUS daemon
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH"

GWIFACE=`ip route | grep 'default via' | { read _ _ _ _ I _; echo $I; }`

if [ -z "$GWIFACE" ]
then
  GWIFACE=`ip route | grep 'default dev' | { read _ _ I _ ; echo $I; }`  
fi

if [ -n "$GWIFACE" ]
then
  IP=`ip addr show dev $GWIFACE | grep 'inet ' | { read _ J _; echo $J; } | sed 's/\/[0-9]*//'`
else
  IP="127.0.0.1"
fi
echo "$IP"
