         return dict
     }
     
-    func writeMenifest() -> Bool {
+    func writeManifest(at: String = "./Contents/Manifest.plist") throws {
         let nsdictionary = manifest() as NSDictionary
-        return nsdictionary.write(toFile: manifestPath, atomically: false)
+        if !nsdictionary.write(toFile: at, atomically: false) {
+            throw NSError.error(description: "Can not write plist.")
+        }
     }
     
     func create() throws {
         // create book
         do {
             try FileManager.default().createDirectory(atPath: contentsPath, withIntermediateDirectories: true, attributes: nil)
-            if !writeMenifest() {
-                throw NSError.error(description: "Can not write plist.")
-            }
-        } catch {
-            throw error
-        }
-    }
-    
-    static func loadBook() throws -> Book {
-        // load `Contents/Manifest.plist`
-        return Book(name: "a", version: "1.0", contentIdentifier: "b", contentVersion: "1.0", deploymentTarget: "ios10.10")
+            try writeManifest(at: "./\(path)/Contents/Manifest.plist")
+        } catch {
+            throw error
+        }
     }
 }
 
 struct Chapter {
+    let name: String
+    var pages: [Page]
+    let version: String
+    
+    init(name aName: String, version aVersion: String = "1.0", pages arrayPages: [String] = []) {
+        print("Chapter name = \(aName)")
+        name = aName
+        version = aVersion
+        pages = arrayPages.map({ (name) -> Page in
+            let title = name.replacingOccurrences(of: ".playgroundpage", with: "")
+            print("\(name) => \(title)")
+            return Page(name: title, chapterName: name)
+        })
+    }
+
+    func manifest() -> [String:AnyObject] {
+        let dict: [String:AnyObject] = [
+            "Name" : name,
+            "Version" : version,
+            "Pages" : pages.map({ $0.name + ".playgroundpage" }) as [String]
+        ]
+        return dict
+    }
+    
+    mutating func add(page: Page) throws {
+        pages.append(page)
+        do {
+            try writeManifest()
+        } catch {
+            throw error
+        }
+    }
+    
+    func writeManifest() throws {
+        let at = "./Contents/Chapters/\(name).playgroundchapter"
+        do {
+            try FileManager.default().createDirectory(atPath: at, withIntermediateDirectories: true, attributes: nil)
+        } catch {
+            print(error)
+        }
+        let nsdictionary = manifest() as NSDictionary
+        print(nsdictionary)
+        print(at)
+        if !nsdictionary.write(toFile: at + "/Manifest.plist", atomically: false) {
+            throw NSError.error(description: "Chapter: Can not write plist.")
+        }
+    }
 }
 
 struct Page {
+    let name: String
+    let version: String
+    let chapterName: String
+    
+    init(name aName: String, version aVersion: String = "1.0", chapterName aChapterName: String) {
+        name = aName
+        version = aVersion
+        chapterName = aChapterName
+    }
+    
+    func manifest() -> [String:AnyObject] {
+        let dict: [String:AnyObject] = [
+            "Name" : name,
+            "Version" : version,
+            ]
+        return dict
+    }
+    
+    func writeManifest() throws {
+        let at = "./Contents/Chapters/\(chapterName).playgroundchapter/Pages/\(name).playgroundpage/"
+        do {
+            try FileManager.default().createDirectory(atPath: at, withIntermediateDirectories: true, attributes: nil)
+        } catch {
+            print(error)
+        }
+        let nsdictionary = manifest() as NSDictionary
+        if !nsdictionary.write(toFile: at + "/Manifest.plist", atomically: false) {
+            throw NSError.error(description: "Page : Can not write plist.")
+        }
+    }
 }
 
 func bookExists(bookName: String) -> Bool {
     return FileManager.default().fileExists(atPath: "./\(bookName).playgroundbook")
+}
+
+func loadBook() throws -> Book {
+    let bookManifestPath = "./Contents/Manifest.plist"
+    if FileManager.default().isReadableFile(atPath: bookManifestPath) {
+        // open
+        guard let dict = NSDictionary(contentsOfFile: bookManifestPath) else { throw NSError.error(description: "Can not parse Manifest.plist.") }
+        
+        if let name = dict["Name"] as? String,
+            version = dict["Version"] as? String,
+            contentIdentifier = dict["ContentIdentifier"] as? String,
+            contentVersion = dict["ContentVersion"] as? String,
+            deploymentTarget = dict["DeploymentTarget"] as? String {
+            
+            let imageReference = dict["ImageReference"] as? String
+            let chapters = dict["Chapters"] as? [String] ?? [] as [String]
+            
+            return Book(name: name, version: version, contentIdentifier: contentIdentifier, contentVersion: contentVersion, imageReference: imageReference, deploymentTarget: deploymentTarget, chapters: chapters)
+            
+        } else {
+            throw NSError.error(description: "Manifest.plist is deformed.")
+        }
+    } else {
+        throw NSError.error(description: "Can not find Manifest.plist. Moved inside playgroundbook's directory.")
+    }
 }
 
 func createBook(argc: Int, arguments: [String]) throws {

    }
}

func parseArguments() throws {
    var arguments :[String] = ProcessInfo.processInfo().arguments
    let argc = arguments.count - 1
    if argc >= 1 {
        print(arguments.first)
        arguments.removeFirst() // remove own path
        switch arguments[0] {
        case "create":
            do { try createBook(argc: argc, arguments: arguments) } catch { throw error }
        case "chapter":
            // add chapter to a book
            print(argc)
        case "page":
            // add page to a chapter
            print(argc)
        case "rm":
            // remove chapter from book
            // remove page from chapter
            print(argc)
        default:
            print(argc)

    } catch {
        print(error)
    }
}

main()
