module Unison.Codebase.SqliteCodebase.Paths
  ( codebasePath,
    makeCodebasePath,
    makeCodebaseDirPath,
    backupCodebasePath,
    lockfilePath,
  )
where

import Data.Time (NominalDiffTime)
import System.FilePath ((</>))
import U.Codebase.Sqlite.DbId (SchemaVersion (SchemaVersion))
import Unison.Codebase (CodebasePath)

-- | Prefer makeCodebasePath or makeCodebaseDirPath when possible.
codebasePath :: FilePath
codebasePath :: [Char]
codebasePath = [Char]
".unison" [Char] -> [Char] -> [Char]
</> [Char]
"v2" [Char] -> [Char] -> [Char]
</> [Char]
"unison.sqlite3"

-- | Makes a path to a sqlite database from a codebase path.
makeCodebasePath :: CodebasePath -> FilePath
makeCodebasePath :: [Char] -> [Char]
makeCodebasePath [Char]
root = [Char] -> [Char]
makeCodebaseDirPath [Char]
root [Char] -> [Char] -> [Char]
</> [Char]
"unison.sqlite3"

lockfilePath :: CodebasePath -> FilePath
lockfilePath :: [Char] -> [Char]
lockfilePath [Char]
root = [Char] -> [Char]
makeCodebaseDirPath [Char]
root [Char] -> [Char] -> [Char]
</> [Char]
"unison.lockfile"

-- | Makes a path to the location where sqlite files are stored within a codebase path.
makeCodebaseDirPath :: CodebasePath -> FilePath
makeCodebaseDirPath :: [Char] -> [Char]
makeCodebaseDirPath [Char]
root = [Char]
root [Char] -> [Char] -> [Char]
</> [Char]
".unison" [Char] -> [Char] -> [Char]
</> [Char]
"v2"

-- | Makes a path to store a backup of a sqlite database given the current time.
backupCodebasePath :: SchemaVersion -> NominalDiffTime -> FilePath
backupCodebasePath :: SchemaVersion -> NominalDiffTime -> [Char]
backupCodebasePath (SchemaVersion Word64
schemaVersion) NominalDiffTime
now =
  [Char]
codebasePath [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
".v" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Word64 -> [Char]
forall a. Show a => a -> [Char]
show Word64
schemaVersion [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"." [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show @Int (NominalDiffTime -> Int
forall b. Integral b => NominalDiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor NominalDiffTime
now)