In order to simplify the problem and since your are getting the variable sitename, why don't you read a username variable?
With that you'd make sure that the script execution is not dependent on the environmental variables made available the way the script is executed.
Hope this alternative helps you make it work the way you want.
Btw, why the two shebangs in that script? :)