# clear all old aliases
# XXX this wouldn't work with functions, and stuffs up aliases from elsewhere
# (e.g. ls), so disabled
#unalias aliases 2>/dev/null
#aliases() { alias | sed 's/alias //; s/=.*//'; }
#unalias `aliases` 2>/dev/null

export clear=`clear`
alias clear='echo -n "$clear"'

#source /home/mod_perl/hm/conf/home/.alias

# Ensure only root edits crontab
alias editcron='vim ~mod_perl/hm/conf/cron/backendcrontab; [ `perl -e "print $<"` == "0" ] && crontab ~mod_perl/hm/conf/cron/backendcrontab'

# editing common files
alias va='vi ~/.alias'
alias vl='vi ~/.larswmrc'
alias vr='vi ~/.rc'
alias eb='exec bash'
alias sb='. ~/.rc'
alias vxi='vi ~/.xinitrc'
alias vs='vi ~/.session'
alias vp='vi ~/.profile'
alias vc='vi ~share/profile/common'
alias vsam='vi ~share/profile/sam'

# naming X windows
nw() { xname `xsel` "$*"; }
nt() { echo -ne "\033]0;$*\007"; }

# launching and killing stuff
alias diso='disown %`jobs | tail -n1 | sed '"'"'s/.\([0-9]*\).*/\1/'"'"'`'
launchqd() { "$@" >/dev/null 2>&1 & diso; }
launchq() { "$@" >/dev/null 2>&1 & }
launchd() { "$@" & diso; }
launchb() { "$@" & }
laqd() { launchqd "$@"; }
lad() { launchd "$@"; }
laq() { launchq "$@"; }
lab() { launchb "$@"; }

launch() { lad "$@"; }   # NOTE: change THIS line for a different launch behaviour,
la() { launch "$@"; }    #       NOT this one!!

alias lt='la xterm'
alias konq='la konqueror'
alias dil='la dillo'
alias moz='la mozilla'
alias fb='la firebird'
alias ff='la firefox'
alias ym='la ymessenger'
alias abi='la abiword-2.0'
alias kym='killall ymessenger'
alias kmoz='killall mozilla-bin'

alias pst='pstree -u `whoami`'

