#!/bin/bash
# This script creates a new subtheme for zen-6.x-2.x-dev
# Place this file in your theme directory, alongside the zen folder, as a file name "subtheme-creator"
# Usage: open a shell, navigate to the zen directory and run ./subtheme-creator
# (Run chmod 700 on this file to make it executable)
# Based on script submitted here: drupal.org/node/276120

#~ Chain of events:
#~ * copy STARTERKIT/ from zen/ and rename to NEWNAME (lowercase/underscores)
#~ * rename STARTERKIT.info.txt to NEWNAME.info
#~ * edit NEWNAME.info name and description field
#~ * remove unneeded layout-{!chosen layout}.css file and reference in NEWNAME.info
#~ * replace all occurances of STARTERKIT in template.php & theme-settings.php with NEWNAME


echo -n "Enter a name for your theme: "
read -e NAME

HUMAN=`echo $NAME | sed -e 's/[^a-zA-Z0-9\ ]//g'`
NAME=`echo $NAME | tr [:upper:] [:lower:] | sed -e 's/[\-\ ]/_/g' -e 's/[^a-z_]//g'`

if [ -d $NAME ]; then
    echo "Theme folder already exists with that name."
    echo "Do you want to remove the existing theme?"
    select OVERWRITE in Yes No
    do
        if [ $OVERWRITE = "Yes" ]; then
            rm -r $NAME /tmp/$NAME;
            break
        else
            echo "Exiting..."
            exit
        fi
    done
fi

echo "Choose a page layout"
select LAYOUT in Fixed Liquid
do
    if [ $LAYOUT = "Liquid" ]; then
        DELCSS="layout-fixed"
        CSS="layout-liquid"
        break
    else
        DELCSS="layout-liquid"
        CSS="layout-fixed"
        break
    fi
done

# Copy the STARTERKIT folder
cp -r zen/STARTERKIT $NAME

# Create .info file
sed -e 's/STARTERKIT/'$NAME'/g' -e 's/Zen Sub-theme Starter Kit/'"$HUMAN"'/g' -e 's/^\(description = \).*$/\1A Zen sub-theme/g' $NAME/STARTERKIT.info.txt > $NAME/$NAME.info
rm $NAME/STARTERKIT.info*

# Remove layout css file and references not required
rm $NAME/css/$DELCSS*
sed -e 's/'$DELCSS'/'$CSS'/g' $NAME/$NAME.info > /tmp/zen.info
mv /tmp/zen.info $NAME/$NAME.info

# Replace all occurances of STARTERKIT with theme name in template and theme-settings files
sed 's/STARTERKIT/'$NAME'/g' $NAME/template.php > /tmp/zen-template.php
mv /tmp/zen-template.php $NAME/template.php
sed 's/STARTERKIT/'$NAME'/g' $NAME/theme-settings.php > /tmp/zen-theme-settings.php
mv /tmp/zen-theme-settings.php $NAME/theme-settings.php

echo New theme folder $NAME created in `pwd`

exit 0
