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

if [[ ${BASH_VERSINFO[0]} -ge "4" ]]; then

	xdel() {

		shopt -s globstar

		[[ "$@" != "" ]] &&
			{ for argv in "$@"; do
				rm -fv ./**/*"$argv"
			done }

	}

fi

trunc_pwd ()
{

	case $1 in
		*help )
			echo -e "\n${ewhite}Usage:\n"
			echo -e "${eorange}trunc_pwd ${ewhite}|${egreen} ! no options !\n"
			tput sgr0
		;;

		* )

			pwdmaxlen=$(get_key pwdmaxlength)
			trunc_symbol=$(get_key pwdchar)
			dirchar=$(get_key dirchar)

			newsepacolor=$(echo $sepacolor | sed -e 's/\\\[//g' -e 's/\\\]//g')
			newwdircolor=$(echo $wdircolor | sed -e 's/\\\[//g' -e 's/\\\]//g')
			newpscoloror=$(echo $pscolor | sed -e 's/\\\[//g' -e 's/\\\]//g')

			if [ ${#PWD} -gt $pwdmaxlen ]; then
				pwdoffset=$(( ${#PWD} - $pwdmaxlen ))
				if [[ $pstyle_supported == true ]]; then
					xPWD="$newpscoloror${trunc_symbol}${PWD:$pwdoffset:$pwdmaxlen}"
				else	xPWD="${trunc_symbol}${PWD:$pwdoffset:$pwdmaxlen}"
				fi
				if [[ $enabcol == true && $pstyle_supported == true ]]; then
					newPWD="${xPWD//\//$newsepacolor$dirchar$newwdircolor}"
				else	newPWD="${xPWD//\//$dirchar}"
				fi
			else
				if [[ $enabcol == true && $pstyle_supported == true ]]; then
				newPWD="${PWD//\//$newsepacolor$dirchar$newwdircolor}"
			else	newPWD="${PWD//\//$dirchar}"
			fi
			fi

			echo -e $newPWD
		;;
	esac

}

up () {

[ "${1/[^0-9]/}" == "$1" ] && {
        local ups=""
        for ((i=1; i<=$1; i++))
        do
                ups=$ups"../"
        done
        cd $ups
        }

}

empty () {

	find "$1" -empty

}

deadlink () {

	find -L -type l
}

bookmark () {

	if [[ $1 != "" && $(alias | grep -w go-$1) == "" ]]; then
		echo "alias go-$1='cd $PWD'" >> $HOME/.bookmarks
		. $HOME/.bookmarks
	elif [[ $1 == "" ]]; then
		echo "need name for the bookmark."
	else	echo "bookmark go-$1 already exists."
	fi

}

unmark () {

	if [[ $(alias | grep -w go-$1= ) != "" ]]; then
		sed -e "/go-$1/d" -i $HOME/.bookmarks
		xunalias go-$1
	fi

}

man2pdf ()
{

	case $1 in
		*help | "" )
			echo -e "\n${ewhite}Usage:"
			echo -e	"\n${eorange}man2pdf${ewhite} |${egreen} <manualpage>${eiceblue} [generate a pdf from <manualpage>]\n"
			tput sgr0
		;;

		* )
			check_opt ps2pdf man2pdf
			if [[ $? != "1"  && $1 ]]; then
				man -t $1 | ps2pdf - >$1.pdf
			else	echo "No manpage given."
			fi
		;;
	esac

}

exchange()
{
	typeset temp
	SAVE=$IFS
	IFS=''
	eval $(echo "temp=\"\$$1\"")
	eval $(echo "$1=\"\$$2\"")
	eval $(echo "$2=\"$temp\"")
	IFS=$SAVE
}

swap()
{

	if [[ $1 && $2 ]]; then
		mv "$1" temp.$$
		mv "$2" "$1"
		mv temp.$$ "$2"
	fi

}

progressbar()
{

	SP_COLOUR="\e[37;44m"
	SP_WIDTH=5.5
	SP_DELAY=0.2
	SP_STRING=${2:-'+     '}

	while [ -d /proc/$1 ]
	do
		printf "$SP_COLOUR\e7  %${SP_WIDTH}s  \e8\e[01;37m" "$SP_STRING"
		sleep ${SP_DELAY:-.2}
		SP_STRING=${SP_STRING#"${SP_STRING%?}"}${SP_STRING%?}
	done

	tput sgr0

}

spinner()
{

	PROC=$1
	while [ -d /proc/$PROC ];do
		echo -ne '\e[01;32m/\x08' ; sleep 0.05
		echo -ne '\e[01;32m-\x08' ; sleep 0.05
		echo -ne '\e[01;32m\\\x08' ; sleep 0.05
		echo -ne '\e[01;32m|\x08' ; sleep 0.05
	done

}


spanner(){

	PROC=$1;COUNT=0
	while [ -d /proc/$PROC ];do
		while [ "$COUNT" -lt 10 ];do
			echo -ne '\x08  ' ; sleep 0.1
			((COUNT++))
		done
		until [ "$COUNT" -eq 0 ];do
			echo -ne '\x08\x08 ' ; sleep 0.1
			((COUNT -= 1))
		done
	done

}

working ()
{
   while [ -d /proc/$1 ]
   do
	echo -ne "w      \b\b\b\b\b\b\b";sleep .08;
	echo -ne "wo     \b\b\b\b\b\b\b";sleep .08;
	echo -ne "wor    \b\b\b\b\b\b\b";sleep .08;
	echo -ne "work   \b\b\b\b\b\b\b";sleep .08;
	echo -ne "worki  \b\b\b\b\b\b\b";sleep .08;
	echo -ne "workin \b\b\b\b\b\b\b";sleep .08;
	echo -ne "working\b\b\b\b\b\b\b";sleep .08;
	echo -ne " orking\b\b\b\b\b\b\b";sleep .08;
	echo -ne "  rking\b\b\b\b\b\b\b";sleep .08;
	echo -ne "   king\b\b\b\b\b\b\b";sleep .08;
	echo -ne "    ing\b\b\b\b\b\b\b";sleep .08;
	echo -ne "     ng\b\b\b\b\b\b\b";sleep .08;
	echo -ne "      g\b\b\b\b\b\b\b";sleep .08;
   done

}

spin () {

        echo -n "|/     |"
        while [ -d /proc/$1 ]
        do
        # moving right
        echo -ne "\b\b\b\b\b\b\b-     |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\b\\     |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\b|     |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\b /    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b-    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\\    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b|    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b /   |"
        sleep .03
        echo -ne "\b\b\b\b\b-   |"
        sleep .03
        echo -ne "\b\b\b\b\b\\   |"
        sleep .03
        echo -ne "\b\b\b\b\b|   |"
        sleep .03
        echo -ne "\b\b\b\b\b /  |"
        sleep .03
        echo -ne "\b\b\b\b-  |"
        sleep .03
        echo -ne "\b\b\b\b\\  |"
        sleep .03
        echo -ne "\b\b\b\b|  |"
        sleep .03
        echo -ne "\b\b\b\b / |"
        sleep .03
        echo -ne "\b\b\b- |"
        sleep .03
        echo -ne "\b\b\b\\ |"
        sleep .03
        echo -ne "\b\b\b| |"
        sleep .03
        echo -ne "\b\b\b /|"
        sleep .03
        echo -ne "\b\b-|"
        sleep .03
        echo -ne "\b\b\\|"
        sleep .03
        echo -ne "\b\b||"
        sleep .03
        echo -ne "\b\b/|"
        sleep .03

        # moving left
        echo -ne "\b\b||"
        sleep .03
        echo -ne "\b\b\\|"
        sleep .03
        echo -ne "\b\b-|"
        sleep .03
        echo -ne "\b\b\b/ |"
        sleep .03
        echo -ne "\b\b\b| |"
        sleep .03
        echo -ne "\b\b\b\\ |"
        sleep .03
        echo -ne "\b\b\b- |"
        sleep .03
        echo -ne "\b\b\b\b/  |"
        sleep .03
        echo -ne "\b\b\b\b|  |"
        sleep .03
        echo -ne "\b\b\b\b\\  |"
        sleep .03
        echo -ne "\b\b\b\b-  |"
        sleep .03
        echo -ne "\b\b\b\b\b/   |"
        sleep .03
        echo -ne "\b\b\b\b\b|   |"
        sleep .03
        echo -ne "\b\b\b\b\b\\   |"
        sleep .03
        echo -ne "\b\b\b\b\b-   |"
        sleep .03
        echo -ne "\b\b\b\b\b\b/    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b|    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\\    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b-    |"
        sleep .03
        echo -ne "\b\b\b\b\b\b\b/     |"
        sleep .03
        done
	echo -e "\b\b\b\b\b\b\b\b\b|=======| done!"
}

clock ()
{
	while true; do
		clear;
		echo "===========";
		date +"%r";
		echo "===========";
		sleep 1;
	done
}

binary-clock ()
{
	clear
	perl -e 'for(;;){@d=split("",`date +%H%M%S`);print"\r";for(0..5){printf"%.4b ",$d[$_]}sleep 1}'
}

lscd () {

	builtin cd "${@}" &>/dev/null
	. $BSNG_RC_DIR/dirinfo/display
	dirinfo_display

	echo -e "${epink}content:\n"
	if [[ $(count_files -h) -gt 0 ]]; then
		ls $LSCD_OPTS
	else	echo -e "${ecream}-- empty --\n"
	fi
	echo "$PWD" > $HOME/.lastpwd

}

treecd () {

	builtin cd "${@}" &>/dev/null
	. $BSNG_RC_DIR/dirinfo/display
	dirinfo_display

	echo -e "${epink}content:"
	tree -L 1 $TREE_OPTS
	echo "$PWD" > $HOME/.lastpwd

}

treefind () {

	find "$@" | sed 's/[^/]*\//|   /g;s/| *\([^| ]\)/+--- \1/'

}

wininfo () {

	xprop | grep -w "WM_NAME\|WM_CLASS\|WM_WINDOW_ROLE\|_NET_WM_STATE"

}

repeat () {

	local period
	period=$1; shift;

	while (true); do
	    eval "$@";
	    sleep $period;
	done

}

nseq () {

	seq -w 0 $1

}

ruler () {

  for s in '....^....|' '123456789|'; do
	w=${#s}; str=$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do
		echo -n $s; done ); str=$(echo $str | cut -c -$COLUMNS) ;
	echo $str;
  done;

}

box () {

	t="$1xxxx";c=${2:-#};
	echo ${t//?/$c};
	echo "$c $1 $c";
	echo ${t//?/$c};

}

ugrep () {

	egrep -i "^[0-9a-f]{4,} .*$*" $BSNG_RC_DIR/CharName.pm | while read h d; 
		do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"
	done

}
