# POSIX -*- sh -*-
#

LS_OPTIONS=
for lsopt in -B --color=auto -FN; do
    ls $lsopt > /dev/null 2>&1 && LS_OPTIONS="$LS_OPTIONS $lsopt"
done

if [ `uname -s` = "FreeBSD" ] && alias | grep -q "ls="; then
    unalias ls
fi

GREP_OPTIONS=
for grepopt in --colour=auto; do
    grep -q $grepopt . /etc/passwd && GREP_OPTIONS="$GREP_OPTIONS $grepopt"
done

findcommand() {
    while [ "$#" -ge 1 ]; do
        P="$PATH"
        while [ "$P" ]; do 
            D=${P%%:*}
            P=${P#*:}
            if [ "$D" = "$P" ]; then
                P=
            fi
            if [ -z "$D" ]; then
                D=.
            fi
            if [ -x "$D/$1" ]; then
                echo "$D/$1"
                return 0
            fi
        done
        shift
    done
    return 1
}

nod () {
    NOD="$HOME/Desktop/NoD.txt"
    if ! [ -f "$NOD" ]; then 
        echo "No ~/Desktop/NoD.txt"
        exit 1
    fi
    if ! grep -q $(date -I) ${NOD}; then
        printf "\n%s:\n\n- Mail, IRC, etc\n" $(date -I) >> ${NOD}
    fi
    printf "- %s" "$@">> ${NOD}
    vim + ${NOD}
}

meetingdate () {
    TIME=$1
    TZMEETING=$2
    TZ=${TZ:=$3}
    date -d "$(TZ=$TZMEETING date --rfc-822 -d "$TIME")"
}

blog () {
    pushd $(pwd)
    if [ ! -d "$HOME/blog" ]; then
        echo "You don't have your blog checked out here.  Can't blog."
        return 1
    fi
    if [ "$1" ]; then
        CAT="$1"
    else
        CAT="/"
    fi

    if [ "$2" ]; then
        SUBJ="$(echo $2 | tr ' ' '_')"
    else
        SUBJ="blank_subject"
    fi
    DIR=$HOME/blog/$CAT/
    FILE=$(date +"%Y-%m-%d-%H-%M")_$SUBJ.txt
    cd $DIR
    $EDITOR $FILE
    if [ "$OFFLINE" != "1" ]; then
        svn add $FILE
        svn ci -m"Initial commit" $FILE
    fi
    popd
}

apache2Release()
{
    A2ROOT=http://svn.clearairturbulence.org/apache2/trunk
    if [ "$1" = "release" ]; then
        DBCMD='debuild';
    else
        DBCMD='dpkg-buildpackage -rfakeroot -uc -us';
    fi
    pushd > /dev/null;
    [ ! -d /tmp/debian ] && mkdir -p /tmp/debian
    svn cat "$A2ROOT/debian/changelog" > /tmp/debian/changelog;
    cd /tmp && VER=$(dpkg-parsechangelog|grep urgency|sed -e 's/.*(\(.*\)-.*).*/\1/');
    popd > /dev/null;
    rm -r apache2-$VER apache2_$VER*diff.gz apache2_$VER*-*;
    svn export "$A2ROOT" apache2-$VER;
    if [ ! -f apache2_$VER.orig.tar.gz ]; then
        cd apache2-$VER && fakeroot debian/rules clean && cd ..;
        mv apache2-$VER/debian ./debian-$VER;
        tar czf apache2_$VER.orig.tar.gz apache2-$VER;
        mv debian-$VER apache2-$VER/debian;
    fi
    cd apache2-$VER && $DBCMD && cd ..;
}

is_debian () {
    if [ -f /etc/debian_version ]; then
        return 0
    fi
    return 1
}

removepathelem () {
    PATH=$(echo "$PATH" | sed -e "s,:*$1:,:,g" -e "s,::,:,g")
}

setpathifdir () {
    if [ -d "$1" ]; then
        removepathelem "$1"
        PATH="$1:$PATH"
    fi
}

appendpathifdir () {
    if [ -d "$1" ]; then
        removepathelem "$1"
        PATH="$PATH:$1"
    fi
}

busstuc () {
        echo "lang=nor&quest=$@" | \
        xargs -0 -i wget -q -O - --post-data {} http://www.idi.ntnu.no/~tagore/cgi-bin/busstuc/busq.cgi| \
        html2text -width 1000| \
        sed '/[[:alpha:]]/p;d'
}

if is_debian; then
    appendpathifdir /store/gnu/bin
    appendpathifdir /store/bin
else
    setpathifdir /store/gnu/bin
    setpathifdir /store/bin
fi
setpathifdir /usr/local/bin
setpathifdir $HOME/usr/bin
setpathifdir $HOME/bin
appendpathifdir /usr/sbin
appendpathifdir /sbin

if [ `id -u` = "0" ]; then 
    setpathifdir /sbin
    setpathifdir /usr/sbin
    setpathifdir /usr/local/sbin
    setpathifdir /local/sbin
fi

CVSROOT=~/data/cvs
EDITOR=emacs
TZ=CET
WWW_HOME=http://localhost/~tfheen/services.html
test -x "$HOME/bin/lesspipe.sh" && LESSOPEN="|$HOME/bin/lesspipe.sh '%s'"
test -x "$HOME/bin/lesspipe" && LESSOPEN="|$HOME/bin/lesspipe '%s'"
LESS="-m"
RSYNC_RSH=ssh
CVS_RSH=ssh
#MAIL=/var/spool/mail/tfheen
DEBEMAIL=tfheen@debian.org
DEBFULLNAME="Tollef Fog Heen"
CVSEDITOR="emacs -nw"
SVN_EDITOR="emacs -nw"
XTERM=$(findcommand gnome-terminal pterm Eterm xterm)
PAGER=less
GNUSTEP_USER_ROOT="$HOME/.GNUstep"
OOO_DISABLE_RECOVERY=True
GTK_IM_MODULE=xim

CHROOT=""
if [ -f /etc/debian_chroot ]; then
    CHROOT="($(cat /etc/debian_chroot | tr -d '\n'))"
fi

if test `uname -s` = "Linux" && test `uname -m` = i686; then
    ldlib=ld-linux.so.2
fi

case `uname -s` in
    *BSD|SunOS)
        HOSTNAME=`/bin/hostname`
        DOMAINNAME=`/bin/hostname | cut -d. -f 2-`
        ;;
    Linux)
        HOSTNAME=`/bin/hostname -f`
        DOMAINNAME=`/bin/hostname -f | cut -d. -f 2-`
        ;;
