Help for understanding this random variable derivation I can't decypher how the author comes to this solution, here is the problem

The cost of manufacturing a certain book is \$3 per book up to
  1000 copies, \$2 per copy between 1000 and 5000 copies, and \$1 per copy afterwards. In reality of course books
  are printed in round lots and not on demand "as you go." What we
  assume here is tantamount to selling all overstock at cost, with no
  loss of business due to understock. Suppose we print 1000 copies
  initially and price the book at $5. Let X be the number of copies
  that will be sold. It should evident that once X is known, we can
  compute the profit or loss from the sales, call this Y. The
  formula connecting Y with X is given below


I dont understand the derivations to come out with this random variable Y, if anybody can provide me a step by step, I will very gracefuly.
 A: First interval
The revenue for $0$ to $1000$ books is $5\cdot X$. The costs are $3\cdot 1000=3000 $ because $1000$ books has to be printed at most. Thus the profit function for the first interval is $5\cdot X-3000 \quad \texttt{if} \ \ X\leq 1000$
Second interval
To evaluate the profit for the second interval we have first to calculate the profit at $X=1000$ (upper limit of first interval): $5\cdot 1000-3000=2000$.
Every additional book above $X=1000$ has a cost of $2$ and a selling price of $5$. Therefore the profit for each book is $3(=5-2)$.
From the $1001st$ to the $5000th$ book each book creates 3 dollars additional profit. If we have, for instance, $1001$ books in total $1000$ books have to be substracted because they have been regarded in the cost of the upper bound of the first interval, which is $2000$. The profit function for the interval from 1001 to 5000 becomes
$2000+3\cdot (X-1000) \quad \texttt{if} \ \ 1000<  X\leq 5000$
Third interval
Now we have to calculate the profit of $5000$ books. This is $2000+3\cdot (5000-1000)=14000$. The cost for each book is $1$ dollar. Thus the profit for one book is $4(=5-1)$ dollars. And again we subtract the amount of books which has been regarded in the previous intervals. We substract $5000$ books. The profit function for the third interval is
$14000+4\cdot (X-5000) \quad \texttt{if} \ \ X> 5000$
