(1) If at any time prior to trial it appears that the accused has become mentally incompetent to stand trial, such disability may be called to the attention of the district or county court by the county attorney or city attorney, by the accused, or by any person for the accused. The judge of the district or county court of the county where the accused is to be tried shall have the authority to determine whether or not the accused is competent to stand trial. The judge may also cause such medical, psychiatric, or psychological examination of the accused to be made as he or she deems warranted and hold such hearing as he or she deems necessary. The cost of the examination, when ordered by the court, shall be the expense of the county in which the crime is charged. The judge may allow any physician, psychiatrist, or psychologist a reasonable fee for his or her services, which amount, when determined by the judge, shall be certified to the county board which shall cause payment to be made. Should the judge determine after a hearing that the accused is mentally incompetent to stand trial and that there is a substantial probability that the accused will become competent within the foreseeable future, the judge shall order the accused to be committed to a state hospital for the mentally ill or some other appropriate state-owned or state-operated facility for appropriate treatment until such time as the disability may be removed.
(2) Within six months after the commencement of the treatment ordered by the district or county court, and every six months thereafter until either the disability is removed or other disposition of the accused has been made, the court shall hold a hearing to determine (a) whether the accused is competent to stand trial or (b) whether or not there is a substantial probability that the accused will become competent within the foreseeable future.
(3) If it is determined that there is not a substantial probability that the accused will become competent within the foreseeable future, then the state shall either (a) commence the applicable civil commitment proceeding that would be required to commit any other person for an indefinite period of time or (b) release the accused. If during the period of time between the six-month review hearings set forth in subsection (2) of this section it is the opinion of the Department of Health and Human Services that the accused is competent to stand trial, the department shall file a report outlining its opinion with the court, and within twenty-one days after such report being filed, the court shall hold a hearing to determine whether or not the accused is competent to stand trial. The state shall pay the cost of maintenance and care of the accused during the period of time ordered by the court for treatment to remove the disability.