#!/usr/bin/python

import sys, zipfile
import getopt
from urlparse import urlparse
from urlparse import unquote

opt,par = getopt.getopt(sys.argv[1:],'-s:')
uri = urlparse(par[0])
if uri.scheme != "file":
   sys.exit(1)
   
inpfile = unquote(uri.path)
outfile = par[1]

try:
	zfile=zipfile.ZipFile(inpfile)
	files=zfile.namelist()
	# check for meta-file if it's really a FreeCAD document
	if files[0] != "Document.xml":
		sys.exit(1)

	image="thumbnails/Thumbnail.png"
	if image in files:
		image=zfile.read(image)
	else:
		#freecad=open("/usr/share/freecad/freecad-doc.png")
		#image=freecad.read()
		sys.exit(1)

	thumb=open(outfile,"wb")
	thumb.write(image)
	thumb.close()

except:
	sys.exit(1)

