In any criminal proceeding no person shall be excused from testifying or from producing documentary or other evidence upon the ground that such testimony or evidence may criminate or tend to criminate him if the court in which he is examined is of the opinion that the ends of justice may be promoted by compelling such testimony or evidence. And if, but for this section, the person would have been excused from so testifying or from producing such evidence, then if the person is so compelled to testify or produce other evidence and if such testimony or evidence is self-criminating, such self-criminating testimony or evidence shall not be used or receivable in evidence against him in any proceeding against him thereafter taking place other than a prosecution for perjury in the giving of such evidence, and the person so compelled to testify or furnish evidence shall not be prosecuted for the offense in regard to which he is so compelled to testify or furnish evidence, and he shall have complete legal immunity in regard thereto.