# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
##@require instinc/prefixes
##@require instinc/wobase
##@require instinc/wofunctions

function wotag_display_help() {
    echo "$scriptname: attacher une information de version à une application ou un framework

USAGE
    $scriptname <app.woa|fw.framework>"
}

function wotag() {
    local action=tag description version releaseDate developmentSnapshot message
    local tagdir="$1" srcdir bundir srvresdir

    local end_of_options
##@include sysinc/begingetopt
    -h|--help)
        wotag_display_help
        exit 0
        ;;
    -c|--check-tagged)
        action=check
        ;;
    -s|--show)
        action=show
        ;;
    -v|--version)
        # spécifier le numéro de version, ou - s'il n'y a pas de numéro de version
        shift
        version="$1"
        ;;
    -d|--date)
        # spécifier la date de release
        shift
        releaseDate="$1"
        ;;
    -m|--desc)
        # spécifier la description de l'application ou du framework
        shift
        description="$1"
        ;;
    --message)
        shift
        message="$1"
        ;;
##@include sysinc/endgetopt

    if [ -z "$tagdir" ]; then
        die "Il faut spécifier l'application ou le framework à tagger"
    fi
    tagdir="$(abspath "$1")"
    if is_wosrcdir "$tagdir"; then
        srcdir="$tagdir"
        set_wobindir "$srcdir" bundir desc
        
    elif is_woappdir "$tagdir"; then
        bundir="$tagdir"
        
    elif is_wofwdir "$tagdir"; then
        bundir="$tagdir"
        
    else
        [ "$action" == "show" ] && return 1
        die "Le bundle doit être un framework ou une application"
    fi
    
    bundir="${bundir%/}"
    if endswith "$bundir" .woa; then
        local srvresdir="$APP_SRVRESDIR"
    elif endswith "$bundir" .framework; then
        local srvresdir="$FW_SRVRESDIR"
    fi
    local infosfile="$(get_jawotools_properties_path "$bundir")"
    local infosfile2="$bundir/jawotools.properties"
    local versionfile="$(get_version_txt_path "$bundir/$srvresdir")"
    
    if [ "$action" == "check" ]; then
        if [ -f "$infosfile" ]; then
            # vérifier la présence de description, version, releaseDate, developmentSnapshot
            read_jawotools_properties "$infosfile"
            if [ "$description" != "--NOT-SET--" -a "$version" != "--NOT-SET--" -a "$releaseDate" != "--NOT-SET--" -a "$developmentSnapshot" != "--NOT-SET--" ]; then
                return 0
            fi
        fi
        return 1
        
    elif [ "$action" == "show" ]; then
        if [ -f "$infosfile" ]; then
            read_jawotools_properties "$infosfile"
            [ "$description" == "--NOT-SET--" ] && description=
            [ "$version" == "--NOT-SET--" ] && version=
            [ "$releaseDate" == "--NOT-SET--" ] && releaseDate=
            [ "$developmentSnapshot" == "--NOT-SET--" ] && developmentSnapshot=
            if [ -z "$version" -a -z "$releaseDate" ]; then
                einfo "${message:-$(basename "$bundir")}: ?"
            else
                einfo "${message:-$(basename "$bundir")}: Version${version:+ $version}${releaseDate:+ du $releaseDate}${developmentSnapshot:+ (developmentSnapshot)}"
            fi
        else
            einfo "${message:-$(basename "$bundir")}: ?"
        fi
        return 0
        
    elif [ "$action" == "tag" ]; then
        developmentSnapshot=
        
        if [ -z "$description" -a -f "$infosfile" ]; then
            read_jawotools_properties "$infosfile"
        fi
        
        if [ -f "$versionfile" ]; then
        # si le fichier de version existe...
            version="$(awk <"$versionfile" '{
    gsub("-r../../....$", "")
    print
    exit
}')"
            releaseDate="$(awk <"$versionfile" '{
    if ($0 ~ /.*-r..\/..\/....$/) {
        gsub(".*-r", "")
        print
    }
    exit
}')"
            developmentSnapshot=
            if [ -n "$srcdir" -a -x "$scriptdir/reldiff" ]; then
                "$scriptdir/reldiff" -p "$srcdir" -c && developmentSnapshot=1
            fi
            
        else
            # pas de fichier de version. il faut saisir les valeurs à la main
            version_or_date_flag=-i
            [ \( -z "$version" -o "$version" == "-" \) -a -z "$releaseDate" ] && version_or_date_flag=-c
            
            read_value -i "Entrez la description" description "$description" N
            if [ "$version" == "-" ]; then
                version=
            else
                read_value $version_or_date_flag "Entrez la version (vide s'il n'y a que la date de release)" version "$version" N
                [ "$version" == "-" ] && version=
            fi
            if [ -z "$releaseDate" ]; then
                # date de release par défaut: la date du jour
                releaseDate="$(date +"%d/%m/%Y")"
            fi
            read_value $version_or_date_flag "Entrez la date de release" releaseDate "$releaseDate" N
            [ "$releaseDate" == "-" ] && releaseDate=
            
            if [ -z "$version" -a -z "$releaseDate" ]; then
                die "Il faut avoir un numéro de version et/ou une date de release"
            fi
        fi
        
        einfo "${message:-$(basename "$bundir")}: Version${version:+ $version}${releaseDate:+ du $releaseDate}${developmentSnapshot:+ (developmentSnapshot)}"
        [ "$infosfile" != "$infosfile2" ] && rm -f "$infosfile"
        file_set_properties -c "$infosfile2" description "$description" version "$version" releaseDate "$releaseDate" developmentSnapshot "$developmentSnapshot"
        
    else
        die "Action inconnue: $action"
    fi
}
