#!/bin/bash

start() {
    debug.log "...started"
}

stop() {
    debug.log "...stooped"
}

if [ ! -r bin/start ]; then
    echo "This is not a Structr Directory"
    exit -1
fi


if [ -z "$1" ]; then
	echo "usage: $0 <appName> <?defaultAppDirectory=../structrApp> <?defaultAppsWorkspaceDirectory=../structrAppWorkspace>
        Has to be executed within a structr directory.

        Creates symbolic links for directories that hold the state of a structr instance
        
"
    exit -1
fi

i=0

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

    case $i in
        1)
            debug.log "setting appName to $1"
            appName=$1
            ;;
        2)
            debug.log "setting defaultAppDirectory to $1"
            defaultAppDirectory=$1
            ;;
        3)
            debug.log "setting defaultAppsWorkspaceDirectory to $1"
            defaultAppsWorkspaceDirectory=$1
            ;;
    esac

    case $1 in
        start)
            debug.log starting
            start
            ;;
        stop)
            debug.log stoping
            stop
            ;;
        restart)
            debug.log restarting
            stop
            start
            ;;
    esac
    shift
done


#set defaults
if [ -z "$defaultAppDirectory" ]; then
	defaultAppDirectory="../structrApp/"
	debug.log "defaultAppDirectory: $defaultAppDirectory"
fi

if [ -z "$defaultAppsWorkspaceDirectory" ]; then
	defaultAppsWorkspaceDirectory="../../Workspaces/structrAppWorkspace/"
	debug.log "defaultAppsWorkspaceDirectory: $defaultAppsWorkspaceDirectory"
fi
#done set defaults

#check if default directories exist and create them
checkDirs=$defaultAppDirectory,$defaultAppsWorkspaceDirectory,$defaultAppsWorkspaceDirectory$appName
for currentDir in ${checkDirs//,/ };	do
    debug.log "checking directory: $currentDir"
    if [ ! -d $currentDir ]; then
        err.log "currentDir does not exist: $currentDir"
        mkdir $currentDir
        console.log "created: $currentDir"
    fi
done




# care for the structr-server directory
checkLinks=./db,./files,./logs,./sessions,./snapshots,./layouts,./scripts
for current in ${checkLinks//,/ };	do
    debug.log "checking link: $current"
    # check if the structr-server subdirectory is a link
    targetDir=$defaultAppDirectory/$current
    if [ ! -L $current ]; then
        err.log "current Link does not exist: $current"
	# not a link: so move the existing directory to the $defaultAppsWorkspaceDirectory in the app directory
	
	#check the workspace
        targetDir=$defaultAppsWorkspaceDirectory$appName/$current
	#is current an existing directory in the workspace
        if [ -d $targetDir ]; then
            err.log "  targetDir already exist: $targetDir"
            err.log "Did not remove the directory. Please remove it manually to be sure to not delete important data!"
            err.log "rm -Rf $targetDir"
	    exit -1
        else
            debug.log "initializing WorkspaceApp $appName wit $current"
	    debug.log "move $current to $targetDir"
            mv $current $targetDir
            # now the $current is moved to the AppWorkspace, so we need to create a link in the server directory
            ln -sf $defaultAppDirectory$current $current
        fi
    else 
        debug.log "current Link $current exists and points to"
	# enforce the link to be to the right app
        targetDir=$defaultAppDirectory$current
        debug.log "`ls -l $targetDir`"
	# remove
        rm $targetDir
	# and recreate to the $appName
        ln -sf $defaultAppsWorkspaceDirectory$appName/$current $targetDir
    fi

    # care for the structrApp directory
    targetDir=$defaultAppDirectory$current
    # is there a link to the app
    if [ -L $targetDir ]; then
       debug.log "  all Fine: targetDir is a Link: $targetDir"
# there is a link, but we do not know the link target is correct here. 
# uncomment the next two lines to enfore a correct link to be regenerated
#      err.log "rm $targetDir"
       rm $targetDir
       ln -sf $defaultAppsWorkspaceDirectory$appName/$current $targetDir
    else 
       err.log "no link in $defaultAppDirectory"
       console.log " creating link $targetDir --> $defaultAppsWorkspaceDirectory$appName/$current: "
       ln -sf $defaultAppsWorkspaceDirectory$appName/$current $targetDir
    fi  
done

ls -l $defaultAppDirectory

exit 0
