A former Medical University of South Carolina surgical resident is out of jail after pleading guilty Thursday to a first-degree harassment charge stemming from accusations that he stalked a 17-year-old woman who rejected his romantic advances.

Karl Ehrens, 32, of Charleston, appeared before Charleston County Circuit Judge Stephanie McDonald, who accepted the terms of a negotiated plea struck between Ehrens and prosecutors. The deal meant Ehrens would be sentenced to 495 days, and be given credit for the time he’s already served — 495 days.

The maximum sentence for the charge would have only kept Ehrens in jail for another two months, a three-year term, so prosecutors agreed to this sentence since Ehrens was accepting responsibility by pleading guilty, according to 9th Circuit Assistant Solicitor Amy Harrell.

Ehrens reportedly started hounding the victim and exhibiting bizarre behavior in 2009 after she rejected his romantic overtures. She told authorities that Ehrens, who was 29 at the time, kept bothering her after she got a restraining order.

The woman, now 21, did not appear in court for Ehrens’ guilty plea, but her mother and stepfather were there on her behalf. Her mother told McDonald this entire case has been “a full-blown nightmare.”

As part of the sentence, McDonald ordered Ehrens to avoid all contact with the victim and her family,

Ehrens recently spent 60 days at a state psychiatric hospital in an effort to “attain competence,” according to a previous order by McDonald.

Ehrens’ attorneys told McDonald that despite variations in several evaluations throughout the course of this case, a final evaluation showed Ehrens was mentally competent. Ehrens told judge he is now taking anti-psychotic medication.

One of his attorneys, Christopher Adams, said during the hearing that Ehrens wants to put the episode behind him.

Shackled and wearing a jail jumpsuit, Ehrens was soft-spoken and kept his head down for most of the proceeding, answering McDonald’s questions with “Yes, your honor” or “No, your honor.”

Several sheriff’s deputies stood nearby while Ehrens entered his plea.

In July 2012, Ehrens caused a stir by sending odd emails to several people, including court officials, and mailing a box containing a dozen Care Bears to the Clerk of Court. After arriving back in town from Massachusetts where he was staying with family, he was taken into custody by a SWAT team working with crisis counselors.

Ehrens later wrote prosecutors indicating that he wanted to call the alleged victim in his case to testify as a witness for the defense. He also told people that he wanted to ask her to marry him, according to sources familiar with the case.

A doctor who evaluated him July 25, 2012 had determined that he was mentally ill and posed “a substantial risk of physical harm to himself or others,” according to a motion by prosecutors. That prompted McDonald’s initial order to place him in the Columbia facility.

Reach Natalie Caula at 937-5594 or Twitter.com/ncaula.