I have a code that looks like the following:
\documentclass{scrbook}
\usepackage[automark]{scrpage2}
\usepackage{lipsum}
\let\oldchapter\chapter
\newcommand\temphead{}
\newcommand\chaphead{}
\renewcommand\chapter[2][\temphead]{%
\renewcommand\temphead{#2}%
\renewcommand\chaphead{#2}%
\oldchapter[#1]{#2}}
\title{My title}
\makeatletter
\let\titlehead\@title
\makeatother
\cfoot[\rule{6mm}{0.8pt}\\\pagemark]{\rule{6mm}{0.8pt}\\\pagemark}
\ofoot[]{}
\lehead[]{}
\cehead[]{\titlehead}
\cohead[]{\chaphead}
\ohead[]{}
\renewcommand*{\chapterpagestyle}{scrplain}
\begin{document}
\pagestyle{scrheadings}
\chapter{My first chapter}
\lipsum
\lipsum
\chapter{My second chapter}
\end{document}
The goal is to display the chapter name on right pages, and the title of the book of left pages.
Now when the page preceding a new chapter is a right page, the chapter displayed is the one of the following chapter. My sample code shows it, as the last page of the first chapter says "My second chapter".
How can I fix this?