/* $VER: ENV_Locale 1.0 (27.04.94) by NDY's */

/* Usage:    ENV_Locale language
   Effect:   OS40+    none
             OS38/39  Default language to ENV[ARC]:Locale
             OS37-    Set ENV[ARC]:Locale to "language"
   Result:   5        Failed to open/write either ENV:Locale or ENVARC:Locale
             10       Failed to open/write both files
             15       Failed to open locale.library and no argument was given */

MODULE 'libraries/locale' /* Created with Iconvert from include/libraries/locale.i */
DEF localebase

PROC main()
  DEF res,file,fhandle,i,defloc:PTR TO locale,preflang,language[30]:STRING
  IF (localebase:=OpenLibrary({locname},40))=NIL
    IF localebase:=OpenLibrary({locname},0)
      /* Locale V38/39 -> read preferred language (only first one) */
      defloc:=openlocale(NIL)
      preflang:=defloc.preflanguages
      language:=^preflang
      closelocale(defloc)
      CloseLibrary(localebase)
    ELSEIF StrLen(arg)>0
      /* No locale -> use argument */
      language:=arg
    ELSE
      /* Neither locale nor argument -> info */
      WriteF({usage})
      RETURN 15
    ENDIF
  ELSE
    /* Locale V40+ -> ENV[ARC]:Language correctly set */
    CloseLibrary(localebase)
    RETURN NIL
  ENDIF
  file:={envarc}
  res:=0
  FOR i:=1 TO 2
    IF fhandle:=Open(file,NEWFILE)
      IF Write(fhandle,language,StrLen(language))<>StrLen(language) THEN res:=res+5
      Close(fhandle)
    ELSE
      res:=res+5
    ENDIF
    file:={env}
  ENDFOR
  RETURN res
             CHAR '$VER: ENV_Locale 1.0 (27.04.94) by NDY''s'
  locname:   CHAR 'locale.library'
  usage:     CHAR 'Usage: ENV_Locale language  equals: Echo >ENV[ARC]:Locale language\n'
  envarc:    CHAR 'ENVARC:Locale'
  env:       CHAR 'ENV:Locale'
ENDPROC

/* The used locale.library functions */
PROC openlocale(name)
  DEF localeptr
  CONST OPENLOCALE=-156
  IF localebase<>NIL
    MOVE.L  localebase,A6
    MOVE.L  name,A0
    JSR     OPENLOCALE(A6)
    MOVE.L  D0,localeptr
  ENDIF
ENDPROC localeptr
PROC closelocale(localeptr)
  CONST CLOSELOCALE=-42
  IF localebase<>NIL
    MOVE.L  localebase,A6
    MOVE.L  localeptr,A0
    JSR     CLOSELOCALE(A6)
  ENDIF
ENDPROC

