#! /bin/sh

# Maybe "port" the option too
#parser = optparse.OptionParser()
#parser.add_option("", "--create-extension", action="store_true",
#                  help="Create a new GNOME Shell extension")

cat <<OUTPUT
Name should be a very short (ideally descriptive) string.
Examples are: "Click To Focus",  "Adblock", "Shell Window Shrinker".
OUTPUT
echo -n 'Name: '
read name

cat <<OUTPUT
Description is a single-sentence explanation of what your extension does.
Examples are: "Make windows visible on click", "Block advertisement popups"
              "Animate windows shrinking on minimize"
OUTPUT
echo -n 'Description: '
read description


sample_uuid=${name/ /_}
sample_uuid=$sample_uuid'@'$HOSTNAME
cat <<OUTPUT
Uuid is a globally-unique identifier for your extension.
This should be in the format of an email address (foo.bar@extensions.example.com), but
need not be an actual email address, though it's a good idea to base the uuid on your
email address.  For example, if your email address is janedoe@example.com, you might
use an extension title clicktofocus@janedoe.example.com.
OUTPUT
echo -n 'Uuid ['$sample_uuid']: '
read uuid
[[ -z "$uuid" ]] && uuid=$sample_uuid

extension_path=$HOME'/.local/share/gnome-shell/extensions/'$uuid
if [[ -d $extension_path ]]; then
	echo "Extension path $extension_path already exists"
	exit 0
fi
mkdir -p $extension_path

cat >> $extension_path/'metadata.json' <<EOF
{
    'name': $name,
    'description': =description,
    'uuid': $uuid,
    'shell-version': ['@VERSION@']
}
EOF

cat >> $extension_path/'extension.js' <<EOF
// Sample extension code, makes clicking on the panel show a message
const St = imports.gi.St;
const Mainloop = imports.mainloop;

const Main = imports.ui.main;

function _showHello() {
    let text = new St.Label({ style_class: 'helloworld-label', text: "Hello, world!" });
    let monitor = global.get_primary_monitor();
    global.stage.add_actor(text);
    text.set_position(Math.floor (monitor.width / 2 - text.width / 2), Math.floor(monitor.height / 2 - text.height / 2));
    Mainloop.timeout_add(3000, function () { text.destroy(); });
}

// Put your extension initialization code here
function main() {
    Main.panel.actor.reactive = true;
    Main.panel.actor.connect('button-release-event', _showHello);
}
EOF

cat >> $extension_path/'stylesheet.css' <<EOF
/* Example stylesheet */
.helloworld-label {
    font-size: 36px;
    font-weight: bold;
    color: #ffffff;
    background-color: rgba(10,10,10,0.7);
    border-radius: 5px;
}
EOF

echo "Created extension in $extension_path"
gnome-open $extensionjs_path
