If, at the time of the application for a writ or order for the production of the infant, the infant is being brought up by another person, or by an institution, the court to which the application is made may in its discretion, if it orders the infant to be given up to the parent, or to any other person at law liable to maintain the infant or entitled to his custody, further order that the person to whom the infant is to be given up shall pay to the person who or the institution which has been bringing up the infant the whole of the costs, charges and expenses properly incurred in bringing up the infant or such portion thereof as seems to the court to be just and reasonable having regard to all the circumstances of the case.