#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2011 Christopher Bratusek		#
#							#
#########################################################

top10 () {

	history | awk '{print $2}' |\
	awk 'BEGIN {FS="|"}{print $1}' |\
	sort | uniq -c | sort -n | tail | sort -nr

}

crypt () {

	if [[ -e "$1" ]]; then
		tr a-zA-Z n-za-mN-ZA-M < "$1" > "$1".crypt
		rm -f "$1"
		mv "$1".crypt "$1"
	fi
}

get_kernel () {

	if [[ $1 == "-s" ]]; then
		wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-${2}.tar.bz2
	elif [[ $1 == "-t" ]]; then
		wget http://www.kernel.org/pub/linux/kernel/v2.6/testing/linux-${2}.tar.bz2
	elif [[ $1 == "-sg" ]]; then
		git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-${2}.y.git
	elif [[ $1 == "-tg" ]]; then
		git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
	elif [[ $1 == "-z" ]]; then
		wget http://downloads.zen-kernel.org/${2}/${2}-zen${3}.patch.lzma
	fi

}

stop_watch () {

BEGIN=$(date +%s)

while true; do
    NOW=$(date +%s)
    DIFF=$(($NOW - $BEGIN))
    MINS=$(($DIFF / 60))
    SECS=$(($DIFF % 60))
    echo -ne "Time elapsed: $MINS:`printf %02d $SECS`\r"
    sleep .1
done

}

bashtips () {

cat <<EOF
DIRECTORIES
-----------
~-          Previous working directory
pushd tmp   Push tmp && cd tmp
popd        Pop && cd

GLOBBING AND OUTPUT SUBSTITUTION
--------------------------------
ls a[b-dx]e Globs abe, ace, ade, axe
ls a{c,bl}e Globs ace, able
\$(ls)      \`ls\` (but nestable!)

HISTORY MANIPULATION
--------------------
!!        Last command
!?foo     Last command containing \`foo'
^foo^bar^ Last command containing \`foo', but substitute \`bar'
!!:0      Last command word
!!:^      Last command's first argument
!\$       Last command's last argument
!!:*      Last command's arguments
!!:x-y    Arguments x to y of last command
C-s       search forwards in history
C-r       search backwards in history

LINE EDITING
------------
M-d     kill to end of word
C-w     kill to beginning of word
C-k     kill to end of line
C-u     kill to beginning of line
M-r     revert all modifications to current line
C-]     search forwards in line
M-C-]   search backwards in line
C-t     transpose characters
M-t     transpose words
M-u     uppercase word
M-l     lowercase word
M-c     capitalize word

COMPLETION
----------
M-/     complete filename
M-~     complete user name
M-@     complete host name
M-\$    complete variable name
M-!     complete command name
M-^     complete history
EOF

}

ll() {

	ls -l --group-directories-first "$@"

}

la() {

	ls -A --group-directories-first "$@"

}

l1() {

	ls -1 --group-directories-first "$@"

}

lo() {

	if [[ "$@" == "" ]]; then
		$@="*"
	fi

	ls -l --group-directories-first "$@" | gawk '{print $9, "belongs to User ->", $3}' | sed -e '1d' | column -t

}

lg() {

	if [[ "$@" == "" ]]; then
		$@="*"
	fi

	ls -l --group-directories-first "$@" | gawk '{print $9, "belongs to Group ->", $4}' | column -t

}

lm() {

	if [[ ! "$@" == "" ]]; then

		for file in "$@"; do
			stat -c "%A %a %n" "$file" | gawk '{print "Permissions of:", $3, "->", $1, "("$2")"}'
		done | column -t

	fi

}

define ()
{
	check_opt lynx define

	if [[ $? != "1" && $1 ]]; then
		lynx -dump "http://www.google.com/search?hl=en&q=define%3A+${1}&btnG=Google+Search" | grep -m 3 -w "*"  | sed 's/;/ -/g' | cut -d- -f1 > /tmp/templookup.txt
			if [[ -s  /tmp/templookup.txt ]] ;then
				until ! read response
				do
					echo "${response}"
				done < /tmp/templookup.txt
			else
				echo "Sorry $USER, I can't find the term \"${1} \""
				fi
		rm -f /tmp/templookup.txt
	else	echo "No keyword given"
	fi
}

resetleds ()
{

	setleds -L < /dev/tty1

}

kitt () {

	setleds -L -num;
	setleds -L -caps;
	setleds -L -scroll;

	while :; do
		setleds -L +num;
		sleep 0.2;

		setleds -L -num;
		setleds -L +caps;
		sleep 0.2;

		setleds -L -caps;
		setleds -L +scroll;
		sleep 0.2;

		setleds -L -scroll;
		setleds -L +caps;
		sleep 0.2;

		setleds -L -caps;
	done

	resetleds

}

hilow()
{
      biggest=1000                            # maximum number possible
      guess=0                                 # guessed by player
      guesses=0                               # number of guesses made
      number=$(( $$ % $biggest ))             # random number, 1 .. $biggest
      while [ $guess -ne $number ] ; do
	echo -n "Guess? " ; read guess
	if [ "$guess" -lt $number ] ; then
	  echo "... bigger!"
	elif [ "$guess" -gt $number ] ; then
	  echo "... smaller!"
	fi
	guesses=$(( $guesses + 1 ))
      done
      echo "Right!! Guessed $number in $guesses guesses."
}

batch()
{
	find . -type f -iname '*.'${1}'' -exec ${@:2}  {} \; ;
}
