If no legal cause is shown for the imprisonment or restraint, or for its continuation, the court shall discharge the party from the custody or restraint under which the party is held or grant any other appropriate remedy.