#!/bin/bash
#set defaults
if [ -z "$EAMDdirectory" ]; then
	EAMDdirectory="/EAMD.ucp/"
	debug.log "EAMDdirectory: $EAMDdirectory"
fi

if [ -z "$defaultWorkspace" ]; then
    defaultWorkspace=$(cd .;pwd)
	debug.log "defaultWorkspace: $defaultWorkspace"
fi

if [ -z "$componentDir" ]; then
	componentDir="Components/"
	debug.log "componentDir: $componentDir"
fi

if [ -z "$scenarioDir" ]; then
	scenarioDir="Scenarios/"
	debug.log "scenarioDir: $scenarioDir"
fi

if [ -z "$defaultVersion" ]; then
	defaultVersion="0.1.0"
	debug.log "defaultVersion: $defaultVersion"
fi

#done set defaults


create() {
    shift
    debug.log " function create($*)"

    while [ -n "$1" ]; do
        let i++
        debug.log "Parameter($i): $1"

        case $1 in
            component)
                debug.log "...create component" 
                createComponent $2 $3 $4
                exit 0
                ;;
            weBean)
                debug.log "create WeBean"
                createWeBean $2 $3 $4
                exit 0
                ;;
            version)
                debug.log "create version"
                createVersion $2 $3 $4
                exit 0
                ;;
            descriptor)
                debug.log "create descriptor"
                createDescriptor $2 $3 $4
                exit 0
                ;;
            *)
                err.log "don\'t know $1"
                console.log "You can create: component | descriptor | weBean | version"
                ;;
       esac
        
        shift
    done
}

createComponent() {
    component=$1
    version=$2

    if [ -z "$version" ]; then
        version=$defaultVersion
    fi

    createComponentNamescpaces $component

    createDir $componentPath/$componentName
    createDir $componentPath/$componentName/$version

    createFile $componentPath/$componentName/$version/$componentName.component.xml

    console.log "$componentPath:"
    ls -al $componentPath
    console.log "$componentPath/$componentName"
    ls -al $componentPath/$componentName
    console.log "$componentPath/$componentName/$version"
    ls -al $componentPath/$componentName/$version

}

