#!/bin/sh

if [ -z "$1" ] ; then
        _name=${PWD##*/}
        if [ -z "$_name" ] ; then
                echo "Error - could not determine theme name"
                echo "Please rerun and pass theme name as an argument."
                exit 1
        fi
else
        _name="$1"
fi

_name_uppercase="$(echo "$_name" | tr '[:lower:]' '[:upper:]')"

# search and replace

find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e s/\'_s\'/"'$_name'"/g
find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e 's/_s_/'"$_name"'_/g'
find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e 's/Text Domain: _s/Text Domain: '"$_name"'/g'
find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e 's/ _s/ '"$_name"'/g'
find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e 's/_s-/'"$_name"'-/g'
find . -type f -name "*.*" -print0 | xargs -0 sed -i '' -e 's/_S_/'"$_name_uppercase"'_/g'

# install composer
# https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 10
fi

php composer-setup.php

php composer.phar install

# npm install

npm install