A cockerel threatened with an anti-social behaviour order has been handed a night-time curfew.
Charlie will have to be housed in a lightproof shed overnight
The deal was agreed at Scottish Borders District Court as it met to discuss the fate of Charlie - a four-year-old bird from Selkirk.
Neighbours had raised a court action against the cockerel's owners in a bid to curb its early morning crowing.
Charlie will now have to be kept in lightproof accommodation between specified hours every night.
Last year Scottish Borders Council applied to Selkirk Sheriff Court for an anti-social behaviour order on the bird.
Neighbours had complained the cockerel was crowing from 2.30am and exceeding a 30 decibel limit set by World Health Organisation guidelines.
However, the Asbo application was put on hold when neighbours - George MacFarlane and John Emond - raised an action against the bird's owner Kenneth Williamson under the Civic Government (Scotland) Act.
Two days had been set aside to hear the case at district court but a deal was agreed between both parties in the year-long dispute.
It means Charlie will be in lightproof accommodation between 8pm and 9am from 1 May to 30 September and between 7pm and 7am for the rest of the year.
"This has caused both parties a considerable amount of grief for quite a while," said JP Andrew Bramhall.
"Both parties have been looking for this result and it is in all your interests that an agreement has been reached."
However, he warned Mr Williamson that failure to observe the curfew would be dealt with severely.
"To breach an order to allow the cockerels out within these times would be the subject of criminal proceedings," he said.
"It would then become a police matter."
Mr Williamson said he was happy to accept the court agreement
Mr Williamson has been given four weeks to erect the lightproof sheds required to house his animals.
The 60-year-old said he was happy to go along with the agreement reached in court.
"But I have to say the whole thing has been ridiculous from the start," he said.
"There is more noise from the lorries which go along the road outside our houses than from Charlie.
"You would think I had killed someone by all the fuss that has been caused."
In a joint statement, the neighbours who raised the action said they were pleased with the outcome of the court hearing.
"The reason why we and other neighbours wanted to gain a court order against Mr Williamson to stop unhoused geese and cockerels during the night was that both families have suffered sleep deprivation," it said.
"Members of both families have suffered life-threatening illnesses in which they need sleep to recover and rest.
"Mediation was offered several times to Mr Williamson but was rejected."
Scottish Borders Council's asbo application has been put on hold in the wake of the district court agreement.