'################################# Iguana CMS ##################################
'Main application file
'Compile it with FreeBasic => 0.24.0
'###############################################################################

#INCLUDE "iguanacms.bi"

DIM AS STRING Request, PageName, Result

'Look for a request
IF CINT(Settings("site_fancy_url")) THEN
  IF UriPart(0) <> "" THEN
    Request = UriPart(0)
  END IF
ELSE
  IF QueryString("module") <> "" THEN
    Request = QueryString("module")
  END IF
END IF

IF Request <> "" THEN
  'Load a module
  IF Request = "pages" THEN
    'Load the pages module
    IF CINT(Settings("site_fancy_url")) THEN
      IF UriPart(1) <> "" THEN
        PageName = UriPart(1)
      END IF
    ELSE
      IF QueryString("pagename") <> "" THEN
        PageName = QueryString("pagename")
      END IF
    END IF
    IF PageName <> "" THEN
      LoadPage(PageName)
    ELSE
      'Load the default page
      LoadPage(Settings("site_index_page"))
    END IF
  ELSE
    'Load other module
    LoadModule(Request)
  END IF
ELSE
  'Load default settings
  SELECT CASE Settings("site_index_type")
    CASE "page"
      LoadPage(Settings("site_index_page"))
    CASE "module"
      LoadModule(Settings("site_index_module"))
  END SELECT
END IF

IF SiteContent = "403" THEN
  'Displays error 403
  SiteContent = LoadTplFile("", "403.html")
END IF

IF SiteContent = "" THEN
  'Displays error 404
  SiteContent = LoadTplFile("", "404.html")
END IF

IF SiteTitle = "" THEN
  SiteTitle = Settings("site_description")
END IF

'Load everything into a variable
Result = LoadTplFile("", "main.html")

'Do the magic
PRINT HttpHeader
PRINT Result

Result = ""
END
