-- License: ISC (see LICENSE)
  Author :   <  >

module EpubTools.EpubMeta.Import
   ( importOpf )
   where

import Codec.Archive.Zip ( Entry (..), addEntryToArchive, readEntry, toArchive )
import Codec.Epub.IO ( getPkgPathXmlFromBS, writeArchive )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Maybe ( fromJust )
import System.Directory ( removeFile )

import EpubTools.EpubMeta.Opts
import EpubTools.EpubMeta.Util


importOpf :: Options -> FilePath -> EM ()
importOpf opts zipPath = do
   -- The file we want to insert into the book
   let pathToNewOpf = fromJust . optImport $ opts

   -- Make new file into a Zip Entry
   tempEntry <- liftIO $ readEntry [] pathToNewOpf

   -- The path in the book where new file must go
   strictBytes <- liftIO $ BS.readFile zipPath
   (pathToOldFile, _) <- getPkgPathXmlFromBS strictBytes

   -- Adjust the entry's path
   let newEntry = tempEntry { eRelativePath = pathToOldFile }

  Bytes of existing archive already loaded , make an Archive from that
   let oldArchive = toArchive . BL.fromChunks $ [strictBytes]

   -- Add new entry to it (replacing old one)
   let newArchive = addEntryToArchive newEntry oldArchive

  We 're ready to write the new file , delete the old one
   liftIO $ removeFile zipPath

   -- Write the new archive out
   liftIO $ writeArchive zipPath newArchive

 License: ISC (see LICENSE)
 The file we want to insert into the book
 Make new file into a Zip Entry
 The path in the book where new file must go
 Adjust the entry's path
 Add new entry to it (replacing old one)
 Write the new archive out
  Author :   <  >

module EpubTools.EpubMeta.Import
   ( importOpf )
   where

import Codec.Archive.Zip ( Entry (..), addEntryToArchive, readEntry, toArchive )
import Codec.Epub.IO ( getPkgPathXmlFromBS, writeArchive )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Maybe ( fromJust )
import System.Directory ( removeFile )

import EpubTools.EpubMeta.Opts
import EpubTools.EpubMeta.Util


importOpf :: Options -> FilePath -> EM ()
importOpf opts zipPath = do
   let pathToNewOpf = fromJust . optImport $ opts

   tempEntry <- liftIO $ readEntry [] pathToNewOpf

   strictBytes <- liftIO $ BS.readFile zipPath
   (pathToOldFile, _) <- getPkgPathXmlFromBS strictBytes

   let newEntry = tempEntry { eRelativePath = pathToOldFile }

  Bytes of existing archive already loaded , make an Archive from that
   let oldArchive = toArchive . BL.fromChunks $ [strictBytes]

   let newArchive = addEntryToArchive newEntry oldArchive

  We 're ready to write the new file , delete the old one
   liftIO $ removeFile zipPath

   liftIO $ writeArchive zipPath newArchive
