/*
 *  Copyright (C) 2014 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.akvo.gae.remoteapi;

import java.io.File;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.repackaged.org.apache.commons.io.FileUtils;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;

public class UserList {

    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            throw new IllegalArgumentException("Missing params");
        }

        final String usr = args[0];
        final String pwd = args[1];
        final List<String> instances = FileUtils
                .readLines(new File("/tmp/instances.txt"));
        final File f = new File("/tmp/device-list.csv");

        FileUtils.write(f, "Instance,Device,Version\n");

        for (String i : instances) {

            final RemoteApiOptions options = new RemoteApiOptions().server(i + ".appspot.com", 443)
                    .credentials(usr, pwd);
            final RemoteApiInstaller installer = new RemoteApiInstaller();
            final StringBuffer sb = new StringBuffer();
            installer.install(options);
            try {
                System.out.println("Processing: " + i);
                DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
                Query q = new Query("User");
                PreparedQuery pq = ds.prepare(q);
                int j = 1;
                for (Entity e : pq.asIterable()) {

                    if (j % 100 == 0) {
                        System.out.println(".");
                    } else {
                        System.out.print(".");
                    }

                    sb.append(
                            String.format("%s,%s,%s", i, e.getProperty("emailAddress"),
                                    getRole((String) e.getProperty("permissionList"))))
                                    .append("\n");
                    j++;
                }
                FileUtils.write(f, sb.toString(), true);
            } finally {
                installer.uninstall();
            }
            System.out.println("\n");
        }

    }

    private static String getRole(String value) {
        if ("0".equals(value)) {
            return "SUPER_ADMIN";
        }
        if ("10".equals(value)) {
            return "ADMIN";
        }
        if ("20".equals(value)) {
            return "USER";
        }
        return value;
    }
}

@@ -1373,17 +1373,17 @@
 ngth != 
-2
+1
 ) %7B%0A    
@@ -1467,71 +1467,174 @@
     
-final String usr = args%5B0%5D;%0A        final String pwd = args%5B1%5D;
+StringBuilder configFolder = new StringBuilder(args%5B0%5D.trim());%0A        if (!configFolder.toString().endsWith(%22/%22)) %7B%0A            configFolder.append(%22/%22);%0A        %7D%0A
 %0A   
@@ -1888,25 +1888,198 @@
 nstances) %7B%0A
+            final String serviceAccount = %22sa-%22 + i + %22@%22 + i + %22.iam.gserviceaccount.com%22;%0A            final String serviceAccountKey = configFolder + i + %22/%22 + i + %22.p12%22;
 %0A
-
             
@@ -2191,28 +2191,69 @@
    .
-credentials(usr, pwd
+useServiceAccountCredential(serviceAccount, serviceAccountKey
 );%0A 

Refactor `UserList` generation script to use service accounts