package co.ootf.garmin;

import javastrava.api.v3.auth.AuthorisationService;
import javastrava.api.v3.auth.impl.retrofit.AuthorisationServiceImpl;
import javastrava.api.v3.auth.model.Token;
import javastrava.api.v3.model.StravaActivity;
import javastrava.api.v3.model.StravaUploadResponse;
import javastrava.api.v3.service.Strava;
import javastrava.api.v3.service.exception.BadRequestException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.Arrays;

public class GarminUploadApp {

    private static final Log LOG = LogFactory.getLog(GarminUploadApp.class);
    public static final boolean PRIVATE_DEBUG = true;
    public static final String DATA_TYPE = "fit";

    private static PropertiesService propertiesService = new PropertiesService();

    public static void main(String[] args) {
        File activitiesFolder = null;
        String deviceName = null;
        if (args.length > 0) {
            String path = args[0];
            if (StringUtils.isEmpty(path)) {
                LOG.error("No path specified");
                System.exit(-1);
            }
            File devicePathFile = new File(path);
            deviceName = devicePathFile.getName();
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            activitiesFolder = new File(path + propertiesService.getProperties().getProperty(PropertiesService.DEVICE_ACTIVITIES + deviceName));
        }


        LOG.info(deviceName);

        Token token = null;

        int clientId = Integer.parseInt(propertiesService.getProperties().getProperty(PropertiesService.API_CLIENTID));
        String secret = propertiesService.getProperties().getProperty(PropertiesService.API_SECRET);
        String code = propertiesService.getProperties().getProperty(PropertiesService.API_CODE);

        try {

            AuthorisationService service = new AuthorisationServiceImpl();
            token = service.tokenExchange(clientId, secret, code);
        } catch (BadRequestException e) {
            LOG.error("Please register a Strava Application at \"https://www.strava.com/settings/api\"", e);
            System.exit(-1);
        }

        Strava strava = new Strava(token);
        int fileCount = 0;
        for (File file : activitiesFolder.listFiles()) {

            LOG.info("Attempting to upload file #" + fileCount++ + " for device " + deviceName);

            final StravaUploadResponse uploadResponse = strava.upload(null, null, null, PRIVATE_DEBUG, false, DATA_TYPE, null, file);

            LOG.debug("*** UPLOAD RESPONSE: " + uploadResponse.getStatus());
            LOG.debug("*** UPLOAD ERROR: " + uploadResponse.getError());

            if (uploadResponse.getError() == null) {
                LOG.info("Upload appears to have been successful, deleting file.");
                file.delete();
            }

        }

    }

}

@@ -187,55 +187,8 @@
 en;%0A
-import javastrava.api.v3.model.StravaActivity;%0A
 impo
@@ -236,16 +236,16 @@
 sponse;%0A
+
 import j
@@ -496,33 +496,8 @@
 ile;
-%0Aimport java.util.Arrays;
 %0A%0Apu
@@ -530,85 +530,8 @@
  %7B%0A%0A
-    private static final Log LOG = LogFactory.getLog(GarminUploadApp.class);%0A
     
@@ -630,16 +630,92 @@
  %22fit%22;%0A
+    private static final Log LOG = LogFactory.getLog(GarminUploadApp.class);
 %0A    pri
@@ -2256,16 +2256,103 @@
 nt = 0;%0A
+        if (activitiesFolder != null %7C%7C activitiesFolder.listFiles().length %3C 1) %7B%0A    
         
@@ -2397,24 +2397,28 @@
 Files()) %7B%0A%0A
+    
             
@@ -2515,16 +2515,20 @@
         
+    
 final St
@@ -2646,32 +2646,36 @@
 );%0A%0A            
+    
 LOG.debug(%22*** U
@@ -2719,24 +2719,28 @@
 tStatus());%0A
+    
             
@@ -2809,24 +2809,28 @@
             
+    
 if (uploadRe
@@ -2854,24 +2854,28 @@
  == null) %7B%0A
+    
             
@@ -2962,16 +2962,20 @@
         
+    
 file.del
@@ -2997,21 +2997,112 @@
     
+ 
-%7D%0A%0A        %7D%0A
+   %7D%0A%0A            %7D%0A%0A        %7D else %7B%0A            LOG.info(%22No files found on this device.%22);%0A        %7D
 %0A   

Delete file on error free.