Skip to content

Instantly share code, notes, and snippets.

@sattellite
Forked from henriquemoody/sublime-text-2.sh
Last active May 26, 2016 09:01
Show Gist options
  • Save sattellite/10596069 to your computer and use it in GitHub Desktop.
Save sattellite/10596069 to your computer and use it in GitHub Desktop.
Install new version of Sublime Text 3
#!/usr/bin/env bash
# Usage: {script} [ OPTIONS ]
#
# OPTIONS
#
# -h, --help Displays this help message.
# -t, --type Default type is "dev". Values is "stable" and "dev".
# -b, --build Version of Sublime Text 3. If not defined tries to get the
# build into the Sublime Text 3 website.
# --target Default target is "/usr/local".
# -c, --crack Try patch and crack Sublime Text binary for accepted license.
# -s, --sudo Use with `sudo' command
#
# ABOUT
#
# This script is fork from Henrique Moody <henriquemoody@gmail.com>
#
# CRACK
#
# Cracking binary of Sublime Text 3 get from https://sublimecrack.wordpress.com/2015/06/07/sublime_text3_crack/
# And it needed a `sudo' execution.
#
# Report bugs to Alexander Groshev <null@sattellite.me>
#
set -e
# Prepare arguments (based on http://stackoverflow.com/a/5230306/1489324)
declare HELP=0
declare CRACK=0
declare TARGET="/usr/local"
declare SUDO=""
declare TYPE
declare BUILD
declare options=$(getopt --unquoted --options hcst:b: --longoptions help,crack,sudo,type:,target:,build: -- "$@")
set -- ${options}
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help) HELP=1 ;;
-c|--crack) CRACK=1 ;;
-b|--build) BUILD="${2}"; shift ;;
--target) TARGET="${2}"; shift ;;
-t|--type) TYPE=$2; shift ;;
-s|--sudo) SUDO="sudo ";;
(--) shift; break ;;
(-*) echo "${0}: error - unrecognized option ${1}" 1>&2; exit 1 ;;
(*) break ;;
esac
shift
done
if [[ -z "${TYPE}" ]]; then
TYPE="dev"
fi
if [[ -z "${TARGET}" ]]; then
TARGET="/usr/local"
fi
if [[ "${HELP}" = '1' ]]; then
sed -E 's/^#\s?(.*)/\1/g' "${0}" |
sed -nE '/^Usage/,/^Report/p' |
sed "s/{script}/$(basename "${0}")/g"
exit 1
fi
declare URL
# OLD URL_FORMAT was "http://c758482.r82.cf2.rackcdn.com/sublime_text_3_build_%d_x%d.tar.bz2"
declare URL_FORMAT="https://download.sublimetext.com/sublime_text_3_build_%d_x%d.tar.bz2"
declare BITS
declare SOURCE_URL="http://www.sublimetext.com/3%s"
if [[ "${TYPE}" != "dev" ]]; then
TYPE=""
fi
declare SURL=$(printf "${SOURCE_URL}" "${TYPE}")
if [[ -z "${BUILD}" ]]; then
BUILD=$(
curl -Ls ${SURL} |
grep '<h2>Build' |
head -n1 |
sed -E 's#<h2>Build ([0-9]+)</h2>#\1#g'
)
fi
if [[ "$(uname -m)" = "x86_64" ]]; then
BITS=64
else
BITS=32
fi
URL=$(printf "${URL_FORMAT}" "${BUILD}" "${BITS}")
read -p "Do you really want to install Sublime Text 3 (Build ${BUILD}, x${BITS}) on \"${TARGET}\"? [Y/n]: " CONFIRM
CONFIRM=$(echo "${CONFIRM}" | tr [a-z] [A-Z])
if [[ "${CONFIRM}" = 'N' ]] || [[ "${CONFIRM}" = 'NO' ]]; then
echo "Aborted!"
exit
fi
echo "Downloading Sublime Text 3"
curl --progress-bar --location "${URL}" | ${SUDO} tar -xjC ${TARGET}
echo "Creating shortcut file"
cat ${TARGET}/sublime_text_3/sublime_text.desktop |
sed -e "s#/opt#${TARGET}#g" -e "s#/sublime_text/#/sublime_text_3/#g" |
${SUDO} tee "/usr/share/applications/sublime_text.desktop" 1>/dev/null
echo "Creating binary file"
${SUDO} tee ${TARGET}/bin/subl 1>/dev/null <<SCRIPT
#!/bin/bash
if [[ "\${1}" == '--help' ]]; then
${TARGET}/sublime_text_3/sublime_text --help
else
${TARGET}/sublime_text_3/sublime_text \$@ > /dev/null 2>&1 &
fi
SCRIPT
if [[ "${CRACK}" = "1" ]]; then
declare CRACK_URL='https://sublimecrack.wordpress.com/2015/06/07/sublime_text3_crack/'
declare RESULT=$(curl -Ls https://sublimecrack.wordpress.com/2015/06/07/sublime_text3_crack/|grep "${BUILD}</td>" -A5|grep "Linux x${BITS}" -A3 -m1|sed -e 's#</\?td>##g'|perl -ne 'print unless $.%2')
declare CHANGES=(${RESULT// /})
declare OFFSET=${CHANGES[0]}
declare SEEK=$(printf "%d" ${OFFSET})
declare PATCH=${CHANGES[1]}
echo "${PATCH} ${OFFSET} ${SEEK}"
printf "\\x${PATCH}"|${SUDO} dd status=none seek=${SEEK} conv=notrunc bs=1 of=${TARGET}/sublime_text_3/sublime_text
fi
echo "Finish!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment