#!/bin/bash
set -e

KUMATUXVERSION=1.0

#--------------------------
# Name: usage
# Purpose: prints usage message
#--------------------------

function usage {
    echo "usage: kumatux-setup.sh -r <path to kumatux root> [-l <path to kumatux links>] [-h] [-v]" >&2
    if [[ ${1:-} = '' ]]
    then
        echo "(use -h for help)" >&2
    else
        echo "where:
    -h prints this help
    -l kumatux links refer to the links, that will be created for each program's files,
       in a kumatux centralized links' repository;
       if not specified, links will be placed in a 10 directory in the kumatux root;
    -r kumatux root refers to the directory which will hold special numeric links to
       kumatux links. These root links will be appended to PATH etc.
    -v prints kumatux version
" >&2
    fi
}  # end of function usage

## Process commandline options
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
	    ;;
	v)  
	    echo "Kumatux version $KUMATUXVERSION"
	    echo "© kumatux.org 2009"
	    ;;
        * )
            usage
            \exit 1
    esac
done

## Test for extra arguments
shift $(( $OPTIND-1 ))
if [[ $* != '' ]]; then
    usage
    \exit 1
fi

## Test for any mandatory options not set
if [[ ${KUMATUXSETUPROOT:-} = '' ]]; then
    usage
    \exit 1
fi

if [[ ${KUMATUXSETUPLINKS:-} = '' ]]; then
    KUMATUXSETUPLINKS=$KUMATUXSETUPROOT/10
fi

# get the absolute path of the executable
SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH=$SELF_PATH/$(basename -- "$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=$(dirname -- "$SELF_PATH")
    SYM=$(readlink $SELF_PATH)
    SELF_PATH=$(cd $DIR && cd $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
done

KUMATUXSETUPBIN=$(dirname -- "$SELF_PATH")/bin

## Record the paths in users profile
if [ "`grep "^export KUMATUXBIN" ~/.profile`" ]
then
    sed -i '/^export KUMATUXBIN/d' ~/.profile
fi
echo "export KUMATUXBIN=$KUMATUXSETUPBIN" >> ~/.profile

if [ "`grep "^export KUMATUXLINKS" ~/.profile`" ]
then
    sed -i '/^export KUMATUXLINKS/d' ~/.profile
fi
echo "export KUMATUXLINKS=$KUMATUXSETUPLINKS" >> ~/.profile

if [ "`grep "^export KUMATUXROOT" ~/.profile`" ]
then
    sed -i '/^export KUMATUXROOT/d' ~/.profile
fi
echo "export KUMATUXROOT=$KUMATUXSETUPROOT" >> ~/.profile

if [ "`grep "optexport" ~/.profile`" ]
then
    sed -i '/optexport/d' ~/.profile
fi
echo '. $KUMATUXBIN/optexport' >> ~/.profile

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