#!@l_prefix@/lib/openpkg/bash
##
##  release -- OpenPKG Release Determination Utility
##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   configuration
prefix="@l_prefix@"

#   minimum command line parsing
opt_F="OpenPKG-%t %u"
opt_r=""
while [ $# -gt 0 ]; do
    case "$1" in
        -F          ) opt_F="$2"; shift;                      shift ;;
        --fmt       ) opt_F="$2"; shift;                      shift ;;
        -F*         ) opt_F="`expr ".$1" : '.-F\(.*\)'`";     shift ;;
        --fmt=*     ) opt_F="`expr ".$1" : '.--fmt=\(.*\)'`"; shift ;;
        -r          ) opt_r="$2"; shift;                      shift ;;
        --release   ) opt_r="$2"; shift;                      shift ;;
        -r*         ) opt_r="`expr ".$1" : '.-r\(.*\)'`";     shift ;;
        --release=* ) opt_r="`expr ".$1" : '.--release=\(.*\)'`"; shift ;;
        *       ) break ;;
    esac
done

#   translate a release number to a release tag
number_to_tag () {
    sed -e 's;^;X;' \
        -e 's;^X\([^.-][^.-]*\.[^.-][^.-]*\)\.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*$;\1-SOLID;' \
        -e 's;^X\([^.-][^.-]*\.[^.-][^.-]*\)\.[^.-][^.-]*.*$;\1-RELEASE;' \
        -e 's;^X\([^.-][^.-]*\)\.[^.-][^.-]*.*$;\1-STABLE;' \
        -e 's;^X[^.-][^.-]*.*$;CURRENT;' \
        -e 's;^X.*$;UNKNOWN;'
}

#   sanity check a release tag
tag_sanity () {
    sed -e 's;^;X;' \
        -e 's;^X[^.-][^.-]*\.[^.-][^.-]*-RELEASE$;OK;' \
        -e 's;^X[^.-][^.-]*\.[^.-][^.-]*-SOLID-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$;OK;' \
        -e 's;^X[^.-][^.-]*\.[^.-][^.-]*-SOLID$;OK;' \
        -e 's;^X[^.-][^.-]*-STABLE-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$;OK;' \
        -e 's;^X[^.-][^.-]*-STABLE$;OK;' \
        -e 's;^XCURRENT-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$;OK;' \
        -e 's;^XCURRENT$;OK;' \
        -e 's;^X.*$;ERROR;'
}

#   determine release
tag=""
if [ ".$opt_r" != . ]; then
    tag=`echo ".$opt_r" | sed -e 's;^\.;;' | number_to_tag`
elif [ -f "$prefix/etc/openpkg/release" ]; then
    tag=`(cat $prefix/etc/openpkg/release; echo "") | sed \
        -e 's;^;X;' \
        -e 's;^X *TAG *= *\([^ ][^ ]*\).*;\1;' \
        -e '/^X/d' | \
        sed -n -e '$p'`
else
    tag=`$prefix/bin/openpkg rpm \
        -q --qf '%{VERSION}\n' openpkg | number_to_tag`
fi
if [ .`echo ".$tag" | sed -e 's;^\.;;' | tag_sanity` = .ERROR ]; then
    echo "openpkg:release: WARNING: unable to determine OpenPKG release tag" 1>&2
    tag="UNKNOWN"
fi

#   determine distribution URL
url=""
if [ -f "$prefix/etc/openpkg/release" ]; then
    url=`(cat $prefix/etc/openpkg/release; echo "") | sed \
        -e 's;^;X;' \
        -e 's;^X *URL *= *\([^ ][^ ]*\).*;\1;' \
        -e '/^X/d' | \
        sed -n -e '$p'`
fi
if [ ".$url" = . ]; then
    url="ftp://ftp.openpkg.org/*"
fi
case ".$url" in
    */\* )
        url=`echo ".$url" | sed -e 's;^\.;;' -e 's;/\*$;;'`
        case "$tag" in
            CURRENT )
                url="$url/current/SRC/"
                ;;
            CURRENT-* )
                version=`echo "$tag" | sed -e 's;^CURRENT-;;'`
                url="$url/current/$version/"
                ;;
            *-STABLE )
                version=`echo "$tag" | sed -e 's;^\(.*\)-STABLE$;\1;'`
                url="$url/stable/$version/"
                ;;
            *-STABLE-* )
                version=`echo "$tag" | sed -e 's;^\(.*\)-STABLE-\(.*\)$;\1.\2;'`
                url="$url/stable/$version/"
                ;;
            *-SOLID )
                version=`echo "$tag" | sed -e 's;^\(.*\)-SOLID$;\1;'`
                url="$url/solid/$version/"
                ;;
            *-SOLID-* )
                version=`echo "$tag" | sed -e 's;^\(.*\)-SOLID-\(.*\)$;\1.\2;'`
                url="$url/solid/$version/"
                ;;
            *-RELEASE )
                version=`echo "$tag" | sed -e 's;^\(.*\)-RELEASE$;\1;'`
                url="$url/release/$version/"
                ;;
        esac
        ;;
esac

#   read uuid
[ -f "$prefix/etc/openpkg/uuid" ] && . "$prefix/etc/openpkg/uuid"

#   generate output
echo "X$opt_F" |\
sed -e 's/^X//' \
    -e "s;%t;${tag};g" \
    -e "s;%u;${url};g" \
    -e "s;%r;${UUID_REGISTRY};g" \
    -e "s;%i;${UUID_INSTANCE};g" \
    -e "s;%p;${UUID_PLATFORM};g" \
    -e 's/\\n/^/g' | tr '^' '\012'

