#!/bin/bash


log_failure_msg() {
    echo $@
}

control_path () {
    # job_id=`echo "$2" | sed 's/\(.\{9\}\)/\1\//g' | sed 's/\/$//'`
    job_id=`echo "$2" | sed -e 's#\(.\{3\}\)#\1/#3' -e 's#\(.\{3\}\)#\1/#2' -e 's#\(.\{3\}\)#\1/#1' -e 's#$#/#'`
    path="$1/jobs/${job_id}/$3"
    echo "$path"
}

update_control_dir() {
    controldir="$1"
    # convert files stored directly in control dir
    find "$controldir" -maxdepth 1 -regex '.*/job\.[0-9a-zA-Z]*\.[^.]*' -printf '%f\n' | \
    while true; do
        read file
        if [ $? -ne 0 ]; then break; fi
        id=`echo "$file" | sed -e 's/^job\.//' -e 's/\.[^.]*$//'`
        suffix=`echo "$file" | sed -e 's/^job\.//' -e 's/^[^.]*\.//'`
        newfile=$(control_path "$controldir" "$id" "$suffix")
        newpath=`dirname "$newfile"`
        mkdir -p "$newpath"
        if [ $? -ne 0 ]; then return 1; fi
        mv "$controldir/$file" "$newfile"
        if [ $? -ne 0 ]; then return 1; fi
    done
    # convert status files
    for subdir in accepting finished processing restarting; do
        find "$controldir/$subdir" -maxdepth 1  -regex '.*/job\.[0-9a-zA-Z]*\.[^.]*' -printf '%f\n' | \
        while true; do
            read file
            if [ $? -ne 0 ]; then break; fi
            newfile=`echo "$file" | sed -e 's/^job\.//'`
            mv "$controldir/$subdir/$file" "$controldir/$subdir/$newfile"
            if [ $? -ne 0 ]; then return 1; fi
        done
    done
}

readconfigvar() {
    fname="$1"
    optname="$2"
    blocks=""
    while [ ! -z "$3" ] ; do
	blocks="$blocks -b $3"
	shift
    done
    value=`$ARC_LOCATION/libexec/arc/arcconfig-parser --runconfig "$fname" --load $blocks -o "$optname" 2>/dev/null`
    if [ $? -eq 0 ] ; then
	echo "$value"
	exit 0
    else
	exit 1
    fi
}


if [ "$1" = '-h' ]; then
    echo "Usage: update-controldir [path to controldir]"
    echo " Used env. variables: ARC_LOCATION, ARC_CONFIG."
    exit 0
fi

# ARC_LOCATION
ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    log_failure_msg "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi
export ARC_LOCATION

if [ -z "$1" ]; then
    # ARC_CONFIG
    if [ "x$ARC_CONFIG" = "x" ]; then
        if [ -r $ARC_LOCATION/etc/arc.conf ]; then
            ARC_CONFIG=$ARC_LOCATION/etc/arc.conf
        elif [ -r /etc/arc.conf ]; then
            ARC_CONFIG=/etc/arc.conf
        fi
    fi
    if [ -z "$ARC_CONFIG" ] ;  then
        log_failure_msg "Missing A-REX configuration file"
        exit 1
    fi

    CONTROLDIR=`readconfigvar "$ARC_CONFIG" controldir arex`
    if [ -z "$CONTROLDIR" ] ;  then
        log_failure_msg "Missing controldir in A-REX configuration"
        exit 1
    fi
else
    CONTROLDIR="$1"
fi

if [ ! -d "$CONTROLDIR" ]; then
    log_failure_msg "The controldir '$CONTROLDIR' does not exist or is not accessible."
    exit 1
fi

# check special mark indicating new version of control dir
if [ ! -f "$CONTROLDIR/version2" ] ; then
    echo "Updating A-REX control dir..."
    update_control_dir "$CONTROLDIR"
    if [ $? -ne 0 ]; then
        log_failure_msg "Failed to update A-REX control dir"
        exit 1
    fi
    touch "$CONTROLDIR/version2"
else
    echo "A-REX control dir '$CONTROLDIR' is already updated."
    echo "Remove file named 'version2' from controldir to force update."
fi

exit 0

