Our greatest president was also our most spiritually tortured. The carnage of the Civil War, by far the deadliest in American history before or since, harrowed Abraham Lincoln’s soul.

It was 150 years ago today that he shared with the nation the bittersweet fruits of his struggle, in his Second Inaugural Address.

Lincoln’s speech was in fact a sermon, delivered on the East Portico of the Capitol.

In 700 densely-packed words he outlined the roles of God and men in history: God ruled history and judged it; at the same time, Americans were duty-bound, under God’s guidance, to make their country a better place.

Lincoln hadn’t always been much concerned with God. As a young man he admired the skeptic Thomas Paine.

Paine’s “Age of Reason” savaged Christianity, especially the belief that Jesus died to atone for men’s sins:

“The Christian story of God the Father putting his son to death, or employing people to do it . . . cannot be told by a parent to a child; and to tell him that it was done to make mankind happier and better is making the story still worse, as if mankind could be improved by the example of murder.”

Young Lincoln was so impressed, he wrote a Paine-ite pamphlet of his own, and planned to have it printed — until Samuel Hill, an older friend, burned it.

He began writing about religion in September 1862, after two Union losses in the Seven Days’ Battle and the Second Battle of Bull Run. Trying to find a meaning in these bloody debacles, Lincoln laid out his thoughts in a private memo.

Each side in the war claimed to be on God’s side, the president noted. But both couldn’t be right, since “God cannot be for, and against the same thing at the same time.”

Perhaps God favored neither side, but “wills this contest, and wills that it shall not end yet.”

But why would God want the war to drag on? Lincoln began to answer in an April 1864 letter to Albert Hodges, a Kentucky editor: “God now wills the removal of a great wrong, and wills also that we of the North as well as you of the South, shall pay fairly for our complicity in” it.

That is: God willed the Civil War to abolish slavery, and also to punish all those, South and North, who had profited from it.

Eleven months later, in his Second Inaugural, Lincoln cast these thoughts in sonorous prose.

“Fondly do we hope — fervently do we pray — that this mighty scourge of war may speedily pass away. Yet, if God wills that it continue, until all the wealth piled by the bond-man’s 250 years of unrequited toil shall be sunk, and until every drop of blood drawn by the lash, shall be paid by another drawn with the sword, . . . so still it must be said ‘the judgments of the Lord, are true and righteous altogether.’ ”

How far Lincoln had moved from Paine, who wouldn’t tell the crucifixion story to a child because it involved a murder.

Now Lincoln’s God exacted the deaths of hundreds of thousands of men in battle to atone for the enslavement of millions throughout American history.

It was a vision of implacable justice. But Lincoln wasn’t done yet.

His closing thought in the Second Inaugural described man’s duties going forward:

“With malice toward none; with charity for all; with firmness in the right, as God gives us to see the right, let us strive on to finish the work we are in; to bind up the nation’s wounds; to care for him who shall have borne the battle, and for his widow, and his orphan — to do all which may achieve and cherish a just, and a lasting peace. . .”

God remained in the picture as a guide. But it was men who had to repair the damage of the past and establish a just future — to do the hard work of mercy.