echo 'Plist file: ' . $plistfile . '<br />';
 $plistValue = file_get_contents($plistfile);
 if (!empty($plistValue)) {
     $plist = new CFPropertyList();
     $plist->parse($plistValue, CFPropertyList::FORMAT_AUTO);
     $plistData = $plist->toArray();
     //TODO connect to apple and be sure a profile exists for this app.
     //Create an application object and set basic data here
     $entityManager = initDoctrine();
     // Retrieve the developer connected (unique)
     $developer = $entityManager->getRepository('Entities\\Developer')->find($_SESSION['developerId']);
     //verify if the application does not already exist (Ask to create an new version if it exists)
     $application = $entityManager->getRepository('Entities\\Application')->findOneBy(array('bundleId' => $plistData['CFBundleIdentifier']));
     if ($application == NULL) {
         $application = new Application();
         $application->setDeveloper($developer);
         $application->setBundleId($plistData['CFBundleIdentifier']);
         $application->setBundleName($plistData['CFBundleName']);
     }
     //ensure version doesn't already exists.
     //TODO version can be unspefied, handle this case.
     $version = $entityManager->getRepository('Entities\\Version')->findOneBy(array('version' => $plistData['CFBundleVersion']));
     if ($version == NULL) {
         $version = new Version();
         $version->setVersion($plistData['CFBundleVersion']);
         $version->setApplication($application);
     } else {
         // old version will be linked with new data folder using new token,
         // but delete old data folder before
         $versionToken = $version->getToken();
         Tools::deleteVersionDataFiles($versionToken);