Optimal light storage with full pulse-shape control