If a defendant who has been found to be not competent to stand trial believes that he or she can establish a defense of not guilty to the charges pending against him or her, other than the defense of not guilty by reason of mental illness, the defendant may request an opportunity to offer a defense thereto on the merits before the court which has criminal jurisdiction. If the defendant is unable to obtain legal counsel, the court of record shall appoint counsel for the defendant to assist him or her in supporting the request by affidavit or other evidence. If the court of record in its discretion grants such a request, the evidence of the defendant and of the state shall be heard by the court of record sitting without a jury. If after hearing such petition the court of record finds insufficient evidence to support a conviction, it shall dismiss the indictment and order the release of the defendant from criminal custody. The release order, however, may be stayed for ten days to allow civil commitment proceedings to be instituted by the prosecutor pursuant to article five of this chapter: Provided, That a defendant committed to a mental health facility pursuant to subsection (f) or (h), section three of this article shall be immediately released from the facility unless civilly committed.