dkukartsev /.bashrc

#### Command line
C_RED="\[\033[0;31m\]"
C_RESET="\[\033[0m\]"
export PS1="$C_RED\w$C_RESET > "

#### Terminal
stty -ixon #  and 
if [ "$TERM" = "linux" ] ; then
    loadkeys -q .keymap
fi

#### History
export HISTIGNORE="&:ls:l[al]:[bf]g:exit:clear:ps:logout"
export HISTCONTROL=ignoreboth   # ignores duplicate entries & preceding spaces
shopt -s histappend             # appends history rather than overwriting
shopt -s cmdhist                # condenses multiple-line commands

#### Bash settings
export HISTCONTROL="ignoredups" # ignore dublicate commands

#### System variables
if [ $(uname) = "FreeBSD" ]; then
    declare FreeBSD=1;export FreeBSD
else
    declare FreeBSD=0;export FreeBSD
fi
declare PATH=$PATH:$HOME/bin;export PATH
declare PAGER=less; export PAGER
declare EDITOR=vim;export EDITOR
declare LANG=ru_RU.KOI8-R;export LANG
declare LC_ALL=ru_RU.KOI8-R;export LC_ALL
declare LSCOLORS="ExFxCxDxBxegedabagacad";export LSCOLORS # FreeBSD
declare LS_COLORS="no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:"; export LS_COLORS

###### ALIASES ######
alias :www='cd /http/'
alias 'cd..'='cd ..'

###### CMD ######
if (($FreeBSD));then
    #### GREP
    :grep(){
        if(($#==2)); then
            grep -rins --color=always $1 $2
        elif(($#==1)); then
            grep -rins --color=always $1 .
        else
            echo 'no arguments found'
        fi
    }
    #### PS
    :ps(){
        if(($#==1)); then
            ps -uxwrm -U $1 | more
        else
            ps -auxwrm | more
        fi
    }
    #### LS
    :ls(){
        ls -laG $1
    }
else
    #### GREP
    :grep(){
        if(($#==2)); then
            grep -riPns --color=always $1 $2 | more
        elif(($#==1)); then
            grep -riPns --color=always $1 . | more
        else
            echo 'no arguments found'
        fi
    }
    #### PS
    :ps(){
        if(($#==1)); then
            ps -uxw --sort=cmd -U $1 | more
        else
            ps -auxw --sort=user,cmd | more
        fi
    }
    #### LS
    :ls(){
        ls -laFh --ignore='CVS' --ignore='.cvsignore' --color $1 | more
    }
fi
#### FIND
:find(){
    if(($#==3)); then
        find $2 -name $1 -type $3 -print
    elif(($#==2)); then
        find $2 -name $1 -print
    elif(($#==1)); then
        find . -name $1 -print
    else
        echo 'no arguments found'
    fi
}
#### CD BACK
:back(){
    if [ "$OLDPWD" != "" ] && [ "$OLDPWD" != "$PWD" ];then
        cd $OLDPWD
# TODO: need to grep history here
#    elif [ "$OLDPWD" == "$PWD" ];then
#        cd
    fi
}
#### CVS COMMIT
:commit(){
    if(($#==2)); then
        cvs commit -m $2 $1
    elif(($#==1)); then
        cvs commit $1
    else
        echo 'no file found'
    fi
}
#### CVS UPDATE
:up(){
    if(($#==2)); then
        cvs up -Ad -r $2 $1
    else
        cvs up -Ad $1
    fi
}
#### CVS LOG
:log(){
    cvs log $1 | less
}
#### CVS DIFF
:diff(){
    if(($#==3)); then
        cvs diff -u -r $2 -r $3 $1 | less
    elif(($#==2)); then
        cvs diff -u -r $2 $1 | less
    else
        cvs diff -u $1 | less
    fi
}
#### CVS ANNOTATE
:anno(){
    if(($#==1)); then
        cvs annotate $1
    else
        cvs annotate
    fi
}
#### UPLOAD TO APACHE fixme more generic
:up2ap(){
    lib_uploaded=0
    if(($#!=0)); then
	for FN in $*
	do
            cp -f "/home/kukartsev/$FN" "/usr/local/$FN"
	    if [[ "$FN" == lib/* ]]; then
		lib_uploaded=1
	    else
		chmod +x "/usr/local/$FN"
	    fi
	done
	if (($lib_uploaded)); then
	    /etc/init.d/httpd restart
	fi
    else
	echo "no files to upload found"
    fi
}
#### UNIQ
:uniq(){
    if(($#==1)); then
	sort -n $1 | uniq --repeat
    else
        echo 'Usage: :uniq filename'
    fi
}
#### TAIL fixme perl vs sed fixme
:tail(){
    if(($#==2)); then
        tail -n 200 -f $1 | perl -pe 's/ZZZ|'"$2"'/\e[1;31;41m$&\e[0m/g' | perl -pe 's/^\[D\].*//g' | perl -pe 's/^.*My\:\:Conference.*$//g'
    elif(($#==1)); then
        tail -n 200 -f $1 | perl -pe 's/ZZZ/\e[1;31;41m$&\e[0m/g' | perl -pe 's/^\[D\].*//g' | perl -pe 's/^.*My\:\:Conference.*$//g'
    else
        echo 'no file found'
    fi
}
#### SPLITs file to requested number of chunks by lines 
:split(){
    if(($#==2)); then
	count_lines=$(sed -n '$=' $1)
	split_lines=$(($count_lines/$2))
	last_lines=$(($count_lines%$2+$split_lines))
        cnt=1;from=1
        while [ $cnt -ne $2 ]; do
	    to=$(($cnt*$split_lines))
	    sed -n "$from","$to"p $1 > "$1_$cnt"
            let cnt=cnt+1
	    let from=to+1
        done
	to=$(($from+$last_lines))
	sed -n "$from","$to"p $1 > "$1_$cnt"
    else
        echo 'usage file num_lines'
    fi
}
#### Curl: from dos to unix, save file
:curl(){
    if(($#==1)); then
        file_name=$(perl -e 'my $str = "'$1'"; $str =~ /^.*\/([^\?\#]*)\??.*$/; print $1 ? $1 : "" ;')
        curl $1 | tr -d '\15\32' > $file_name
    else
        echo 'usage http://file'
    fi
}