Skip to content

Instantly share code, notes, and snippets.

@friartuck6000
Last active August 29, 2015 13:58
Show Gist options
  • Save friartuck6000/10345055 to your computer and use it in GitHub Desktop.
Save friartuck6000/10345055 to your computer and use it in GitHub Desktop.
Install any version of WordPress with a single command
#!/bin/bash
# USAGE:
# getwp [-v version] dest
# Help screen
function usage()
{
echo
echo "getwp -- Made with <3 by @friartuck6000"
echo
echo -e "\033[1mDESCRIPTION\033[0m"
echo -e " Install or upgrade a WordPress instance"
echo
echo -e "\033[1mUSAGE\033[0m"
echo -e " getwp [options] \033[4mdestination\033[0m"
echo
echo -e "\033[1mOPTIONS\033[0m"
echo -e " \033[4m-h\033[0m, \033[4m--help\033[0m, \033[4m--halp\033[0m"
echo -e " Show this help screen."
echo
echo -e " \033[4m-v\033[0m, \033[4m--version\033[0m"
echo -e " Specify a WordPress version to download; gets latest if omitted."
echo -e " See \033[1mhttp://wordpress.org/download/release-archive/\033[0m for version details."
exit 1
}
# Die!
function die()
{
echo -e "$*"
exit 1
}
function opts()
{
ver="latest"
argv=()
while [ $# -gt 0 ]; do
opt=$1; shift
case ${opt} in
-v | --version )
if [ $# -eq 0 -o "${1:0:1}" = "-" ]; then
die "Argument required for ${opt} option."
fi
vermatch='^[0-9\.]+\-?[a-zA-Z0-9]*$'
if [[ "$1" =~ $vermatch ]]; then
ver="$1"
fi
shift;;
-h | --help | --halp )
usage;;
* )
if [ "${opt:0:1}" = "-" ]; then
die "Unknown option ${opt}."
fi
argv+=(${opt});;
esac
done
}
# Parse options
opts $*
# Here we go
# -----------------------------------------------------------------------------------------
MSG_ERROR="\033[41m[ERROR]\033[0m"
MSG_OK="\033[1;32m[OK]\033[0m"
# Set base components
DOWNLOAD_DIR=~/tmp/getwp
INSTALL_DIR=""
WP_BASE_URL="http://wordpress.org"
WP_FILE_EXT="tar.gz"
# Set up the full download URL
if [ "$ver" != "latest" ]; then
WP_ARCHIVE_NAME="wordpress-${ver}.${WP_FILE_EXT}"
else
WP_ARCHIVE_NAME="${ver}.${WP_FILE_EXT}"
fi
WP_DOWNLOAD_URL="${WP_BASE_URL}/${WP_ARCHIVE_NAME}"
# Make sure the download directory exists
if [ ! -d "$DOWNLOAD_DIR" ]; then
mkdir -p $DOWNLOAD_DIR
fi
# Set the install directory
if [ "${argv[0]}" != "" ]; then
if [ ! -d "${argv[0]}" ]; then
echo -e "\033[1;34mCreating destination directory: \033[0m${argv[0]}"
(mkdir -p ${argv[0]} && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
fi
INSTALL_DIR=`echo $(cd ${argv[0]}; pwd)`
else
die "You must supply a destination directory to use this tool!"
fi
echo -e "\033[1;34mEntering download directory...\033[0m"
sleep 1
cd $DOWNLOAD_DIR
rm -rf *
echo -e "$MSG_OK"
# Prefer WGET, but use CURL if it's not available
echo
echo -e "\033[1;34mDownloading latest WordPress version...\033[0m"
sleep 1
if [ "$(which wget)" != "" ]; then
(wget $WP_DOWNLOAD_URL && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
else
(curl -L $WP_DOWNLOAD_URL > latest.tar.gz && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
fi
# Extract
echo
echo -e "\033[1;34mExtracting WordPress files...\033[0m"
sleep 1
if [ "$WP_FILE_EXT" = "tar.gz" ]; then
(tar zxf $WP_ARCHIVE_NAME && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
elif [ "$WP_FILE_EXT" = "zip" ]; then
(unzip $WP_ARCHIVE_NAME && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
fi
# Copy
echo
echo -e "\033[1;34mUpdating files in \033[0m${INSTALL_DIR}..."
sleep 1
shopt -s extglob
(cp -rf wordpress/!(wp-content) $INSTALL_DIR && echo -e "$MSG_OK") \
|| die "$MSG_ERROR"
# Glorious
echo
echo "All done! Enjoy."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment