module Main where

import Codec.Archive.Zip
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.Char8 as BSL8
import System.Environment (getArgs)

import SDLClient.SpriteSheet

usage :: String
usage = unlines [ "USAGE: make-texture [name] [width] [height] [type] [spritesheet] "
                , "    name        : string"
                , "    width       : int"
                , "    height      : int"
                , "    type        : basic | fence | blob"
                , "    spritesheet : png"
                ]

main :: IO ()
main = do
  args <- getArgs
  case args of
    (name:width:height:typ:sheet:_) -> do
      imgContents <- BSL.readFile sheet
      let desc = SpriteDescription
                   { sdSize = (read width, read height)
                   , sdName = (BS8.pack name)
                   , sdType = case typ of
                                "basic" -> BasicSprite
                                "fence" -> FenceSprite
                                "blob"  -> BlobSprite
                                _       -> error "unknown sprite type"
                   }
      let meta = toEntry "texture.hs"  0 (BSL8.pack (show desc))
      let png  = toEntry "sprites.png" 0 imgContents
      let archive = meta `addEntryToArchive`
                    (png  `addEntryToArchive` emptyArchive)
      BSL.writeFile (name ++ ".texture") (fromArchive archive)
    _ -> putStrLn usage
