"""
    MoinMoin - Processor for idl (www.rsinc.com) compatible languages

    INSTALL: create a user without a password e.g. idl_user
             set the rights of su only callable from users of the su group
	     the httpd user account normally wwwrun must be in this group.

             config_dl_vartmp_dir must be writable for this user.

             Check the rights of this user to prevent corruption of your system.


    @copyright: 2004-04-10 by R.Bauer
                code logic taken from latex.py (Won-kyu Park <wkpark@kldp.org>, Benny Siegert, R.Bauer)

    @license: GNU GPL, see COPYING for details.

"""

Dependencies = []

import sys, os, re, sha
from MoinMoin.Page import Page
from MoinMoin.action import AttachFile

config_external_language='idl'
config_external_dl = "/usr/local/bin/"+config_external_language
config_external_convert = "/usr/bin/convert"

config_umask = 022

config_dl_cache_dir = "./data"
config_dl_cache_url = "./data"
config_dl_header = ""
config_dl_vartmp_dir = "./data/tmp"
config_dl_image_type = 'jpg'
# normally I prefer png, but at the moment my standalone wiki server has
# some problems to show them perfect.

config_dl_user ="idl_user"

def process(request, formatter, lines):
    if not config_dl_cache_dir:
    	return
    if lines[0].strip() == "#!DataLanguage":
        del lines[0]

    texstr = '\n'.join(lines).strip()

    imgname = re.sub('\s+', ' ', texstr)
    imgname = sha.new(imgname).hexdigest()

    attdir = config_dl_cache_dir + "/"+config_external_language+"/attachments"
    atturl = config_dl_cache_url + "/"+config_external_language+"/attachments"
    outpath = "%s/%s.%s" % (attdir, imgname,config_dl_image_type)
    outurl = "%s/%s.%s" % (atturl, imgname,config_dl_image_type)
    if not os.path.isdir(attdir):
        os.makedirs(attdir, 0775)

    pagename=formatter.page.page_name

    url=AttachFile.getAttachUrl(pagename,imgname+'.'+config_dl_image_type,request)
    attach_dir=AttachFile.getAttachDir(pagename,create=1)



    if not os.path.isfile(attach_dir+'/'+imgname+'.'+config_dl_image_type):
     if not os.path.exists(outpath):
        vartmp = config_dl_vartmp_dir

        data = open("%s/%s.pro" % (vartmp, imgname), "w")
        data.write("%s\n set_plot,'ps'\n device,file='%s'\n%s\n  device,/close\n  exit" %(
          config_dl_header, imgname+'.ps',texstr))
        data.close()



        cmd = "cd %(vartmp)s;%(dl)s %(options)s %(pro)s ;exit >/dev/null" % {
           "dl": config_external_dl,
           "vartmp": vartmp,
           "options": '<',
           "pro": imgname+'.pro'
        }
        #request.write(cmd)
        data = open("%s/%s.sh" % (vartmp, imgname), "w")
        data.write(cmd)
        data.close()

	cmd = "su %(dl_user)s %(vartmp)s/%(imgname)s" % {
	   "dl_user": config_dl_user,
	   "vartmp":vartmp,
	   "imgname":imgname+".sh"
	}
        #request.write(cmd)

	os.umask(config_umask)
        os.system(cmd)

        os.system("%(convert)s -rotate 90 -crop 0x0 -density 72x72 %(vartmp)s/%(imgname)s.ps %(outpath)s" % {
            "convert": config_external_convert,
            "vartmp": vartmp,
            "outpath": attach_dir+'/'+imgname+'.'+config_dl_image_type,
            "imgname": imgname,
        })

        os.system("rm -f %(vartmp)s/%(imgname)s.*" % {
            "vartmp": vartmp,
            "imgname": imgname,
        })

    request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))



