#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2010 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

}

