#!/bin/bash
#
# Copyright 2009 Michael G. Collins.

# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/

# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#

if [ -n "$1" ]
then
    EXTENSION_NAME=$1
else
    echo "You must provide a name for the new extension."
    exit
fi

#get a guid
UUID=`uuidgen | tr A-Z a-z`

echo "Creating extension $EXTENSION_NAME with ID $UUID in directory `pwd`/$EXTENSION_NAME"

#make dirs
mkdir -p "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME"

#create a skeleton install.rdf
cat > "$EXTENSION_NAME/install.rdf" <<EOF
<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:id>{$UUID}</em:id>
    <em:name>$EXTENSION_NAME</em:name>
    <em:type>2</em:type>
    <em:version>0.1</em:version>
    <em:creator>mk_ext.sh</em:creator>
    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
        <em:minVersion>1.5</em:minVersion>
        <em:maxVersion>3.0.*</em:maxVersion>
      </Description>
    </em:targetApplication>
  </Description>
</RDF>
EOF

#create a basic chrome.manifest
cat > "$EXTENSION_NAME/chrome.manifest" <<EOF
content     $EXTENSION_NAME     chrome/content/$EXTENSION_NAME/
overlay chrome://browser/content/browser.xul chrome://$EXTENSION_NAME/content/$EXTENSION_NAME.xul
EOF

#create a stub overlay XUL file
cat > "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME/$EXTENSION_NAME.xul" <<EOF
<?xml version="1.0"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
         <script type="application/x-javascript" src="chrome://$EXTENSION_NAME/content/$EXTENSION_NAME.js"/>
</overlay>
EOF

#create a JS file
cat > "$EXTENSION_NAME/chrome/content/$EXTENSION_NAME/$EXTENSION_NAME.js" <<EOF
const Cc = Components.classes;
const Ci = Components.interfaces;

EOF

echo "Done."
echo "You can install this into a Firefox profile with:"
echo "echo \"`pwd`/$EXTENSION_NAME\" > \"extensions/{$UUID}\""
exit
