#!/bin/bash
if test $(id -u) != 0; then
 echo Error: You must be root to run this script!
 exit 1
fi
LC_ALL=C
export LC_ALL
ALL=$(ifconfig|awk '/Link/{print $1}')
IF=""
for if in $ALL; do
 test "$if" == "ppp0" -a "$IF" == "" && IF=$if
done 
for if in $ALL; do
 test "$if" == "ippp0" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
 test "${if/#eth*/eth}" == "eth" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
 test "${if/#wlan*/wlan}" == "wlan" -a "$IF" == "" && IF=$if
done
test "$IF" == "" && IF=lo
echo Using $IF
IP=$(ifconfig $IF|awk '/inet/{print $2}'|cut -d: -f2)
SERVERNAME=$(awk '/^ServerName/{print $2}' /etc/apache/httpd.conf)
#SERVERNAME=localhost
echo ServerName changed from $SERVERNAME to $IP
perl -pi -e "s/(^ServerName).*/\1 $IP/" /etc/apache/httpd.conf 
/etc/init.d/apache restart
#perl -pi -e "s/^(ServerName).*/\1 $SERVERNAME/" /etc/apache/httpd.conf
