#!/bin/bash
set -e

KUMATUXVERSION=1.5
SYSTEMPATH=$PATH
PATH=`echo $0 | sed 's/\/kumatux-setup.sh//'`/bin:$PATH

#--------------------------
# Name: usage
# Purpose: prints usage message
#--------------------------
function usage {
    echo "usage: kumatux.org-setup -r <path to kumatux.org root> [-l <path to kumatux.org links>] [-h] [-v]" >&2
    if [[ ${1:-} = '' ]]
    then
        echo "(use -h for help)" >&2
    else
        echo "where:
    -h prints this help
    -l kumatux.org links refer to the links, that will be created for each program\'s files,
       in a kumatux.org centralized links\'s repository.
       If not specified, \`~/kumatux.org-links\' will be used;
    -r kumatux.org root refers to the directory which will hold special numeric links to
       kumatux.org links. These root links will be appended to PATH etc.
       If not specified, \`~/kumatux.org-root\' will be used;
    -t kumatux.org test links refer to the links, that will be created for testing each
       program in a clean kumatux.org environment.
       If not specified, \`~/kumatux.org-test/kumatux.org-links\' will be used;
    -u kumatux.org test root refers to the directory, that will be created for testing each
       program in a clean kumatux.org environment.
       If not specified, \`~/kumatux.org-test/kumatux.org-root\' will be used;
    -v prints kumatux.org version
" >&2
    fi
}  # end of function usage

## Process commandline kumatux.org-ions
args="${*:-}"
while getopts hl:r:v opt 2>/dev/null
do
    case $opt in
	h)
            usage -v
            \exit 0
            ;;
	l)  
	    KUMATUXSETUPLINKS=$OPTARG
	    ;;
	r)  
	    KUMATUXSETUPROOT=$OPTARG
	    ;;
	t)
	    KUMATUXSETUPTESTLINKS=$OPTARG
	    ;;
	u)
	    KUMATUXSETUPTESTROOT=$OPTARG
	    ;;
	v)  
	    echo "kumatux.org version $KUMATUXVERSION"
	    echo "© kumatux.org 2010"
            \exit 0
	    ;;
        * )
            usage
            \exit 1
    esac
done

## Test for extra arguments
shift $(( $OPTIND-1 ))

## Test for any mandatory options not set
if [[ ${KUMATUXSETUPROOT:-} = '' ]]; then
    KUMATUXSETUPROOT=~/kumatux.org-root
fi
if [[ ${KUMATUXSETUPLINKS:-} = '' ]]; then
    KUMATUXSETUPLINKS=~/kumatux.org-links
fi
if [[ ${KUMATUXSETUPTESTROOT:-} = '' ]]; then
    KUMATUXSETUPTESTROOT=~/kumatux.org-test/kumatux.org-root
fi
if [[ ${KUMATUXSETUPTESTLINKS:-} = '' ]]; then
    KUMATUXSETUPTESTLINKS=~/kumatux.org-test/kumatux.org-links
fi

# get the absolute path of the executable
SELF_PATH=$(cd -P -- "$($KUMATUXDIRNAME -- "$0")" && pwd -P) && SELF_PATH=$SELF_PATH/$($KUMATUXBASENAME -- "$0")
# resolve symlinks
while [ -h $SELF_PATH ]; do
    # 1) cd to directory of the symlink
    # 2) cd to the directory of where the symlink points
    # 3) get the pwd
    # 4) append the basename
    DIR=$($KUMATUXDIRNAME -- "$SELF_PATH")
    SYM=$(readlink $SELF_PATH)
    SELF_PATH=$(cd $DIR && cd $($KUMATUXDIRNAME -- "$SYM") && pwd)/$($KUMATUXBASENAME -- "$SYM")
done

KUMATUXSETUPBIN=`echo $($KUMATUXDIRNAME -- "$SELF_PATH") | sed 's/\/setup.sh//'`
PATH=$KUMATUXSETUPBIN:$SYSTEMPATH

echo "Installing kumatux.org version $KUMATUXVERSION..."
echo "kumatux.org links        =>   $KUMATUXSETUPLINKS"
echo "kumatux.org root         =>   $KUMATUXSETUPROOT"
echo "kumatux.org test links   =>   $KUMATUXSETUPTESTLINKS"
echo "kumatux.org test root    =>   $KUMATUXSETUPTESTROOT"

touch ~/.profile
sed '/KUMATUX/d' -i ~/.profile
## Record the paths in users profile
echo 'export KUMATUX_ORIG_PATH=$PATH' >> ~/.profile
echo 'export KUMATUX_ORIG_LD_PRELOAD=$LD_PRELOAD' >> ~/.profile
echo 'export KUMATUX_ORIG_LD_LIBRARY_PATH=$LD_LIBRARY_PATH' >> ~/.profile
echo 'export KUMATUX_ORIG_PERL5LIB=$PERL5LIB' >> ~/.profile
echo 'export KUMATUX_ORIG_PYTHONPATH=$PYTHONPATH' >> ~/.profile
echo "export KUMATUXBIN=$KUMATUXSETUPBIN" >> ~/.profile
echo "export KUMATUXLINKS=$KUMATUXSETUPLINKS" >> ~/.profile
echo "export KUMATUXROOT=$KUMATUXSETUPROOT" >> ~/.profile
echo "export KUMATUXTESTLINKS=$KUMATUXSETUPTESTLINKS" >> ~/.profile
echo "export KUMATUXTESTROOT=$KUMATUXSETUPTESTROOT" >> ~/.profile

echo '. $KUMATUXBIN/kumatux.org-export' >> ~/.profile

## So that it gets exported under KDE also, since it doesn't source ~/.profile
if [[ -d ~/kde4 ]]
then
    mkdir -p ~/kde4/env/
    ln -s ~/.profile ~/kde4/env/dotprofile.sh 2>/dev/null
fi

KUMATUXSETUPBIN=$KUMATUXSETUPBIN KUMATUXSETUPLINKS=$KUMATUXSETUPLINKS KUMATUXSETUPROOT=$KUMATUXSETUPROOT KUMATUXSETUPTESTLINKS=$KUMATUXSETUPTESTLINKS KUMATUXSETUPTESTROOT=$KUMATUXSETUPTESTROOT $KUMATUXSETUPBIN/setup.sh/directories.sh
KUMATUXSETUPBIN=$KUMATUXSETUPBIN KUMATUXSETUPLINKS=$KUMATUXSETUPLINKS KUMATUXSETUPROOT=$KUMATUXSETUPROOT KUMATUXSETUPTESTLINKS=$KUMATUXSETUPTESTLINKS KUMATUXSETUPTESTROOT=$KUMATUXSETUPTESTROOT $KUMATUXSETUPBIN/setup.sh/etc.sh

echo "kumatux.org set up."
