From 5c89a5fa92027527d5d3dbed80f2db574d4baa26 Mon Sep 17 00:00:00 2001
From: Alex Yatskov <alex@foosoft.net>
Date: Sat, 15 Dec 2018 18:00:49 -0800
Subject: [PATCH] add extraction script, fix bug with having too many file
 handles opened

---
 extract.sh       | 2 ++
 tools/mpq/mpq.go | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
 create mode 100755 extract.sh

diff --git a/extract.sh b/extract.sh
new file mode 100755
index 0000000..210d714
--- /dev/null
+++ b/extract.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/sh
+go run ./tools/mpq/*.go --target="./asset" extract ./mpq/*.mpq
diff --git a/tools/mpq/mpq.go b/tools/mpq/mpq.go
index db2b2ef..4e1dc1e 100644
--- a/tools/mpq/mpq.go
+++ b/tools/mpq/mpq.go
@@ -76,11 +76,13 @@ func extract(mpqPath, filter, targetDir string) error {
 		if err != nil {
 			return err
 		}
-		defer sysFile.Close()
 
 		if _, err := io.Copy(sysFile, resFile); err != nil {
+			sysFile.Close()
 			return err
 		}
+
+		sysFile.Close()
 	}
 
 	return nil