Shepton Mallet Prison, which dates back to 1610, had long faced an uncertain future - until now