# reminders (used to be called nags, motd)
reminder() {
	if [ $# = 0 ]; then
		vi ~/.remind
	else
		{ [ -s ~/.remind ] && echo ; echo "$@"; } >> ~/.remind
	fi
}

reminders() { [ -s ~/.remind ] && { echo ; echo "`<~/.remind`"; echo; } }

# notes stuff
#export NOTES="$HOME/NOTES"
export NOTES="/home/sam/notes/main"
alias note='cat >> "$NOTES"'   # use a or ed -a (FIXME ed!)  Plan 9 ed does it?
                               # send suggestion to Plan 9, GNU and BSD
                               # maintainers of `ed' to do that
                               # the a can be mapped to cle ed -a -q,
                               # which is cool i.e. -q means quit after the
                               # append.  What"s wrong with (linux?) ls.so
                               # such that LD_PRELOAD tricks often break progs?
                               # are there any OSes that support LD_PRELOAD well?
          # I think Darwin might.
          # OSes I have: BSD/Darwin/OS X (X12? in 2 more releases XFree86 and Mac OS will really breed to give Max OS XFree Version 12!)
          # GNU/Linux/X11
          # In 2004, emacs and vim will breed twin children, Bram Vimax
          # and GUP Evmacs
alias no=note
alias notes='cat "$NOTES"'
alias Vn='vi "$NOTES"'
alias vn='less "$NOTES"'
alias en='ed "$NOTES"'
alias cn='cat "$NOTES"'
gncs() { grep "$@" "$NOTES"; }
gn() { gncs -i "$@"; }

# miscellaneous stuff
alias cx='chmod +x'
hl() { "$1" --help 2>&1 | less; }
hg() { X="$1"; shift; "$X" --help 2>&1 | grep "$@"; }

# perl module in path - checks if a module is not installed
pmip() { for A in "$@"; do perl -e "use $A"; done 2>&1 | sed 's/ in @INC.*//; /^\(BEGIN\|Compilation\) failed/d'; }

alias su='sudo su'
alias apm='sudo apm'
esu() { exec sudo su "$@"; }

alias less='less -Q'
alias ipunlock='lynx https://cvs.fastmail.fm/cgi-bin/unlockip.pl'
gre() { grep -I -r "$@" $hm; }
gx() { g XXX; }
alias top='top -d2'
alias oclock='oclock -geometry 30x30-0-0'
alias howto='cat /root/startup/howto'
alias service='/home/mod_perl/hm/utils/Services.pl'
alias fall='fmail & fmessage & fimap & fwww & fwww2 & fallmail &'
alias cl='/home/mod_perl/hm/scripts/compresslogs.pl'
alias pingg='ping www.google.com.au'
htd() {
  kill `jobs | grep -ie apache/logs -e bin/httpd | perl -ne '/(\d+)/ && print "%$1\n"'`
  while { jobs | grep bin/httpd; }; do echo waiting; sleep 4; done
  echo it died
  ~www/bin/httpd -X -Dperlsi & sleep 4; cl; fwww & sleep 2 ; brk
	# can also use -Dperldb to start the perl debugger, then the process needs to be fg
}

#unsymlink() { A=`readlink "$1"`; [ -e "$A" ] && { rm "$1"; cp "$A" "$1" -i; } }
acut() { awk '{print $'$1'}';}
debs() { [ -e index.html ] && { echo index.html exists, sorry >&2; return; }; a=`echo $1 | cut -c1`; wget -q ftp://mirror.aarnet.edu.au/debian/pool/main/$a/$1/ && < .listing grep i386 | grep -v hurd | acut 9 && rm .listing index.html; }
deb() { a=`echo $1 | cut -c1`; wget ftp://mirror.aarnet.edu.au/debian/pool/main/$a/$1/$2; }
cdeb() { a=`echo $1 | cut -c1`; wget -c ftp://mirror.aarnet.edu.au/debian/pool/main/$a/$1/$2; }

alias cvs='cvs -z3'
alias suspend='while true; do apm -s; echo; echo "    awake -- ^C within 5 seconds to resume"; echo; sleep 5; done'
alias pon='ssh bart sudo /etc/init.d/ppp start'
alias poff='ssh bart sudo poff'
alias inet=pon
opens () { strace "$@" 2>&1 >/dev/null | perl -ne '/^open\("(.*?)"/ && print $1'; }
notproc () { p 'm|/\d+/| && p'; }

pl () { perl -I"$HOME"/pm -mDefaults::Sam "$@"; }
pe () { pl -e "$@"; }
pn () { pe -n "$@"; }
pp () { pe -p "$@"; }
pne () { pe -n "$@"; }
ppe () { pe -p "$@"; }

alias mf='mount /mnt/floppy'
alias umf='umount /mnt/floppy'
alias mcd='mount /mnt/cdrom'
alias umcd='umount /mnt/cdrom'

alias u='whoami'
alias killx='killall X'
alias killa='killall sss-agent; rm -rf /tmp/.sa'
alias edisp='export DISPLAY=:0.0'

mail_alias() { grep -i $1 ~/.mail_aliases; }
alias ma=mail_alias

#alias bms='cat ~/personal/www_bookmarks'
#alias bm='cat >> ~/personal/www_bookmarks'
cards() { lynx http://cards.sourceforge.net/; }
card() { cat ~/c/$1; }
alias demo='(unset http_proxy ; dillo http://cards/ &)'
#wiki() { lynx http://wiki.shallow.net/cards?$1; }
#edit() { lynx http://cards.sourceforge.net/cgi-bin/edit?$1; }
#view() { lynx http://cards.sourceforge.net/cgi-bin/view?$1; }
#search() { lynx http://cards.sourceforge.net/cgi-bin/search?query="$1"; }
#lucky() { lynx http://cards.sourceforge.net/cgi-bin/search?lucky=1\&query="$1"; }
alias cards_listinfo='lynx http://yello.shallow.net/mailman/listinfo/'
alias cards_admin='lynx http://yello.shallow.net/mailman/admin/'
alias cards_admindb='links http://yello.shallow.net/mailman/admindb/cards'
alias 225q_listinfo='lynx http://cmetech.com.au/cgi-bin/mailman/listinfo/225q'
alias 225q_admin='lynx http://cmetech.com.au/cgi-bin/mailman/admin/225q'
alias 225q_admindb='links http://cmetech.com.au/cgi-bin/mailman/admindb/225q'

if [ `whoami` != root ]; then
  # friend support routines!
  friends="$HOME/personal/friends"
  friends() { cat $friends; }
  friend() {
    if [ ! -s $friends.1 ]
    then
        L=`wc -l <$friends`
        R=$((RANDOM % ($L - 4) + 2))
        head -n $R <$friends >$friends.1
        (tail +$((R+1)) <$friends ; shuffle <$friends.1) > $friends.2
        mv $friends.2 $friends
    fi
    T=$(( `date +%s` - `<$friends.t` ))
    D=$(( $T / 3600 / 24 ))
    H=$(( $T / 3600 - $D * 24 ))
    M=$(( $T / 60 - $D * 24 * 60 - $H * 60 ))
    echo -n $D $H:$M" "
    head -n1 "$friends.1"
  }
  alias f=friend
  random_friend() { shuffle < "$friends" | head -n1; } # inefficient
  alias rf=random_friend
  new_friend() { cat >> "$friends"; }
  alias nf=new_friend
  contacted_friend() {
    OLD=`friend`
    tail +2 $friends.1 > $friends.2
    mv $friends.2 $friends.1
    friend
    date +%s >|$friends.t
  }
#  alias cf=contacted_friend

#    alias pills='date +$df > ~/.pills'
#    alias unpills='date -d yesterday +$df > ~/.pills'

  my() {
    F="$HOME/cards/my/$1"
    [ -e "$F" ] && cat "$F"
  }
  my+() {
    F="$HOME/cards/my/$1"
    [ -e "$F" ] && cat "$F"
    cat >> "$F"
    [ -s "$F" ] || rm "$F"
  }

  grep_mail() {
    pushd ~/Mail >/dev/null
    grep "$@" *
    popd
  }

  find_info() {
    grep -i $1 ~/personal/people
    mail_alias $1
    card $1 2>/dev/null
    my $1 2>/dev/null
    grep -i $1 ~/personal/www_bookmarks
    grep -i $1 ~/.dillo/bookmarks.html
    grep -i $1 ~/home.bart/.netscape/bookmarks.html
    grep -i $1 ~/home.bart/.dillo/bookmarks.html
  }

  alias i=find_info
fi

#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'

stty -ixon

export HISTSIZE=10000
export HISTFILESIZE=10000

#    lastpills=`<$HOME/.pills`
#    date=`date +$df`
#    if [ $lastpills != $date ]; then echo "TAKE PILLS!"; echo; fi

# preconfiguration
#alias wget='wget --passive-ftp -nr'

# log breaking
alias brk="printf '_______________________________________________________________________________\n\n'"
alias cbrk='clear; brk'
wbrkpat="-\`^'-,_."
wbrkpati="-,_.-\`^'"
wbrk() { printf -- "\n$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat$wbrkpat\n\n"; }
alias cwbrk='clear; wbrk'
dwbrk() {
  printf "\nO%s %s %sO\n\n" \
         "$wbrkpat$wbrkpat$wbrkpat$wbrkpat-" `da` \
         "$wbrkpati$wbrkpati$wbrkpati$wbrkpati-"
}

# killing jobs
killjobs() { kill "$@" `jobs -p`; jobs; }
alias kj=killjobs # XXX dangerous?

# fastmail stuff - startup / shutdown
startup_real() { source /root/startup/howto; brk; jobs; wbrk; }
if [ `whoami` = root ]; then
  startup() { startup_real; }
else
  startup() { echo 'su first'; sudo su; }
  ## XXX FIXME alias startup="esu -c 'source /root/howto'"
fi
alias fm=startup

# splish support, should not be needed, but maybe it is
alias pk=passionate_kisses
[ -n "$splish_prog" ] && pk
alias W='echo ${STY#*.}'

#src() { source ~/.bashrc; }  # fixme should ~switch~ on $SHELL
                             # (we need a reflected ~ so can be a quote!
                             # how about using " as a quote?
                             # Maybe with .. to term (where `.' is a low tick.)

# +1:s/George/Goirge/rR!
# NOTE George Negus - `the world from ISLAM' - A journey of discovery through the Muslim heartland
#
#      a book, ISBN: 0-7322-7623-3

sql_mysql() {
	mysql mysql -u root --password=`cat /etc/fmpasswd`
}

sql_ispmatch() {
	mysql ISP_MATCH -u ispm16668 --password=c8gvuus
}

KILL() { kill -KILL "$@"; }
kstop() { kill -STOP "$@"; }
cont() { kill -CONT "$@"; }
ux() { export DISPLAY=:0.0; }
nx() { unset DISPLAY; }
sa() { source ~/.alias; }

# from splish
list_files() {
	/bin/ls --color=${colour_ls_opt:-auto} -t $list_files_opts -C "$@"
}
list_files_1() {
	/bin/ls --color=${colour_ls_opt:-auto} -t $list_files_opts -1 "$@"
}
list_files_ctime() {
	/bin/ls --color=${colour_ls_opt:-auto} -tc $list_files_opts -C "$@"
}
list_files_ctime_1() {
	/bin/ls --color=${colour_ls_opt:-auto} -tc $list_files_opts -1 "$@"
}

alias l='list_files'
alias lm='list_files_mtime'
alias ll='ls -ltc'
alias nh='unset HISTFILE'
alias ss='stty sane'

alias ,='cd ~/c; l'
,,() {
	if [ $# -gt 0 ]; then
		for A; do
			ln -s `p "$A"` ~/c
		done
	else
		ln -s `p .` ~/c
	fi
}

#alias ftp='ftp -p -z nossl'

fnstat() {
	( while true; do
		netstat -tap | grep java | grep -v LISTEN | grep -v localhost
		echo
		w ; echo ; javacount ; echo
		sleep 10
	done ) &
	sleep 1
	dlrate &
}

alias wshaper='sudo ~share/hacks/wshaper'
alias wshaper_half='sudo ~share/hacks/wshaper_half'
alias wshaper_trickle='sudo ~share/hacks/wshaper_trickle'
alias wshaper_updown='sudo ~share/hacks/wshaper_updown'
alias www=elinks

what_is_going_on() {
	echo ${STY#*.}
}

alice_ssh() {
	export RSYNC_RSH="ssh -p 2345"
}

alias cf=fixcon
alias n='. n'

cdp() { cd `p ${1:-.}`; }
