
module Package where

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

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 ++ "/gen/skeleton/default.scad"    , getSCADVisualSkeleton ew)
      , (cwd ++ "/gen/skeleton/default_col.scad", getSCADCollisionSkeleton ew)
      , ( cwd ++ "/gen/skeleton/folded.scad"
        , getSCADVisualSkeleton (ewalker cwd Folded)
        )
      , ( cwd ++ "/gen/skeleton/expanded.scad"
        , getSCADVisualSkeleton (ewalker cwd Expanded)
        )
      ]

  generateBOM (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 ++ "/tmp/scad"
                             , pkg_dir  = cwd ++ "/pkg/schpin_koke"
                             , src_dir  = cwd ++ "/src/scad"
                             }

