First order conditions in a Moral Hazard problem Consider a principal-agent problem where effort is observable. Let $\pi$ denote profit, let $w$ denote the wage to be paid to the agent, and let $f(\pi|e)$ denote the pdf of the profit, conditional on effort $e$. Further, $e \in \{e_L,e_H\}$. The problem for the principal is then as follows: For each level of effort, $e$: $$ \min_w\int w(\pi) f(\pi|e)\, d\pi$$ subject to $$\int v(w(\pi)) f(\pi|e)\,d\pi - g(e) \ge \overline u$$
(Here, $v$ is the utility function for the agent, $g$ is the cost function of effort and $\overline u$ is the outside option for the agent). Then, choose $e$ that gives maximum profit given the wage $w$, which has now been solved for at each effort level.

I'm not sure how to take the first order conditions in the minimisation problem above. If $w$ were the argument for the integral, it'd be manageable.
Notice that MWG choose a wage function $w(\pi)$ to maximize profit, not a wage level $w$. As footnote 6 on p.481 of MWG says:


  The first-order condition for $w(\pi)$ is derived by taking the derivative with respect to the manager's wage at each level of $\pi$ separately.


In other words, you'd treat the wage function as a variable, e.g. $w(\pi)=z$, and solve 
\begin{equation}
\max_{z} \int [\pi-z]f(\pi|e)\mathrm d\pi +\gamma \left[\int v(z)f(\pi|e)\mathrm d\pi-g(e)-\overline u\right],
\end{equation}
which will yield the FOC 
\begin{equation}
-f(\pi|e)+\gamma v'(z)f(\pi|e)=0. 
\end{equation}
Substituting $z$ with $w(\pi)$, you'd have condition (14.B.3) in MWG.