private function setup()
 {
     global $scratchDirectory, $workingEmailAddress;
     $this->here = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}";
     $state = null;
     $this->start_date = null;
     $this->end_date = null;
     if (isset($_POST["start_date"]) && $_POST["start_date"] != '') {
         $this->start_date = strtotime($_POST["start_date"]);
     }
     if (isset($_POST["end_date"]) && $_POST["end_date"] != "") {
         $this->end_date = strtotime($_POST["end_date"] . " 23:59:59");
     }
     if (array_key_exists("tz", $_POST)) {
         $this->preferences->setTimezone($_POST["tz"]);
     }
     if (array_key_exists("main_form", $_POST)) {
         $this->preferences->setSplitRides(array_key_exists("split_rides", $_POST));
         if (array_key_exists("elevation_units", $_POST)) {
             $this->preferences->setMclUseFeet(true);
             $this->myCyclingLog->setUseFeetForElevation(true);
         } else {
             $this->preferences->setMclUseFeet(false);
             $this->myCyclingLog->setUseFeetForElevation(false);
         }
     }
     if (array_key_exists("clear_cookies", $_POST)) {
         $this->preferences->clear();
         unset($_GET["code"]);
         unset($_GET["state"]);
     }
     $this->myCyclingLog->setUseFeetForElevation($this->preferences->getMclUseFeet());
     $this->endomondo->setSplitOvernightRides($this->preferences->getSplitRides());
     $this->rideWithGps->setSplitOvernightRides($this->preferences->getSplitRides());
     $this->strava->setSplitOvernightRides($this->preferences->getSplitRides());
     $this->strava->setWriteScope($this->preferences->getStravaWriteScope());
     if (array_key_exists("login_mcl", $_POST)) {
         $mcl_username = $_POST['username'];
         $mcl_password = $_POST['password'];
         $auth = base64_encode("{$mcl_username}:{$mcl_password}");
         $this->myCyclingLog->setAuth("{$auth}");
         if ($this->myCyclingLog->isConnected()) {
             $this->preferences->setMclAuth($auth);
             $this->preferences->setMclUsername($_POST['username']);
         } else {
             $this->error_message = "There was a problem connecting to MyCyclingLog, please try again";
         }
     } else {
         if ($this->preferences->getMclAuth()) {
             $this->myCyclingLog->setAuth($this->preferences->getMclAuth());
         }
     }
     if (array_key_exists("login_endo", $_POST)) {
         $endo_username = $_POST['username'];
         $endo_password = $_POST['password'];
         $auth = $this->endomondo->connect($endo_username, $endo_password);
         if ($this->endomondo->isConnected()) {
             $this->preferences->setEndoAuth($auth);
         } else {
             $this->error_message = "There was a problem connecting to Endomondo, please try again.<br>(" . $this->endomondo->getError() . ")";
         }
     } else {
         if ($this->preferences->getEndoAuth() != null) {
             $this->endomondo->setAuth($this->preferences->getEndoAuth());
         }
     }
     if (array_key_exists("login_rwgps", $_POST)) {
         $rwgps_username = $_POST['username'];
         $rwgps_password = $_POST['password'];
         $auth = $this->rideWithGps->connect($rwgps_username, $rwgps_password);
         if ($this->rideWithGps->isConnected()) {
             $this->preferences->setRwgpsAuth($auth);
         } else {
             $this->error_message = "There was a problem connecting to RideWithGps, please try again.<br>(" . $this->rideWithGps->getError() . ")";
         }
     } else {
         if ($this->preferences->getRwgpsAuth() != null) {
             $this->rideWithGps->setAuth($this->preferences->getRwgpsAuth());
         }
     }
     if (array_key_exists("state", $_GET)) {
         if (!array_key_exists("error", $_GET) && array_key_exists("code", $_GET)) {
             $code = $_GET["code"];
             $token = $this->strava->setAccessTokenFromCode($code);
             if ($this->strava->isConnected()) {
                 $this->strava->setWriteScope($_GET["state"] == "write");
                 $this->preferences->setStravaWriteScope($this->strava->writeScope());
                 $this->preferences->setStravaAccessToken($token);
             }
         } else {
             $this->error_message .= 'There was a problem connecting to strava, please try again: ' . $_GET["error"] . " ";
         }
         if ($this->strava->getError()) {
             $this->error_message .= 'There was a problem connecting to strava, please try again: ' . $this->strava->getError() . " ";
         }
         unset($_GET["state"]);
         unset($_GET["code"]);
         unset($_GET["error"]);
     } else {
         if ($this->preferences->getStravaAccessToken() != null) {
             $this->strava->setAccessToken($this->preferences->getStravaAccessToken());
         }
     }
     if (array_key_exists("delete_files", $_POST) && $this->strava->isConnected()) {
         $files = scandir($scratchDirectory);
         $pattern = '/^' . $this->strava->getUserId() . "-.*\\.gpx\$/";
         $count = 0;
         foreach ($files as $file) {
             if (preg_match($pattern, $file, $match) > 0) {
                 unlink($scratchDirectory . DIRECTORY_SEPARATOR . $file);
                 $count++;
             }
         }
         $this->output("Deleted {$count} files.<br>");
     }
     if (array_key_exists("calculate_from_strava", $_POST) && $this->strava->isConnected()) {
         $state = "calculate_from_strava";
     } else {
         if (array_key_exists("calculate_from_mcl", $_POST) && $this->myCyclingLog->isConnected()) {
             $state = "calculate_from_mcl";
         } else {
             if (array_key_exists("calculate_from_endo", $_POST) && $this->endomondo->isConnected()) {
                 $state = "calculate_from_endo";
             } else {
                 if (array_key_exists("calculate_from_rwgps", $_POST) && $this->rideWithGps->isConnected()) {
                     $state = "calculate_from_rwgps";
                 } else {
                     if (array_key_exists("copy_strava_to_mcl", $_POST) && $this->myCyclingLog->isConnected() && $this->strava->isConnected()) {
                         $state = "copy_strava_to_mcl";
                     } else {
                         if (array_key_exists("copy_endo_to_strava", $_POST) && $this->endomondo->isConnected() && $this->strava->isConnected()) {
                             $state = "copy_endo_to_strava";
                         } else {
                             if (array_key_exists("copy_endo_to_rwgps", $_POST) && $this->endomondo->isConnected() && $this->rideWithGps->isConnected()) {
                                 $state = "copy_endo_to_rwgps";
                             } else {
                                 if (array_key_exists('copy_rides', $_POST)) {
                                     $state = 'copy_rides';
                                 } else {
                                     if (array_key_exists("delete_mcl_rides", $_POST) && $this->myCyclingLog->isConnected()) {
                                         $state = "delete_mcl_rides";
                                     } else {
                                         if (array_key_exists("queue_delete_endo_from_strava", $_POST) && $this->strava->isConnected() && $this->endomondo->isConnected()) {
                                             $state = "queue_delete_endo_from_strava";
                                         } else {
                                             if (array_key_exists("delete_from_strava", $_POST) && $this->strava->isConnected()) {
                                                 $state = "delete_from_strava";
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (isset($_POST['commentSend'])) {
         mail("{$workingEmailAddress}", "eddington enquiry", $_POST['commentComments'], "From: " . $_POST['commentRealName'] . "<" . $_POST['commentEmail'] . ">\n");
         $this->info_message = "Thanks.  Email sent.  " . " Name: " . $_POST['commentRealName'] . " Email: " . $_POST['commentEmail'] . " Comment: " . $_POST['commentComments'] . "";
     }
     return $state;
 }