/**
     * Open a named physical or virtual file, extract the text from it, search
     * for document or page attachments, and process these recursively. Either
     * filename must be supplied for physical files, or data+length from which a
     * virtual file will be created. The caller cannot create the PVF file since
     * we create a new TET object here in case an exception happens with the
     * embedded document - the caller can happily continue with his TET object
     * even in case of an exception here.
     *
     * @param outfp
     * @param filename
     * @param realname
     * @param data
     *
     * @return 0 if successful, otherwise a non-null code to be used as exit
     *         status
     */
    static int process_document(BinaryWriter outfp, String filename, String realname,
                                byte[] data)
    {
        int retval = 0;
        TET tet    = null;

        try
        {
            String pvfname = "/pvf/attachment";

            tet = new TET();

            /*
             * Construct a PVF file if data instead of a filename was provided
             */
            if (filename == null || filename.Length == 0)
            {
                tet.create_pvf(pvfname, data, "");
                filename = pvfname;
            }

            tet.set_option(globaloptlist);

            int doc = tet.open_document(filename, docoptlist);

            if (doc == -1)

            {
                Console.WriteLine("Error " + tet.get_errnum() + " in  "
                                  + tet.get_apiname() + "() (source: attachment '"
                                  + realname + "'): " + tet.get_errmsg());

                retval = 5;
            }
            else
            {
                process_document(outfp, tet, doc);
            }

            /*
             * If there was no PVF file deleting it won't do any harm
             */
            tet.delete_pvf(pvfname);
        }
        catch (TETException e)
        {
            Console.WriteLine("Error " + e.get_errnum() + " in  "
                              + e.get_apiname() + "() (source: attachment '" + realname
                              + "'): " + e.get_errmsg());
            retval = 1;
        }
        catch (Exception e)
        {
            Console.WriteLine("General Exception: " + e.ToString());
            retval = 1;
        }
        finally
        {
            if (tet != null)
            {
                tet.Dispose();
            }
        }


        return(retval);
    }