esac

if [ -z "$LANG" ] || [ "$LANG" = "C" ]; then
    if findcommand locale >/dev/null; then
        if locale -a | grep -q '^nb_NO.utf8$' ; then
            LANG=nb_NO.UTF-8
        elif locale -a | grep -q '^en_US.utf8$'; then
            LANG=en_US.UTF-8
        fi
    fi
fi

if [ -z "$LANG" ] || [ "$LANG" = "C" ]; then
    if findcommand localedef >/dev/null; then
        localedef --list-archive > /dev/null 2>&1
        if test "$?" = "0"; then
            if localedef --list-archive | grep -q '^no_NO$' ; then
                LANG=no_NO
            elif localedef --list-archive | grep -q '^en_US$'; then
                LANG=en_US
            fi
        fi
    fi
fi

case "$DOMAINNAME" in
    samfundet*)
        mesg y
        EMAIL=tfheen@samfundet.no
        ;;
    *)
        :
        ;;
esac

removepathelem "\\."

export WINDOW_MANAGER=$(findcommand openbox metacity twm)

export LS_OPTIONS CVSROOT EDITOR TZ WWW_HOME LESSOPEN LESS RSYNC_RSH
export CVS_RSH LANG DEBEMAIL CVSEDITOR ldlib HOSTNAME DOMAINNAME PATH 
export EMAIL XTERM SVN_EDITOR GNUSTEP_USER_ROOT GREP_OPTIONS 
export OOO_DISABLE_RECOVERY GTK_IM_MODULE

if [ -f "$HOME/.gnupg/gpg-agent-info-$HOSTNAME" ]; then
    . "$HOME/.gnupg/gpg-agent-info-$HOSTNAME"
elif [ -f "$HOME/.gnupg/gpg-agent-info-$(hostname)" ]; then
    . "$HOME/.gnupg/gpg-agent-info-$(hostname)"
fi
