Skip to content

Instantly share code, notes, and snippets.

@mdb
Created February 10, 2011 13:57
Show Gist options
  • Save mdb/820540 to your computer and use it in GitHub Desktop.
Save mdb/820540 to your computer and use it in GitHub Desktop.
fc_make_local_frags.sh
#!/bin/bash
# This script:
# 1) makes a directory at $new_directory_loc.
# By default, wordpress/wp-content/ctv/local.
# 2) copies files from $files_to_copy $new_directory_loc.
# By default, from wordpress/wp-content/ctv/* to wordpress/wp-content/ctv/local/
# 3) replaces all instances of $find_css with $replace_css in the files copied to
# $new_directory. By default, the relative css paths are replaced with complete
# css paths pointing to our local dev server.
# 4) backs up the original files with a *.bak file extentsion
# 5) replaces all instances of $find_js with $replace_js. By default, relative
# js paths are replaced with complete js paths pointing to your local dev server.
# 6) backs up the original files with a *.bak file extenstions
# 7) prompts the user for consent (y/n) to delete the *bak files
# 8) deletes or does not delete the *.bak files, based on user response
# Usage:
# 1) Run the FC Fragments plugin from within the Wordpress admin
# 2) run the script from the root of the Fancast blogs checkout like so:
# ./fc_make_local_frags.sh
# declare variables to configure this puppy
find_css="\"/styles/"
replace_css="\"http://local.fancast.com:8080/styles/"
find_js="\"/js/"
replace_js="\"http://local.fancast.com:8080/js/"
files_to_copy="wordpress/wp-content/ctv/*"
new_directory="wordpress/wp-content/ctv/local"
# find/replace function
function rep() {
for i in `grep -R --exclude="*.svn*" "$1" * | sed s/:.*$//g | uniq`; do
echo " $1 => $2 changed x times in $i"
sed -i ".bak" -e "s#$1#$2#g" $i
done
}
# make a 'local' directory in which to to store the fragments
echo "Making 'local' directory..."
#mkdir wordpress/wp-content/ctv/local
mkdir $new_directory
# copy all the fragments into the local directory
echo "Copying files to 'local'..."
cp $files_to_copy $new_directory
# go into the directory where the local fragments will live
cd wordpress/wp-content/ctv/local
# search through the fragments and replace all instances of $find_css with $replace_css
echo "Replacing css paths..."
rep $find_css $replace_css
# search through the fragments and replace all instances of $find_js with $replace_js
echo "Replacing js paths..."
rep $find_js $replace_js
# prompt for permission to delete all the "*.bak" files
read -p "Is it safe to delete the backups? (y/n) " RESP
if [ "$RESP" = "y" ]; then
`find . -name "*.bak" -exec rm "{}" \;`
echo "Your files are ready Sir or Madam!"
else
echo "Your backup files have been preserved Sir or Madam!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment