Merging 3 sketches NOOB ALERT!

However, now the MOSFET (switching a motor) starts up and just continues running..

Does your MOSFET really have legs? That move well enough for it to run?

Motors run. MOSFETs do not.

  pir = digitalRead(pirPin);  // read input value

Why is pir global? What other function needs access to the value in pir?

What IS pri, if not the state of the PIR? Why do you have another variable called pirState?

I KNOW the answer to that is that you didn't write the code, but I want you to THINK about why that useless variable exists. If you can't figure out why, GET RID OF IT.