If any person other than a minor in confinement, under indictment, for want of bail for good behavior, for keeping the peace or appearing as a witness, in consequence of any summary conviction appears to be insane, the judge of the circuit court of the county where he is confined must institute a careful investigation, call a respectable physician and call other credible witnesses; and, if he deems it necessary, he may call a jury, for which purpose he is empowered to compel attendance of witnesses and jurors. If it is satisfactorily proved that the person is insane, the judge may discharge him from imprisonment and order his safe custody and removal to the Alabama state hospitals, where he must remain until restored to his right mind, and then, if the judge shall have so directed, the superintendent must inform the judge and sheriff, whereupon the person must be remanded to jail and criminal proceedings be resumed, or he must be otherwise discharged.