#
#   Version 1.2
#
#   Copyright 2025 Lee Djavaherian
#
#   This file is part of LouiaObScura.
#
#   LouiaObScura 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, either version 3 of the License, or
#   (at your option) any later version.
#
#   LouiaObScura 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 LouiaObScura.  If not, see <http://www.gnu.org/licenses/>.
###################################################################################

cmake_minimum_required(VERSION 3.24)

include(mlua_import.cmake)

project(Louia_ObScura C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

#set(PICO_BOARD pico_w)
set(PICO_BOARD pico2_w)

mlua_init()
mlua_add_lua_modules(Louia_ObScura_main main.lua)

#target_link_libraries(Louia_ObScura_main INTERFACE
# nothing needed here
#)

mlua_add_executable(Louia_ObScura)

target_compile_definitions(Louia_ObScura PRIVATE
    LUA_MAIN_MODULE=LouiaObScura
    MLUA_STDIO_INIT_USB=1
    #BELOW HELPS TO DELAY UNTIL USB CONNECTED TO SEE INITIAL INPUT
    #AND REMOVES NEED TO ADD THE INITIAL DELAY IN THE PROGRAM
    PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS=3000
    #PICO_ENABLE_STDIO_USB=1
    LWIP_DHCP=1
    NO_SYS=1
    LWIP_IPV6=1
    LWIP_IPV4=1
    LWIP_TCP_TTL=60
    LWIP_TCP=1
    #UDP IS REQUIRED FOR DHCP
    LWIP_UDP=1
    LWIP_MEMP_NUM_RAW_PCB=100
    LWIP_MEMP_NUM_TCP_PCB=100
    LWIP_MEMP_NUM_TCP_PCB_LISTEN=100
    #LWIP_MEMP_NUM_NETCONN=100
    #LFS_THREADSAFE
    #LWIP_PBUF_POOL_BUFSIZE=2500
)

target_link_libraries(Louia_ObScura PRIVATE
    Louia_ObScura_main
    mlua_mod_lwip
    mlua_mod_mlua.stdio
    #pico.stdio is required for io.read to work
    #mlua_mod_pico.stdio
    mlua_mod_pico.stdio.usb
    mlua_mod_mlua.io
    mlua_mod_pico.stdlib
    mlua_mod_pico
    mlua_mod_pico.cyw43.wifi
    mlua_mod_pico.cyw43
    mlua_mod_lwip.tcp
    mlua_mod_lwip.netif
    mlua_mod_mlua.time
    mlua_mod_mlua.mem
    mlua_mod_mlua.block.flash
    #mlua_mod_mlua.block.mem
    mlua_mod_mlua.fs
    mlua_mod_mlua.fs.lfs
    mlua_mod_table
    mlua_mod_coroutine
    mlua_mod_string
    mlua_mod_mlua.int64
)

mlua_target_config(Louia_ObScura
    #
    ############ THESE ARE ORIGINAL LOUIA PARAMETERS ##############
    HOMEPATH:string="/index.gmi"
    NOMIME:string="text/gemini\\\\; charset=utf-8" #semicolon has to be double-escaped with 4 backslashes
    MAXTHREADS:integer=100
    MAXPATHLEN:integer=80
    MAXREQBYTES:integer=100 #should be bigger than MAXPATHLEN
    DOWNLOADBUFFER:integer=512  #RAM chunk per coroutine for big downloads
    MAXUPLOADBYTES:integer=32 #holds max `date` string for /settime routine and fits pid and other admin paths
    TIMEOUTCHECKSECS:string=".000001" #this was floating-point in Louia but had to be set as string here and converted later
    TIMEOUTSECS:integer=120
    DEBUG:boolean=false
    UPLOADSENABLED:boolean=true
    SAFEFILES:boolean=true
    POWERSAVEMODE:boolean=true
    RELOADPATH:string="/reloadme" #change this to something unique and random if SAFEFILES is true
    STRICTMODE:boolean=true
    #strict mode disables all chars except for alphanumeric, underscore, hyphen
    #forces 3 character extension, disables percent-decoding,
    #only allows alphanumeric, space and hyphen in uploads, only allows one subdirectory level,
    #this tightens the filter and has lots of yielding/timeouts rather than rejections
    #
    ############ THESE ARE ADDITIONAL LOUIA OBSCURA PARAMETERS ######################
    MAINTMODE:boolean=false
    TIMEOUTSCRIPTSECS:integer=1800 # 30 mins
    TIMEOUTUPLOADSITESECS:integer=90 # 90 seconds
    CONFIGPASS:string="louia" #change this to something unique and random if using a password
    CONFIGTIMEOUTSECS:integer=7
    WIFI_PASSWORD:string="abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789" # wifi password on the router
    WIFI_SSID:string="changeme" #change this to match the router SSID
    #Had to escape \x with extra backslash - this is stored as 8 bits which could cause collision with delimiter depending on MAC
    WIFI_BSSID:string="\\xAB\\xCD\\xEF\\x12\\x34\\x56" #change this to match MAC address of device once connected
    WIFI_COUNTRY:string="US" # two-letter country code from cyw43 driver, "XX" for worldwide, change as needed
    PORT:integer=300
    UPLOADSITEPORT:integer=301
    SAFEIP:string="192.168.0.101" #change this to match admin client IP if using this, or use "any" to allow any (but blocks DOMAINLOCAL)
    RTCIP:string="192.168.0.102" #change this to match time server IP if using this, or use "any" to allow any (but blocks DOMAINLOCAL)
    MYIP:string="dhcp" #leave as "dhcp" if using DHCP
    MYGATEWAY:string="dhcp"
    MYNETMASK:string="dhcp"
    SCRIPTSENABLED:boolean=true
    NICEENABLED:boolean=false
    SAFESCRIPT:boolean=true
    TIMEOFFSETUS:integer=1000000 #In microseconds, default is 1 second
    CONFIGPATH:string="/config"
    DOMAIN:string="pico" #change this to the domain
    DOMAINLOCAL:string="picolocal" #change this to a local domain accessed via private LAN/private IP address
    DIR:string="/louia"
    #
    # Should be replaced with unique, random strings
    FORMATPATH:string="/format"
    UPLOADSITEPATH:string="/uploadsite" 
    DELETEPIDFILESPATH:string="/deletepids" 
    TIMEPATH:string="/settime"
    CLEARLOGPATH:string="/clearlog" 
    #this one must contain root dir as DIR, and the next level scripts and log directory that follow must match them
    MKDIRLIST:string="/louia:/louia/images:/louia/files:/louia/scripts:/louia/log" 
    ADMINPATH:string="/scripts/admin.lua" #change scripts dir something unique and random if using scripts + uploads
    TESTSCRIPTPATH:string="/scripts/obscura.lua" #change scripts dir to something unique and random if using scripts + uploads
    LOGPATH:string="/log/spartan.log" # use "none" to disable logs (but it cannot be re-enabled after the fact)
    ROM_VERSION:string="1.2"
    #for littlefs 8192 works, but 4096 doesn't, yet I needed 16384 to reliably write all of these config values
    ROM_MAXCONFIGPARTSIZE:integer=16384 
    #Changing the flash and part values below will wipe out the site partition
    # For Pico 2 W:
      ROM_MAXFLASHSIZE:integer=4194304 #for Pico 2 W
      ROM_MAXDATAPARTSIZE:integer=3407872 #for Pico 2 W which is 3.25 MiB for site data
      MAXMEMBYTES:integer=300000
      MAXPIDFILEBYTES:integer=1000 #1K PID file size max
      MAXLOGBYTES:integer=100000 #100K log size max
    # For Pico W with half the memory and flash, uncomment and use these values instead
      #ROM_MAXFLASHSIZE:integer=2097152 #for Pico W
      #ROM_MAXDATAPARTSIZE:integer=1310720 #for Pico W which is 1.25 MiB for site data
      #MAXMEMBYTES:integer=150000
      #MAXPIDFILEBYTES:integer=1000 #1K PID file size max
      #MAXLOGBYTES:integer=50000 #50K log size max
)
