The Scottish Labour leadership contest is nearing end after a long and ill-tempered campaign.

The two candidates, Anas Sarwar and Richard Leonard, will learn on Saturday morning which of them has triumphed.

It has at times been a bitterly fought contest and at times a personal one, too.

The contest was triggered by the surprise resignation of Kezia Dugdale in August after just two years in the job.

Dugdale followed a path now well trodden by her predecessors of a short and electorally unsuccessful stint as Scottish Labour leader.

During the 36-year-old's time at the top of the party, Labour crashed to third place behind both the SNP and the Conservatives at Holyrood and Westminster.

Her period in charge also coincided with the rise of Jeremy Corbyn to the top of the UK party, a relationship which was frosty from the outset.

Labour went from a single MP to seven in Scotland at June's snap general election and many commentators pointed to Corbyn's rising popularity as the reason behind it.

For many in the party, how the new leader intends to interact with Corbyn is one of the central questions which must be answered.

It is one that haunted Sarwar wherever he went on the campaign trail.

Sarwar was one of 13 Labour MSPs in June last year to pen a joint letter to Corbyn calling on him to "do the right thing" and consider his position as leader.

A little over a year later Sarwar's position on the UK Labour leader had changed and he said the entire party was "united in our desire to elect Jeremy Corbyn as our next Prime Minister".

It is up to Labour members to decide if it was the zeal of the convert shining through Sarwar as he continually praised Corbyn during the campaign or was he simply on political manoeuvres?

From the very beginning Sarwar was under fire.

His changing position on Corbyn was not the only issue for many on the left of the party.

The business practices of his father's multi million-pound wholesale business and his decision to send his children to a private school also became campaign issues.

The Sarwar family fortune came under scrutiny on two counts. Firstly, the firm did not recognise trade unions and, secondly, it did not pay the living wage to all of its employees.

In a bid to draw a line under the questions, Sarwar relinquished all of his shares (estimated to be worth £4.8m) in the firm to a trust that can only be accessed by his children once they reach adulthood.

"I will never apologise for being my father's son," he said.

"I am incredibly proud of his achievements, building a company from scratch than now employs around 250 workers - many in Nicola Sturgeon's own constituency."

The Labour Party and the labour movement are not just linked by their names and ideals, they are linked by the ability of trade union members to vote in the contest.

These votes could swing the contest. If they do, it is likely they will swing it in favour of Leonard.

Although he is not a household name Leonard is well known figure in the Labour party.

The 55-year-old spent decades in the trades union movement before he was elected to the Scottish Parliament last year.

His commitment to its beliefs, and his connections to those at the helm of the organisations, helped him win the support of almost all of the country's major unions.

It was the endorsement of Unite the Union, the nation's largest, that led to Scottish Labour MP Ian Murray claiming the vote was being "rigged" in favour of the left-wing candidate.

Unite sent out a mass text to all of its members asking them if they wished to vote in the contest.

The message failed to ask if they supported the party, potentially handing a ballot paper to those who wish to harm Labour in some way.

Party officials ruled "the sign-ups are eligible and within the rules of the leadership contest" while Unite called Murray's complaint a "baseless smear".

"I am immensely pleased to have the backing of Unite," said Leonard.

"Unite members in many industries and services have been at the sharp end of austerity. I look forward to working with them and for them in tackling austerity and building a Scotland for the many not the few."

The divisions inside Scottish Labour spilled into the public sphere after the party's then-interim leader Alex Rowley was secretly recorded backing Leonard and alluding to a previous plot to oust Dugdale.

The recording was made while Rowley was in Brighton for his party's annual conference.

On his return to Holyrood, the MSP said he was "gutted" at the tape's publication after he pledged to remain neutral during the election.

Fellow Labour MSP Jackie Baillie and now interim leader, who is a supporter of Sarwar, accused him of "hypocrisy".

She added "the revelations about a plot against Kezia Dugdale are incredibly disappointing".

In response to Baillie, a press officer for Leonard sent an on the record statement to a journalist via email with the message entitled: "coment [sic] on latest jackie baillie p**h".

Stephen Low, the volunteer press officer in question, left the campaign soon after.

The embarrassing episode was too much for Labour's rivals to ignore.

"We fight for Scotland, Scottish Labour just fights amongst themselves," the First Minister told MSPs.

"I mean yesterday, it was incredible yesterday wasn't it, we had Richard Leonard accused by Jackie Baillie of betraying every value that Labour holds dear.

"And then we had Richard Leonard saying this was just the latest Jackie Baillie..."

Before she could mention the word in question, the First Minister was interrupted by the presiding officer.

She added: "I can't actually say it presiding officer."

Whoever is elected leader faces a tough ask.

They have to make Scottish Labour look like an alternative government, which is not easy when you are only the third largest party.

Strategically the party is caught in a vice. Too unionist for some independence supporters and not unionist enough for those who switched allegiances to the Conservatives.

And can the new leader conjure some of the Corbyn magic?

Corbyn may not be in Downing Street but June's general election was still a triumph for the party.

Excitement, momentum and enthusiasm for the party's message grew over the campaign.

Also high on the leader's agenda is the internal investigation into Rowley's alleged past misconduct, which he denies.

In many ways this office is the poisoned chalice of Scottish politics. The next incumbent will hope it will be a longer and more successful stint than their predecessor's.