#!/bin/sh
# -*- Shell-script -*-
# Copyright (C) 2003 by Johnny Lai
#
# This script 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 script 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Returns relative path to source dir assuming source dir is at $SOURCEDIR
# For out of source builds with many diff config files and netconf2.dtd etc.
# Manual activation via source required.

if [ "$SOURCEDIR" = "" ]; then
  SOURCEDIR=~/src/IPv6Suite
fi
function pws()
{
	cpath=`pwd`;
	#find top level IPv6Suite dir by looking for config.h
	while [ "`dirname $cpath`" != "~/" ]
	do
		cd ..;
		if ls config.h &>/dev/null; then
			RealPath=`echo $cpath|perl -i -pwe "s|\`pwd\`|$SOURCEDIR|"`;
			break;
		fi
	done
	cd $cpath
	echo -n $RealPath
}

function pwsm()
{
	if [ -f ~/src/phantasia/master/$1 ]; then
	  DIR=~/src/phantasia/master 
	elif [ -f ~/src/phantasia/vpac/doc/omnetpp2002/$1 ]; then
	  DIR=~/src/phantasia/vpac/doc/omnetpp2002
	else
	  DIR=~/src/phantasia/templates
	fi
}
function diffm()
{
	pwsm $1
	diff $DIR/$1 $1
	unset DIR
}

function rcpsm()
{
	pwsm $1
	cp -p $1 $DIR/$1 
	ln -sf $DIR/$1 .
	unset DIR
}

function diffs()
{
	diff `pws`/$1 $1
}
function cps()
{
	cp `pws`/$1 $1
}
function rcps()
{
    cp $1 `pws`/$1
}
function fnd()
{
    find $1 -name "$2"
}
function fn()
{
    fnd . "$1"
}

function psgrep()
{
    ps -ef|grep -i "$1"|grep -v "grep -i $1"
}

function fngrep()
{
    grep -l "$1" `fn "$2"`
}

function fnlgrep()
{
    grep -n "$1" `fn "$2"`
}

function rpm2cpiox()
{
    rpm2cpio "$1" |cpio -dim
}

NOFASTSUFFIX=nofast
function convnofast()
{
    cp -p $1{,-$NOFASTSUFFIX}.xml
    perl -i -pwe 's|MaxFastRAS="10"|MaxFastRAS="0"|g' $1-$NOFASTSUFFIX.xml
}

PCOASUFFIX=pcoaf
#Relies on fact of having AdvHomeAgent="off" for ARs
function convpcoaf()
{
   cp -p $1{,-$PCOASUFFIX}.xml
   perl -i -pwe 's|AdvHomeAgent="off"|AdvHomeAgent="on"|g' $1-$PCOASUFFIX.xml
}

#Relies on hierarchicalMIPv6Support="off" for MN and maybe even map too (not that it matters)
HMIPSUFFIX=hmip
function convhmip()
{
   cp -p $1{,-$HMIPSUFFIX}.xml
   perl -i -pwe 's|hierarchicalMIPv6Support="off"|hierarchicalMIPv6Support="on"|g' $1-$HMIPSUFFIX.xml
}

#Some stuff from here http://www.jan0sch.de/download/conf/profile.local
# unpack .tgz and .tbz files
function tb { tar jxf $1 ;}
function tg { tar zxf $1 ;}
# i don't like my tabs too wide
function less { /usr/bin/less -x2 $1 ;}


#OpenPackage pull env variables (accepts base dir of installation)
#OpenPKG Add Environment
opa () {
    root="$1"
    if [ ! -d $root ]; then
        echo "opa:ERROR: OpenPKG root directory $root not found"
        return 1
    fi
    if [ ! -f "$root/etc/rc" ]; then
        echo "opa:ERROR: root directory does not contain OpenPKG
hierarchy"
        return 1
    fi
    eval `$root/etc/rc --eval all env`
    alias rpmbuild='openpkg rpm'
    alias rpm='openpkg rpm'
    alias opu='openpkg rpm -Uvh'
    alias opb='openpkg rpm --rebuild'
    export PKG_CONFIG_PATH=$root/lib/pkgconfig
}

function opp()
{
    export PATH=$PATH:.
    unset LC_CTYPE
}

function aacplay()
{
    faad -w "$1" | esdcat
}

function aacrec()
{
    #$1 is input wave file. #$2 is output name usually .aac suffix
    faac -m 4 -a 64 -c 48000 "$1" "$2"
}

function tcls()
{
    /sbin/tc -s -d qdisc ls
}
