{-# LINE 1 "src/Xmobar/System/Localize.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Localize
-- Copyright   :  (C) 2011, 2018 Martin Perner
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Martin Perner <martin@perner.cc>
-- Stability   :  unstable
-- Portability :  unportable
--
-- This module provides an interface to locale information e.g. for DateL
--
-----------------------------------------------------------------------------

module Xmobar.System.Localize
    ( setupTimeLocale,
      getTimeLocale
    ) where

import Foreign.C

{-# LINE 25 "src/Xmobar/System/Localize.hsc" #-}
import qualified Data.Time.Format as L

{-# LINE 27 "src/Xmobar/System/Localize.hsc" #-}

import Codec.Binary.UTF8.String

--  get localized strings
type NlItem = CInt


foreign import ccall unsafe "langinfo.h nl_langinfo"
  nl_langinfo :: NlItem -> IO CString

amStr  :: NlItem
amStr :: CInt
amStr  =  CInt
131110
pmStr    :: NlItem
pmStr :: CInt
pmStr    =  CInt
131111
dTFmt  :: NlItem
dTFmt :: CInt
dTFmt  =  CInt
131112
dFmt  :: NlItem
dFmt :: CInt
dFmt  =  CInt
131113
tFmt  :: NlItem
tFmt :: CInt
tFmt  =  131114
tFmtAmpm    :: NlItem
tFmtAmpm :: CInt
tFmtAmpm    =  CInt
131115
abday1 :: NlItem
abday1 :: CInt
abday1 =  CInt
131072
abday7    :: NlItem
abday7 :: CInt
abday7    =  CInt
131078
day1 :: NlItem
day1 :: CInt
day1 =  131079
day7    :: NlItem
day7 :: CInt
day7    =  131085
abmon1 :: NlItem
abmon1 :: CInt
abmon1 =  CInt
131086
abmon12    :: NlItem
abmon12 :: CInt
abmon12    =  CInt
131097
mon1 :: NlItem
mon1 :: CInt
mon1 =  CInt
131098
mon12  :: NlItem
mon12 :: CInt
mon12  =  CInt
131109

{-# LINE 45 "src/Xmobar/System/Localize.hsc" #-}

getLangInfo :: NlItem -> IO String
getLangInfo item = do
  itemStr <- nl_langinfo item
  str <- peekCString itemStr
  return $ if isUTF8Encoded str then decodeString str else str


foreign import ccall unsafe "locale.h setlocale"
    setlocale :: CInt -> CString -> IO CString

setupTimeLocale :: String -> IO ()
setupTimeLocale l = withCString l (setlocale 2) >> return ()
{-# LINE 58 "src/Xmobar/System/Localize.hsc" #-}

getTimeLocale :: IO L.TimeLocale
getTimeLocale = do
  -- assumes that the defined values are increasing by exactly one.
  -- as they are defined consecutive in an enum this is reasonable
  days   <- mapM getLangInfo [day1 .. day7]
  abdays <- mapM getLangInfo [abday1 .. abday7]

  mons   <- mapM getLangInfo [mon1 .. mon12]
  abmons <- mapM getLangInfo [abmon1 .. abmon12]

  amstr <- getLangInfo amStr
  pmstr <- getLangInfo pmStr
  dtfmt <- getLangInfo dTFmt
  dfmt  <- getLangInfo dFmt
  tfmt  <- getLangInfo tFmt
  tfmta <- getLangInfo tFmtAmpm

  let t =  L.defaultTimeLocale {L.wDays  = zip days abdays
                               ,L.months = zip mons abmons
                               ,L.amPm = (amstr, pmstr)
                               ,L.dateTimeFmt = dtfmt
                               ,L.dateFmt = dfmt
                               ,L.timeFmt = tfmt
                               ,L.time12Fmt = tfmta}
  return t