"use strict";

var fs = require("fs");
var path = require("path");

// compute a release name for S3 based on the version in package.json and
// whether the git HEAD matches the tag for the corresponding version
var config = require("./package.json");
var headPath = path.resolve(__dirname, ".git", "HEAD");
var tagPath = path.resolve(__dirname, ".git", "refs", "tags", "v" + config.version);
var releaseName;
if (fs.existsSync(headPath)) {
    var headHash = fs.readFileSync(headPath, "ascii").trim();
    if (fs.existsSync(tagPath)) {
        var tagHash = fs.readFileSync(tagPath, "ascii").trim();
        if (tagHash === headHash) {
            releaseName = config.version;
        } else {
            releaseName = headHash;
        }
    } else {
        releaseName = headHash;
    }
} else {
    releaseName = "default";
}

module.exports = function (grunt) {
    ["grunt-contrib-uglify",
     "grunt-contrib-clean",
     "grunt-amd-wrap",
     "grunt-global-wrap",
     "grunt-s3"]
        .forEach(grunt.loadNpmTasks);

    grunt.initConfig({
        releaseName: releaseName,
        clean: {
            artifacts: ["release/"]
        },
        amdwrap: {
            artifacts: {
                expand: true,
                src: ["q.js", "queue.js"],
                dest: "release/<%= releaseName %>/amd/"
            }
        },
        globalwrap: {
            artifacts: {
                main: "q.js",
                global: "Q",
                dest: "release/<%= releaseName %>/q.js",

                // don't detect and insert a `process` shim.
                bundleOptions: { detectGlobals: false }
            }
        },
        uglify: {
            artifacts: {
                files: [{
                    expand: true,
                    cwd: "release/",
                    src: ["**/*.js"],
                    dest: "release/",
                    ext: ".min.js"
                }],
                options: {
                    report: "gzip"
                }
            }
        },
        s3: {
            artifacts: {
                options: {
                    bucket: "q-releases",
                    access: "public-read",
                    region: "us-west-1",
                    key: process.env.S3_KEY,
                    secret: process.env.S3_SECRET
                },
                upload: [{
                    // See https://github.com/pifantastic/grunt-s3/issues/91 for
                    // why we use this funky syntax instead of the standard.
                    src: "release/**/*.js",
                    rel: path.basename(path.resolve(__dirname, "release"))
                }]
            }
        }
    });

    grunt.registerTask("pre-browser-test", ["clean", "globalwrap"]);
    grunt.registerTask("release", ["clean", "amdwrap", "globalwrap", "uglify", "s3"]);
};

@@ -397,20 +397,20 @@
  release
-Name
+Path
 ;%0Aif (fs
@@ -649,20 +649,20 @@
  release
-Name
+Path
  = confi
@@ -700,38 +700,51 @@
          release
-Name =
+Path = %22commits/%22 +
  headHash;%0A     
@@ -776,22 +776,35 @@
  release
-Name =
+Path = %22commits/%22 +
  headHas
@@ -832,20 +832,20 @@
  release
-Name
+Path
  = %22defa
@@ -1089,20 +1089,20 @@
  release
-Name
+Path
 : releas
@@ -1102,20 +1102,20 @@
  release
-Name
+Path
 ,%0A      
@@ -1327,28 +1327,28 @@
 /%3C%25= release
-Name
+Path
  %25%3E/amd/%22%0A  
@@ -1522,12 +1522,12 @@
 ease
-Name
+Path
  %25%3E/
@@ -1805,32 +1805,51 @@
   cwd: %22release/
+%3C%25= releasePath %25%3E/
 %22,%0A             
@@ -1862,20 +1862,17 @@
  src: %5B%22
-**/*
+q
 .js%22%5D,%0A 
@@ -1905,16 +1905,35 @@
 release/
+%3C%25= releasePath %25%3E/
 %22,%0A     

Use {version}/ and commits/{hash}/ paths; don't minify AMD.