<?xml version="1.0"?>
<!--
 -
  -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->
<v:login name="calendar_login" realm="wa" mode="url" xmlns:v="http://www.openlinksw.com/vspx/" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <v:template name='inl' type='if-no-login'/>
  <v:after-data-bind>
    <![CDATA[
      declare page_name varchar;
      declare params any;
      declare vh, lh, hf, ods_path any;

      set_qualifier ('DB');
      params := self.vc_page.vc_event.ve_params;
      page_name := CAL.WA.page_name ();
      if (page_name = 'error.vspx')
        return;

      if (isnull (self.account_rights) and not is_empty_or_null (self.sid))
      {
        self.vc_redirect (sprintf ('/ods/error.vspx?__PAGE=%U&__SQL_STATE=%U&__SQL_MESSAGE=%U', 'home.vspx', '23023', 'You have no access rights for the application!'));
        return;
      }

      vh := http_map_get ('vhost');
      lh := http_map_get ('lhost');
      hf := http_request_header (lines, 'Host');
      if (hf is not null and exists (select 1 from HTTP_PATH where HP_HOST = vh and HP_LISTEN_HOST = lh and HP_LPATH = '/ods'))
      {
        ods_path := DB.DBA.WA_GET_PROTOCOL() || hf || '/ods/';
      } else {
        ods_path := DB.DBA.WA_LINK (1, '/ods/');
      }
      if ((page_name = 'home.vspx') and (get_keyword('URL', params, '') <> ''))
      {
        self.vc_redirect(sprintf('%slogin.vspx?URL=%U', ods_path, concat(HTTP_REQUESTED_URL(), '?URI=', get_keyword('URL', params, ''))));
        return;
      }
      if (isnull (self.account_rights))
      {
        http_request_status ('HTTP/1.1 302 Found');
        http_header (sprintf('Location: %slogin.vspx?URL=%U\r\n', ods_path, HTTP_REQUESTED_URL()));;
        return;
      }
    ]]>
  </v:after-data-bind>
</v:login>
