#!/bin/bash
#===============================================================================
#
#          FILE:  setup_profile.sh
# 
#         USAGE:  ./setup_profile.sh 
# 
#   DESCRIPTION:  This is run from .profile and is a way to keep the settings
#                 and scripts together with the other scripts by calling it here
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  John Ryland (JR), (jryland@xiaofrog.com)
#       COMPANY:  InvertedLogic
#       VERSION:  1.0
#       CREATED:  01/03/2008 00:46:55 JST
#      REVISION:  ---
#===============================================================================

export USER_NAME="John Ryland"
export USER_REF="JR"
export USER_EMAIL="(jryland@invertedlogic.com)"
export USER_COMPANY="InvertedLogic"
export PS1='\u@\h:$PWD \$ '
export SVN_EDITOR=vi
export PATH=~/Code/Scripts/:~/usr/local/bin/:$PATH
export BASEDIR=~/Code/research/signals-slots

# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
# umask 002


# JR - Make safe versions of rm, rmdir, unlink, mv etc
##############################################################################
#    Make our environment safer from overwriting/deleting files by mistake   #
##############################################################################

# Used by safe versions of rm, rmdir and unlink
# Add to this function checks for any directories you don't want deleted so easily by mistake
function check_args()
{
    while [ $# -ne 0 ]
    do
        [ "$1" == "~" ] && return 0
        [ "$1" == "/" ] && return 0
        [ "$1" == "/Users" ] && return 0
        [ "$1" == "/Users/" ] && return 0
        [ "$1" == "/Users/jryland" ] && return 0
        [ "$1" == "/Users/jryland/" ] && return 0
        [ "$1" == "/Users/jryland/Code" ] && return 0
        [ "$1" == "/Users/jryland/Code/" ] && return 0
        [ "$1" == "/home" ] && return 0
        [ "$1" == "/home/" ] && return 0
        [ "$1" == "/home/jryland" ] && return 0
        [ "$1" == "/home/jryland/" ] && return 0
        [ "$1" == "/test/test" ] && return 0
        shift
    done
    return 1
}


#
# I don't think I need to explain how the rm command can be dangerous
#
# Pretty obvious that "rm -rf /" can be a pretty devistating command to execute
# Or "rm -rf ~" is not very nice either. Guess what, before making this safe
# function, I had by mistake executed these 2 example commands before.
# Real dumb I know, but it really can happen.
#
# Senerio might be working on a
# remote system such as an embedded development device thats being tested
# and need a fresh home directory for running a test, wiping out the home
# directory on the target is easy with "rm -rf ~" if nothing valuable there.
# I've worked a lot on embedded targets with a telnet session from my PC.
# But if accidently type it in to the wrong terminal and disaster. When it
# most recently happened to me, the senario was another one. It was late at
# night and a little tired, and I had some poo files I was cleaning up after
# executing some commands that left behind some temporary files and also I
# saw a file named "~". When cleaning up these files I typed:
#
# "rm -rf blah.tmp ~ foo"
#
# I hit enter and a moment later when I thought that this is taking a bit
# longer than I expected, I realised, "oh crap"! and immediately hit ctrl-c.
# I was pretty lucky, mainly some .foo directories with thumbnails and cached
# data and unimportant stuff was erased, but this made me realise really how
# dangerous some commands are and I didn't want to make this same mistake
# again if it could be prevented easily which is what these functions are for.
#
# Yes, you should have backups too, but it doesn't hurt to have
# a safer working environment.
#
function rm()
{
    check_args "$@"
    if [ $? -eq 1 ]
    then
        /bin/rm "$@"
    else
        echo "Bailing out - Contains an unsafe argument"
    fi
}


function unlink()
{
    check_args "$@"
    if [ $? -eq 1 ]
    then
        /bin/unlink "$@"
    else
        echo "Bailing out - Contains an unsafe argument"
    fi
}


function rmdir()
{
    check_args "$@"
    if [ $? -eq 1 ]
    then
        /bin/rmdir "$@"
    else
        echo "Bailing out - Contains an unsafe argument"
    fi
}


function check_not_overwrite_file_in_directory()
{
    dir=$1
    shift
    while [ $# -ne 1 ]
    do
        # XXX need to check $1 doesn't start with '-'
        [ -f "$dir/`basename $1`" ] && return 0
        shift
    done
    return 1
}


#
#    mv can be quite dangerous, imagine you type "mv *" and accidently hit
#    enter before entering the directory to put the files to. It will mv
#    all the files in to the last file name in the list which will mean
#    that all files in the directory will be lost except the 2nd last one.
#    Or just as bad, "mv * somename" where somename is the name of a
#    directory but is mis-spelt, then all files except the last in the
#    directory will be lost.
#
#    Even you think you got it right, and the last argument is a directory
#    name, what happens when you mv a file with a name that already exists
#    in the destination directory? Yep, you guest it, puff, gone.
#
#    And nearly always if moving multiple files and last argument is a filename
#    or doesn't exist then it probably is not what is intented unless it
#    is being used as a fancy way to delete some files while renaming another
#    file at the same time, but I think that's a really really bad side
#    effect to take advantage of and a pretty dumb idea in general.
#
function mv()
{
    last="echo \$$#"
    last=`eval $last`

    if [ -d "$last" ]
    then
        check_not_overwrite_file_in_directory "$last" "$@"
        if [ $? -eq 1 ]
        then
            /bin/mv "$@"
        else
            echo "Safe-mv by John Ryland"
            echo "Looks like one of the files will overwrite a file in the destination directory"
            echo "Bailing out just in case to save your arse \(files\)"
        fi
        return
    fi

    if [ $# -gt 2 ]
    then
        echo "Safe-mv by John Ryland"
        echo "Looks like you are wanting to move several files, but"
        echo "NO NO NO... last argument is NOT a directory!!!"
        echo "Bailing out just in case to save your arse \(files\)"
        echo "Send your donations to .... \$-)"
        return
    else
        if [ -f "$last" ]
        then
            echo "Safe-mv by John Ryland"
            echo "NO NO NO... new name is an existing file, you will overwrite it!!!"
            echo "I suggest better to explicity delete the other file first."
            echo "Bailing out just in case to save your arse \(files\)"
            echo "Send your donations to .... \$-)"
            return
        else
            /bin/mv "$@"
            return
        fi
    fi
}


#
#    Similar problems occur for the cp command as mv, easily overwriting destination,
#    if last argument is directory and file exists in destination directory,
#    even if not explicitly given, overwrites the file of the same name as the
#    source acting the same as the mv command. This might catch you out at
#    some time, but can sometimes be useful. Perhaps better to try to be save
#    than sorry.
#
function cp()
{
    last="echo \$$#"
    last=`eval $last`

    if [ -d "$last" ]
    then
        check_not_overwrite_file_in_directory "$last" "$@"
        if [ $? -eq 1 ]
        then
            /bin/cp "$@"
        else
            echo "Safe-cp by John Ryland"
            echo "Looks like one of the files will overwrite a file in the destination directory"
            echo "Bailing out just in case to save your arse \(files\)"
        fi
        return
    fi

    if [ -f "$last" ]
    then
        echo "Safe-cp by John Ryland"
        echo "NO NO NO... destination name is an existing file, you will overwrite it!!!"
        echo "I suggest better to explicity delete the other file first."
        echo "Bailing out just in case to save your arse \(files\)"
        echo "Send your donations to .... \$-)"
        return
    else
        /bin/cp "$@"
        return
    fi
}


