
module Package where

import           Control.Monad.Reader           ( runReader )
import           Parser
import           SCAD
import           System.Directory               ( copyFile
                                                , createDirectory
                                                , createDirectoryIfMissing
                                                , getCurrentDirectory
                                                )
import           Walker
import           Walker.Pkg
import           Walker.SCAD
import           Walker.SRDF
import           Walker.URDF

data ConfType = Default | Folded | Expanded deriving (Show, Eq);

exportPackage :: (ConfType -> SimpleWalker) -> IO ()
exportPackage w = do
    cwd <- getCurrentDirectory
    let ew   = ewalker cwd Default
    let conf = config cwd
    mapM_ (uncurry renderSCAD)
        $ [ (cwd ++ "/out/gen/skeleton/default.scad", getSCADVisualSkeleton ew)
          , ( cwd ++ "/out/gen/skeleton/default_col.scad"
            , getSCADCollisionSkeleton ew
            )
          , ( cwd ++ "/out/gen/skeleton/folded.scad"
            , getSCADVisualSkeleton (ewalker cwd Folded)
            )
          , ( cwd ++ "/out/gen/skeleton/expanded.scad"
            , getSCADVisualSkeleton (ewalker cwd Expanded)
            )
          ]

    generateBOM (ew) (config cwd)
    generatePrint (ew) (config cwd)
    generateWalkerPackage (w Default) conf

    createDirectoryIfMissing False (pkg_dir conf ++ "/launch")
    copyFile "launch/robot_description.launch"
             (pkg_dir conf ++ "/launch/robot_description.launch")
    copyFile "launch/koke_gazebo.launch"
             (pkg_dir conf ++ "/launch/koke_gazebo.launch")

    createDirectoryIfMissing False (pkg_dir conf ++ "/worlds")
    copyFile "worlds/koke.world"      (pkg_dir conf ++ "/worlds/koke.world")

    copyFile "general/CMakeLists.txt" (pkg_dir conf ++ "/CMakeLists.txt")
    copyFile "general/package.xml"    (pkg_dir conf ++ "/package.xml")

  where
    ewalker cwd conf_type = runReader (extendWalker $ w conf_type) $ config cwd

    config cwd = PackageConfig { pkg_name = "schpin_koke"
                               , scad_dir = cwd ++ "/out/tmp/scad"
                               , pkg_dir  = cwd ++ "/out/pkg/schpin_koke"
                               , src_dir  = cwd ++ "/src/scad"
                               }