createComponentNamescpaces() {
    debug.log " function getComponentDetails($component)"

    init

    path=$EAMDdirectory$componentDir
    componentPath=$path
    last=""
    for current in ${component//./ }; do 
        componentPath=$componentPath$last
        debug.log "checking namespace: $current"

        path=$path/$current
        debug.log "checking path: $path"

       #createDir $path

        last="$current/"
    done

    componentName=$current
    debug.log "  Result:"
    debug.log "Component: $component   Version: $version"
    debug.log "Path: $componentPath   Name: $componentName"

}

createVersion() {
    component=$1
    newVersion=$2
    oldVersion=$3
    verion=$newVersion
    debug.log " function createVersion(component $component, newVersion: $newVersion, oldVersion $oldVersion)"
 
    createComponentNamescpaces $component

    debug.log " function createVersion(component $component, newVersion: $newVersion, oldVersion $oldVersion)"

    current=$componentPath$componentName/$oldVersion
    if [ ! -d $current ]; then
        err.log "$current does not exists"
        exit -1
    fi

    current=$componentPath$componentName/$newVersion
    if [ ! -d $current ]; then
        cp -R $componentPath$componentName/$oldVersion $current
        console.log "$current:"
        ls -al $current
    else
        err.log "$current aready exists"
        exit -1
    fi

}


createWeBean() {
    component=$1
    version=$2
    weBeanName=$3
    debug.log " function createWeBean(component $component, weBeanName: $weBeanName)"
    if [ -z "$version" ]; then
        version=$defaultVersion
    fi

    createComponentNamescpaces $component

    if [ -z "$weBeanName" ]; then
        weBeanName=$componentName
    fi
    debug.log "Path: $componentPath   Name: $componentName     weBeanName: $weBeanName"

    createPath $componentPath$componentName/$version/src/html/weBeans
    createFile $componentPath$componentName/$version/src/html/weBeans/$weBeanName.weBean.html

    createPath $componentPath$componentName/$version/src/js
    createFile $componentPath$componentName/$version/src/js/$weBeanName.class.js
 
    tree $componentPath$componentName/$version

}

createDescriptor() {
    current=$1
    if [ -z "$current" ]; then
        current=.
    fi

    debug.log " function createDescriptor(path $current)"

    createFile $current/Default.component.xml
    update
}



init() {
    debug.log " function init($1 $2 $3)"
    if [ -d $defaultWorkspace ]; then
        workspace=""
        for current in ${defaultWorkspace//// }; do 
            debug.log "checking namespace: $current"
            debug.log "checking path: $workspace/$current"
            workspace=$workspace/$current

            if [ "$current" = "EAMD.ucp" ]; then
                break
            fi
        done
        debug.log "EAMD.ucp Workspace = $workspace"
    else
        err.log "current Workspace does not exist: $defaultWorkspace"
        exit -1
    fi


    if [ ! -d $EAMDdirectory ]; then
        err.log "EAMD.ucp Directory does not exist: $EAMDdirectory"
        if [ ! -L $EAMDdirectory ]; then
            err.log "current Link does not exist: $EAMDdirectory"
            console.log "create link $EAMDdirectory --> $workspace"
            sudo ln -sf  $workspace $EAMDdirectory
        fi
    else
        debug.log "EAMD.ucp Directory does exist: $EAMDdirectory"
    fi
}

createPath() {
    debug.log " function createPath($1)"

    path=""
    for current in ${1//// }; do 
        path=$path/$current
        debug.log "checking path: $path"

        createDir $path
    done
}


createDir() {
    current=$1
    if [ ! -d $current ]; then
        debug.log "$current does not exist: creating it..."   
        mkdir $current
    fi
}

createFile() {
    current=$1
    if [ ! -w $current ]; then
        debug.log "$current does not exist: creating it..."   
        touch $current
    fi
}

update() {
    debug.log " function update($1 $2 $3)"

    findComponent $1 $2 $3
    getComponentDetails $componentPath

    if [ -w "$componentPath/Default.component.xml" ]; then
        console.log found
        mv $componentPath/Default.component.xml $componentPath/$component.component.xml
    fi
    

    createWeBean $namespace $version
    exit 0;

}

findComponent() {
    debug.log " function findComponent($1 $2 $3)"

    filePattern="*.component.xml"
    path=.

    current=$path
    dir=$path

    while [ "$dir" != "EAMD.ucp" ] && [ "$dir" != "/" ]; do
        debug.log "$dir  --> $current"
        current=$(cd $current; pwd)

        dir=$(basename $current)
        debug.log "$dir  --> $current:    $current/$filePattern"
        
        if ls $current/$filePattern 1> /dev/null 2>&1; then
            break
        else
            debug.log "files do not exist"
        fi
        current=$current/..
    done

    if [ "$dir" == "EAMD.ucp" ] || [ "$dir" == "/" ]; then
        err.log "No component found. Please enter a Version directory of an existing component"
        exit -1
    fi

    componentPath=$current
    console.log "Found Component in: $componentPath"
}


getComponentDetails() {
    debug.log " function getComponentDetails(componentPath $1 $2 $3)"
    curren=$1

    filepath=$current/../..
    component=${filepath%/*}
#    component=${component##*/}
    component=$(cd $component; pwd)
    component=$(basename $component)
    debug.log "Component: $component    Path:$componentPath"

    filepath=$current/..
    version=${filepath%/*}
#    version=${version##*/}
    version=$(cd $version; pwd)
    version=$(basename $version)
    debug.log "Version: $version"

    workspace=""
    workspaceFound=false
    namespace=""
    for current in ${componentPath//// }; do 
        debug.log "checking namespace: $current"
        debug.log "checking path: $workspace/$current"


        if $workspaceFound ; then
            if [ "$current" = "Components" ]; then
                namespace=""
            else
                if [ "$namespace" = "" ]; then
                    namespace=$current
                else
                    namespace=$namespace.$current
                fi
            fi
        else
            workspace=$workspace/$current
        fi

        if [ "$current" = "EAMD.ucp" ]; then
            workspaceFound=true
        fi

        if [ "$current" = "$component" ]; then
            break
        fi
    done
    console.log "Component Path.....: = $componentPath"
    console.log "EAMD.ucp  Workspace: = $workspace"
    console.log "Component Namespace: = $namespace"
    console.log "Component Name.....: = $component"
    console.log "Component Version..: = $version"
    
}

if [ -z "$1" ]; then
	echo "usage: $0 comands
		commands are
		    create:
                create component some.component.Name 0.1.0        
                create version   some.component.Name 0.2.0   0.1.0        
                create weBean    some.component.Name <?weBeanName=Name>
            init:  creates the /EAMD.ucp link 
"
exit -1
fi

i=0

while [ -n "$1" ]; do
    let i++
    debug.log "Parameter($i): $1"

    case $1 in
        create)
            debug.log "...create..."
            create $*
            ;;
        init)
            debug.log "...init..."
            init $*
            ;;
        update)
            debug.log "update component"
            update
            ;;
        *)
            debug.log "dont know $1"
            ;;
    esac
    
    case $i in
        1)
            debug.log "setting var1 to $1"
            var1=$1
            ;;
        2)
            debug.log "setting var2 to $1"
            var2=$1
            ;;
        3)
            debug.log "setting var3 to $1"
            var3=$1
            ;;
    esac


    shift
done

exit 0
