## -*- coding: utf-8 -*-
##
## CDDL HEADER START
##
## The contents of this file are subject to the terms of the
## Common Development and Distribution License (the "License").
## You may not use this file except in compliance with the License.
##
## You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
## or http://www.opensolaris.org/os/licensing.
## See the License for the specific language governing permissions
## and limitations under the License.
##
## When distributing Covered Code, include this CDDL HEADER in each
## file and include the License file at usr/src/OPENSOLARIS.LICENSE.
## If applicable, add the following below this CDDL HEADER, with the
## fields enclosed by brackets "[]" replaced with your own identifying
## information: Portions Copyright [yyyy] [name of copyright owner]
##
## CDDL HEADER END
##
## Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
##
<%!
        import os.path
        import pkg.server.api as api
        import pkg.server.api_errors as api_errors

        CLIENT_API_VERSION = 12
%>\
<%page args="g_vars"/>\
<%
        base = g_vars["base"]
        http_depot = g_vars["http_depot"]
        config = api.ConfigInterface(CLIENT_API_VERSION, base)
        request = api.RequestInterface(CLIENT_API_VERSION, base)

        # XXX default language if one can't be automatically determined,
        # where to set this?
        dlang = "en"

        # First, get the list of desired languages from the request.
        rlangs = request.get_accepted_languages()

        # Second, check each language name (which should either be an ISO
        # 639(.1/.2) code or an l10n simple name) to see if a directory of
        # content exists for it.
        for rl in rlangs:
                if os.path.exists(os.path.join(config.web_root, rl)):
                        dlang = rl
                        break

        # Third, determine if we need to redirect the user to a particular
        # page.
        rpath = request.path_info.strip("/")
        pub = g_vars["pub"]
        if pub:
                # Ignore publisher component of path.
                rpath = rpath.replace(pub, "").strip("/")

        prefix = ""
        if rpath == "":
                # Because the redirect is relative if the original request
                # didn't end with a '/', special care has to be taken for
                # this particular case.
                if pub and not request.path_info.endswith("/"):
                        prefix = pub + "/"
                rpath = "index.shtml"
        if http_depot:
                prefix = http_depot

        # Finally, redirect the client to the content appropriate for their
        # language and region.
        raise api_errors.RedirectException("{0}{1}/{2}".format(
            prefix, dlang, rpath))
%>\
