Section 18. (a) If the person in charge of any place of detention within the commonwealth has reason to believe that a person confined therein is in need of hospitalization by reason of mental illness at a facility of the department or at the Bridgewater state hospital, he shall cause such prisoner to be examined at such place of detention by a physician or psychologist, designated by the department as qualified to perform such examination. Said physician or psychologist shall report the results of the examination to the district court which has jurisdiction over the place of detention or, if the prisoner is awaiting trial, to the court which has jurisdiction of the criminal case. Such report shall include an opinion, with reasons therefore, as to whether such hospitalization is actually required. The court which receives such report may order the prisoner to be taken to a facility or, if a male, to the Bridgewater state hospital to be received for examination and observation for a period not to exceed thirty days. After completion of such examination and observation, a written report shall be sent to such court and to the person in charge of the place of detention. Such report shall be signed by the physician or psychologist conducting such examination, and shall contain an evaluation, supported by clinical findings, of whether the prisoner is in need of further treatment and care at a facility or, if a male, the Bridgewater state hospital by reason of mental illness. The person in charge of the place of detention shall have the same right as a superintendent of a facility and the medical director of the Bridgewater state hospital to file a petition with the court which received the results of the examination for the commitment of the person to a facility or to the Bridgewater state hospital; provided, however, that, notwithstanding the court's failure, after an initial hearing or after any subsequent hearing, to make a finding required for commitment to the Bridgewater state hospital, the prisoner shall be confined at said hospital if the findings required for commitment to a facility are made and if the commissioner of correction certifies to the court that confinement of the prisoner at said hospital is necessary to insure his continued retention in custody. An initial court order of commitment issued subject to the provisions of this section shall be valid for a six-month period, and all subsequent commitments during the term of the sentence shall take place under the provisions of sections seven and eight and shall be valid for one year.
(b) Notwithstanding any contrary provision of general or special law, a prisoner who is retained in any place of detention within the commonwealth and who is in need of care and treatment in a facility may, with the approval of the person in charge of such place of detention apply for voluntary admission under the provisions of paragraph (a) of section ten.
(c) At the commencement of hospitalization under the provisions of paragraph (a) or paragraph (b) the department of correction shall enter in the patient record of such prisoner the date of the expiration of the sentence of the prisoner. Where applicable, the provisions of sections one hundred and twenty-nine, one hundred and twenty-nine A, one hundred and twenty-nine B and one hundred and twenty-nine C of chapter one hundred and twenty-seven may be applied to reduce such sentence, and on such date the prisoner shall be discharged; provided, however, that if the superintendent or other head of a facility or the medical director of the Bridgewater state hospital determines that the discharge of the prisoner committed subject to the provisions of paragraph (a) would create a likelihood of serious harm by reason of mental illness, he shall petition the district court having jurisdiction over the facility prior to the date of expiration to order the commitment of such person to a facility or to the Bridgewater state hospital under the provisions of this chapter other than paragraph (a); and provided, further, that any prisoner resident in a facility subject to the provisions of paragraph (b) shall be free to leave such facility subject to the provisions of section eleven.
(d) In the event the provisions of this chapter require the release of a prisoner from a facility or from the Bridgewater state hospital prior to the date of expiration of his sentence calculated under the provisions of paragraph (c), such prisoner shall be forthwith returned to the place of detention from which he was transferred to such facility or to said hospital.