#!/usr/bin/env python2

# Rar file filter for Recoll
# Adapted from the Zip archive filter by mroark.
# Copyright (C) 2004 J.F.Dockes + mroark for rar bits
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import sys
import rclexecm
try:
    from rarfile import RarFile
except:
    print "RECFILTERROR HELPERNOTFOUND python:rarfile"
    sys.exit(1);

# Requires RarFile python module. Try "sudo pip install rarfile"
#
# This is identical to rclzip except I did a search/replace from zip
# to rar, and changed this comment.
class RarExtractor:
    def __init__(self, em):
        self.currentindex = 0
        self.em = em

    def extractone(self, ipath):
        #self.em.rclog("extractone: [%s]" % ipath)
        docdata = ""
        isdir = False
        
        try:
            rarinfo = self.rar.getinfo(ipath)
            isdir = rarinfo.isdir()
        except Exception, err:
            self.em.rclog("extractone: getinfo failed: [%s]" % err)
            return (True, docdata, ipath, false)

        if not isdir:
            try:
                if rarinfo.file_size > self.em.maxmembersize:
                    self.em.rclog("extractone: entry %s size %d too big" %
                              (ipath, rarinfo.file_size))
                    docdata = ""
                else:
                    docdata = self.rar.read(ipath)
                ok = True
            except Exception, err:
                self.em.rclog("extractone: failed: [%s]" % err)
                ok = False
        else:
            docdata = ""
            ok = True
            self.em.setmimetype("application/x-fsdirectory")

        iseof = rclexecm.RclExecM.noteof
        if self.currentindex >= len(self.rar.namelist()) -1:
            iseof = rclexecm.RclExecM.eofnext
        if isinstance(ipath, unicode):
            ipath = ipath.encode("utf-8")
        return (ok, docdata, ipath, iseof)

    ###### File type handler api, used by rclexecm ---------->
    def openfile(self, params):
        self.currentindex = -1
        try:
            self.rar = RarFile(params["filename:"])
            return True
        except:
            return False

    def getipath(self, params):
        ipath = params["ipath:"]
        ok, data, ipath, eof = self.extractone(ipath)
        if ok:
            return (ok, data, ipath, eof)
        # Not found. Maybe we need to decode the path?
        try:
            ipath = ipath.decode("utf-8")
            return self.extractone(ipath)
        except Exception, err:
            return (ok, data, ipath, eof)
        
    def getnext(self, params):

        if self.currentindex == -1:
            # Return "self" doc
            self.currentindex = 0
            self.em.setmimetype('text/plain')
            if len(self.rar.namelist()) == 0:
                eof = rclexecm.RclExecM.eofnext
            else:
                eof = rclexecm.RclExecM.noteof
            return (True, "", "", eof)

        if self.currentindex >= len(self.rar.namelist()):
            #self.em.rclog("getnext: EOF hit")
            return (False, "", "", rclexecm.RclExecM.eofnow)
        else:
            ret= self.extractone(self.rar.namelist()[self.currentindex])
            self.currentindex += 1
            return ret

# Main program: create protocol handler and extractor and run them
proto = rclexecm.RclExecM()
extract = RarExtractor(proto)
rclexecm.main(proto, extract)
