Skip to content

Instantly share code, notes, and snippets.

@gedex
Created May 12, 2012 05:30
Show Gist options
  • Save gedex/2664342 to your computer and use it in GitHub Desktop.
Save gedex/2664342 to your computer and use it in GitHub Desktop.
#!/bin/sh
SCRIPT_NAME="wpized"
BASE_NAME="wpized_base"
# Location where wpized base is located.
# When there's an update, the compressed file in this URL must be updated.
# @TODO By using automatic build file, i.e Github, we can add VERSION constant
# and append it as suffix of URL_DOWNLOAD.
WPIZED_FILE="wpized_base.tar.gz"
URL_DOWNLOAD="http://my.dev/$WPIZED_FILE"
UNAME=`uname`
if [ "$UNAME" != "Linux" -a "$UNAME" != "Darwin" ] ; then
echo "Your OS is NOT supported yet."
exit 1
fi
set -e
trap "echo Installation failed." EXIT
if [ "$UNAME" = "Darwin" ] ; then
# OSX
# Path where wpized script and wpized-base will be copied into.
PARENT_PATH="/usr/local"
INSTALL_PATH="/usr/local/$BASE_NAME"
# Check if wpized is already installed.
if [ -e "$INSTALL_PATH" ] ; then
echo "Updating $BASE_NAME in $INSTALL_PATH"
else
echo "Installing $BASE_NAME in $INSTALL_PATH"
fi
# If PARENT_PATH doesn't exist or isn't writeable, fix it with sudo.
if [ ! -d "$PARENT_PATH" ] ; then
echo
echo "$PARENT_PATH does not exist. Creating it with 'sudo mkdir'."
echo "This may prompt for your password."
echo
sudo /bin/mkdir "$PARENT_PATH"
sudo /usr/bin/chgrp admin "$PARENT_PATH"
sudo /bin/chmod 755 "$PARENT_PATH"
elif [ ! -w "$PARENT_PATH" -o ! -w "$PARENT_PATH/bin" ] ; then
echo
echo "The install script needs to change the permissions on $PARENT_PATH "
echo "so that administrators can write to it. This may prompt for your password."
echo
sudo /usr/bin/chgrp admin "$PARENT_PATH"
sudo /bin/chmod g+rwx "$PARENT_PATH"
if [ -d "$PARENT_PATH/bin" ] ; then
sudo /usr/bin/chgrp admin "$PARENT_PATH/bin"
sudo /bin/chmod g+rwx "$PARENT/bin"
fi
fi
# Remove old version.
if [ -e "$INSTALL_PATH" ] ; then
rm -rf "$INSTALL_PATH"
fi
# Make sure INSTALL_PATH exists.
mkdir -p "$INSTALL_PATH" || true
if [ ! -d "$INSTALL_PATH" -o ! -w "$INSTALL_PATH" ] ; then
echo "Can't write to $INSTALL_PATH"
exit 1
fi
# Download and untar.
echo "... downloading"
curl --progress-bar $URL_DOWNLOAD | tar -C "$PARENT_PATH" -xzf -
# Add to path.
mkdir -p "$PARENT_PATH/bin"
rm -f "$PARENT_PATH/bin/$SCRIPT_NAME"
ln -s "$INSTALL_PATH/$SCRIPT_NAME" "$PARENT_PATH/bin/$SCRIPT_NAME"
if [ ! -e "$INSTALL_PATH/$SCRIPT_NAME" ] ; then
echo "The install script needs to change the permissions on $INSTALL_PATH/$SCRIPT_NAME "
echo "so that the script executeable. This may prompt for your password."
sudo /bin/chmod g+rwx "$INSTALL_PATH/$SCRIPT_NAME"
fi
elif [ "$UNAME" = "Linux" ] ; then
# Linux
download_url() {
if [ -x "/usr/bin/curl" ] ; then
/usr/bin/curl -# -O $1
elif [ -x "/usr/bin/wget" ] ; then
/usr/bin/wget -q $1
else
echo "Unable to install $BASE_NAME: can't find"
fi
}
do_with_root() {
# Already have root. Just do it.
if [ `whoami` = "root" ] ; then
$*
elif [ -x /bin/sudo -o -x /usr/bin/sudo ] ; then
echo
echo "Since this system includes sudo, $BASE_NAME will request root "
echo "privileges to install. You may be prompted for a password. If "
echo "you prefer to not use sudo, please re-run this script as root."
echo "sudo $*"
sudo $*
else
echo "$BASE_NAME requires root privileges to install. Please re-run "
echo "this script as root."
exit 1
fi
}
TMP_DIR=`mktemp -d -t wpized-install-XXXXXXX`
cd "$TMP_DIR"
# Path where wpized script and wpized-base will be copied into.
BIN_PATH="/usr/bin"
PARENT_PATH="/usr/lib"
INSTALL_PATH="/usr/lib/$BASE_NAME"
# Download the file.
download_url $URL_DOWNLOAD
if [ ! -f "$WPIZED_FILE" ] ; then
echo "Error: download $WPIZED_FILE in $TMP_DIR failed."
exit 1
fi
# Installing.
echo "Installing $TMP_DIR/$WPIZED_FILE"
do_with_root tar -C "$PARENT_PATH" -xzf "$WPIZED_FILE"
do_with_root rm -f "$BIN_PATH/$SCRIPT_NAME"
do_with_root ln -s "$INSTALL_PATH/$SCRIPT_NAME" "$BIN_PATH/$SCRIPT_NAME"
fi
cat <<EOF
Wpized installed! To get started fast:
$ wpized create ~/project_name
Or see the docs at:
later
EOF
trap - EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment