2024-08-15 10:12:58 +02:00
package main
import (
"context"
"flag"
"fmt"
"os"
"path"
2024-08-15 12:49:36 +02:00
"git.defalsify.org/vise.git/engine"
2024-09-07 21:05:09 +02:00
"git.defalsify.org/vise.git/logging"
2024-08-15 10:12:58 +02:00
"git.defalsify.org/vise.git/resource"
2025-01-02 10:39:49 +01:00
"git.defalsify.org/vise.git/lang"
2024-10-15 22:41:16 +02:00
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers"
2024-09-19 14:54:23 +02:00
"git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/storage"
2025-01-02 10:39:49 +01:00
"git.grassecon.net/urdt/ussd/internal/args"
2024-10-30 14:09:15 +01:00
"git.grassecon.net/urdt/ussd/remote"
2024-08-15 10:12:58 +02:00
)
2024-08-15 12:49:36 +02:00
var (
2024-12-13 09:38:10 +01:00
logg = logging . NewVanilla ( )
scriptDir = path . Join ( "services" , "registration" )
menuSeparator = ": "
2024-08-15 12:49:36 +02:00
)
2024-10-15 22:41:16 +02:00
func init ( ) {
initializers . LoadEnvVariables ( )
}
2025-01-02 15:31:13 +01:00
// TODO: external script automatically generate language handler list from select language vise code OR consider dynamic menu generation script possibility
2024-08-15 10:12:58 +02:00
func main ( ) {
2024-10-15 22:41:16 +02:00
config . LoadConfig ( )
2024-09-05 16:07:20 +02:00
var dbDir string
2024-08-15 10:12:58 +02:00
var size uint
var sessionId string
2024-10-17 11:46:20 +02:00
var database string
2024-09-24 13:10:10 +02:00
var engineDebug bool
2025-01-03 11:28:27 +01:00
var gettextDir string
2025-01-02 10:39:49 +01:00
var langs args . LangVar
2024-09-05 16:07:20 +02:00
flag . StringVar ( & sessionId , "session-id" , "075xx2123" , "session id" )
2024-10-17 11:46:20 +02:00
flag . StringVar ( & database , "db" , "gdbm" , "database to be used" )
2024-09-05 16:07:20 +02:00
flag . StringVar ( & dbDir , "dbdir" , ".state" , "database dir to read from" )
2024-09-24 15:11:22 +02:00
flag . BoolVar ( & engineDebug , "d" , false , "use engine debug output" )
2024-09-05 16:07:20 +02:00
flag . UintVar ( & size , "s" , 160 , "max size of output" )
2025-01-02 10:39:49 +01:00
flag . StringVar ( & gettextDir , "gettext" , "" , "use gettext translations from given directory" )
flag . Var ( & langs , "language" , "add symbol resolution for language" )
2024-09-05 17:45:35 +02:00
flag . Parse ( )
2024-09-10 11:32:19 +02:00
logg . Infof ( "start command" , "dbdir" , dbDir , "outputsize" , size )
2024-08-15 10:12:58 +02:00
2025-01-02 10:39:49 +01:00
if len ( langs . Langs ( ) ) == 0 {
langs . Set ( config . DefaultLanguage )
}
2024-08-15 10:12:58 +02:00
ctx := context . Background ( )
2024-09-07 21:05:09 +02:00
ctx = context . WithValue ( ctx , "SessionId" , sessionId )
2024-10-17 11:46:20 +02:00
ctx = context . WithValue ( ctx , "Database" , database )
2025-01-02 10:39:49 +01:00
ln , err := lang . LanguageFromCode ( config . DefaultLanguage )
if err != nil {
fmt . Fprintf ( os . Stderr , "default language set error: %v" , err )
os . Exit ( 1 )
}
ctx = context . WithValue ( ctx , "Language" , ln )
2024-09-02 20:06:42 +02:00
pfp := path . Join ( scriptDir , "pp.csv" )
2024-08-19 09:59:20 +02:00
2024-08-15 12:49:36 +02:00
cfg := engine . Config {
2024-12-12 19:12:25 +01:00
Root : "root" ,
SessionId : sessionId ,
OutputSize : uint32 ( size ) ,
FlagCount : uint32 ( 128 ) ,
2024-12-13 09:38:10 +01:00
MenuSeparator : menuSeparator ,
2024-08-15 12:49:36 +02:00
}
2024-09-05 16:07:20 +02:00
2024-09-21 19:49:11 +02:00
resourceDir := scriptDir
menuStorageService := storage . NewMenuStorageService ( dbDir , resourceDir )
2025-01-02 10:39:49 +01:00
if gettextDir != "" {
menuStorageService = menuStorageService . WithGettext ( gettextDir , langs . Langs ( ) )
}
2024-09-19 14:54:23 +02:00
2025-01-02 10:39:49 +01:00
err = menuStorageService . EnsureDbDir ( )
2024-09-19 16:01:23 +02:00
if err != nil {
fmt . Fprintf ( os . Stderr , err . Error ( ) )
os . Exit ( 1 )
}
2024-09-21 19:49:11 +02:00
rs , err := menuStorageService . GetResource ( ctx )
2024-08-15 10:12:58 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt . Fprintf ( os . Stderr , err . Error ( ) )
2024-08-15 10:12:58 +02:00
os . Exit ( 1 )
}
2024-08-21 20:37:29 +02:00
2024-09-21 19:49:11 +02:00
pe , err := menuStorageService . GetPersister ( ctx )
2024-08-15 12:49:36 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt . Fprintf ( os . Stderr , err . Error ( ) )
os . Exit ( 1 )
2024-09-04 18:57:08 +02:00
}
2024-09-22 03:33:11 +02:00
userdatastore , err := menuStorageService . GetUserdataDb ( ctx )
2024-09-05 16:07:20 +02:00
if err != nil {
fmt . Fprintf ( os . Stderr , err . Error ( ) )
os . Exit ( 1 )
}
2024-09-04 18:57:08 +02:00
2024-09-05 17:45:35 +02:00
dbResource , ok := rs . ( * resource . DbResource )
2024-09-04 18:57:08 +02:00
if ! ok {
2024-09-06 01:40:57 +02:00
fmt . Fprintf ( os . Stderr , err . Error ( ) )
2024-09-04 18:57:08 +02:00
os . Exit ( 1 )
2024-08-15 12:49:36 +02:00
}
2024-08-31 09:21:20 +02:00
2024-10-31 18:11:26 +01:00
lhs , err := handlers . NewLocalHandlerService ( ctx , pfp , true , dbResource , cfg , rs )
2024-09-21 19:49:11 +02:00
lhs . SetDataStore ( & userdatastore )
lhs . SetPersister ( pe )
2024-09-19 14:54:23 +02:00
if err != nil {
fmt . Fprintf ( os . Stderr , err . Error ( ) )
os . Exit ( 1 )
}
2024-10-30 14:09:15 +01:00
accountService := remote . AccountService { }
2024-10-17 11:54:11 +02:00
hl , err := lhs . GetHandler ( & accountService )
2024-09-02 15:05:13 +02:00
if err != nil {
2024-09-05 16:07:20 +02:00
fmt . Fprintf ( os . Stderr , err . Error ( ) )
os . Exit ( 1 )
2024-08-31 09:21:20 +02:00
}
2024-08-26 14:53:07 +02:00
2024-09-19 14:54:23 +02:00
en := lhs . GetEngine ( )
2024-09-06 00:55:51 +02:00
en = en . WithFirst ( hl . Init )
2024-09-24 13:10:10 +02:00
if engineDebug {
2024-09-06 00:55:51 +02:00
en = en . WithDebug ( nil )
}
2024-08-16 12:38:58 +02:00
2024-09-20 21:16:43 +02:00
err = engine . Loop ( ctx , en , os . Stdin , os . Stdout , nil )
2024-08-15 10:12:58 +02:00
if err != nil {
fmt . Fprintf ( os . Stderr , "loop exited with error: %v\n" , err )
os . Exit ( 1 )
}
}