#!/usr/bin/env bash
# PATCH VERSION: 0.9.7
# ableton_live_9.x.x_smart_patch.command
# by techietrash (patch logic by pitchshifter)
#
# Patches both 64-bit and 32-bit versions of Ableton Live Suite
# After patching double-click the generate Authorize.auz file
#
# Requires Ableton Live 9 Suite.app to be located in /Applications
# ex: /Applications/Ableton\ Live\ 9\ Suite.app
#
# It's recommended to have the Xcode Command Line Tools installed so that
# Live's architecture can be determined automatically and so that Live can be
# re-codesigned after it is patched.
#
# Set Gatekeeper to Allow Applications From: Anywhere
#
# Tested on 10.10.5 with Live 9.7 x64 and x86

# display banner
echo -e "\033[1;31m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃           ABLETON LIVE SUITE 9.x.x SMART PATCHER -- by techietrash           ┃
┃                   (using new patch logic by pitchshifter)                    ┃
┃                                 Patch v0.9.7                                 ┃
┃     This patch will determine your version of Ableton Live and patch it!     ┃
┃                          (Tested on Live 9.7 32/64)                          ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m"
# display menu
echo -e "\033[0;30;42m 1. FRESH INSTALL  \033[0m\033[0;32m: I want to patch Live AND generate the authorization file.\033[0m
\033[0;30;43m 2. AUTH FILE ONLY \033[0m\033[0;33m: I've already patched, just generate the authorization file.\033[0m
\033[0;30;43m 3. PATCH ONLY     \033[0m\033[0;33m: I already have an authorization file, just patch Live.\033[0m
\033[0;34m▶ Please enter an option (\033[0m\033[0;30;42m 1 \033[0m\033[0;34m, \033[0m\033[0;30;43m 2 \033[0m\033[0;34m or \033[0m\033[0;30;43m 3 \033[0m\033[0;34m) \033[0m\033[0;31m(or press any other key to abort)\033[0m: "
read -p "" -n 1
echo ""
# process reply
if [[ $REPLY =~ ^[123]$ ]]; then
  # set variables we'll need
  LIVE="/Applications/Ableton Live 9 Suite.app"
  BINARY="$LIVE/Contents/MacOS/Live"
  INFO="$LIVE/Contents/Info.plist"
  AUTHFILE="$HOME/Desktop/Authorize.auz"
  if [[ -e "$AUTHFILE" ]]; then
    # use unique name if Authorize.auz already exists on the Desktop
    DATE=`date +%s`
    AUTHFILE="$HOME/Desktop/Authorize ${DATE}.auz"
  fi
  # embedded Authorize.auz file, all-in-one, woo!
  AUZ=$(cat <<'AUTH'
3006-B802-3ECF-762C-38AC-E7FD,00,90,Standard,97079038274421008708204196987934929633656964554684957545854029135395601187849874
3404-A10C-2FE7-D7ED-1C77-519F,40,10,Standard,34333153616624383962842015438849562093371961656972439377305501639140517073772519
3415-4101-9F0E-C4F7-7AC3-F7D8,41,10,Standard,37139082434184859929986839531969618349778914555780091203885834205826456835028235
3864-A10C-97C6-E7FA-DF00-EB3D,86,10,Standard,31526400600138321679429933991999660716087999645757611183124194357904494994649444
3614-310F-5100-9DB5-3256-2996,61,10,Standard,13086484553569269582932135515844418936104185227942097645302488606697940762267182
3627-D102-351F-4F8C-8898-937F,62,10,Standard,65579994113800453603233323515844368533368218834222340342166814691227418614580429
3857-4101-31A6-33E2-5658-7372,85,10,Standard,54172760821613136360230099499441996735216670843926577682437575361562675413831378
3802-4101-011B-4887-D01E-251B,80,10,Standard,58733227563797303081103995006094580450126063300420915958359255658834722693207230
3605-210D-A187-6F99-01C0-BE85,60,10,Standard,62562520138357779000599046756587194066359913924765199551824140067768478598200116
3636-7107-F967-817D-8E8A-F2E4,63,10,Standard,38058769567315949576691830153660431958226684725076539864102563903798163182013283
3846-D102-64E2-4A0E-18CE-4E89,84,10,Standard,58222490268800328690827929468020729383436154357075315293952895386267740221892290
3813-7107-5F43-1F3D-FF15-5BA3,81,10,Standard,28874256800035332889993865509640202458593889278322857806206757644643387817086583
3831-8108-5C97-CA69-FA86-885D,83,10,Standard,80324164240521028330880325279916158619203043462611579436284036102595967995588862
3820-B10E-0676-AD80-B7D5-32EB,82,10,Standard,19513570544385731070268910601919751836919482598092736910668552384691993920805037
3875-0109-A35B-E307-FD36-968A,87,10,Standard,80668348456585631293678092237987846667095828409168041633716912962179330944291598
370D-C100-1B85-D323-1B83-D0A3,70,10,Standard,87974700299072829003159164310420965414135877704489648890829066653239882127628501
371C-5103-465C-CDEA-E6D7-AD63,71,10,Standard,57130251648316199841761819209237998045217261243744037009542225217615182494249442
3641-210D-11D5-6527-5694-78DF,64,10,Standard,67461951379841003894828271122476845521809166721337939195210447108564156020613120
3AFC-5103-0871-F249-C1C9-3B2B,AF,10,Standard,54398028116285697109405633056697814023701757532645741610647942838980296048278284
3A56-4101-3597-34AD-9BC6-5F98,A5,10,Standard,87491528599967835839573251653263587519069578542610813030153358388615325766308780
3BC7-B10E-4D40-8CD9-6398-5EE1,BC,10,Standard,26959234851665085770210111982604869300433205300997902331948497327573229574080910
3B6D-E104-8C05-2BF5-2C71-D4D9,B6,10,Standard,93874430183246886847917576174834113053775539997868985762819951789502773938033000
3BD6-110B-C9E9-BFE5-FBF9-7518,BD,10,Standard,99179262466698417931967225999810387868523581105792163182840099918832824833607208
3AB8-A10C-D090-FAB2-9A64-2F56,AB,10,Standard,57060161299114779691570856930027632418185737384922759735576195586807945787267910
3B92-210D-442A-A2AD-7BF8-87A8,B9,10,Standard,41099404521865599275772378834429380421236680003542179990419975325503200019266838
3AA9-7107-62B4-E02C-6D60-64A3,AA,10,Standard,24849182653554494647367715629139573774866172147165187998349304008907504028880336
3A8B-0109-4544-7B88-5086-0ECF,A8,10,Standard,75889145314685672635674460386754266513685439198579406430821536453985546751147397
3B83-5103-6EF6-4BF9-9A57-47C1,B8,10,Standard,37902283556030291676255333031543276952449741708520908290102147207468062068987475
3A9A-7107-3D82-9289-FD41-90C7,A9,10,Standard,82560688260537492890630688684460330475919670258421561931907190514549897212832072
3C66-4101-D46D-CF05-EEF1-958C,C6,10,Standard,99167230652314334105475427089144701947526711472867292423141608024385632221372567
3BA1-B10E-B226-CC84-68C0-763F,BA,10,Standard,82973054208398804348482324948205688128308174177967591088742821204884893938539269
3B5E-8108-624B-35B4-CEB1-8ECD,B5,10,Standard,19799982859285671049050826747825195351738728920817132530599397014578766094528878
3BB0-8108-0329-DA58-1DB3-8982,BB,10,Standard,30708331318112305898897373826798949043029921123662730680521902399349211868483843
3A65-A10C-E99A-55FC-DA39-7541,A6,10,Standard,82207108485850918298109265758144499616502923544975210979697472977385209717141571
3B4F-6105-CB8E-EB47-687B-0AAB,B4,10,Standard,50164103619482047799678054276970290001541691976196281936643012214737814837357686
3A12-310F-28FD-6A6C-34FF-6D63,A1,10,Standard,26047886722052023131708049386736817052952524420038142912310255642422512893358555
3B7C-8108-9E06-EDEE-E45E-E5CF,B7,10,Standard,65819924536765045878479547559651313314904215789281373522144395747235093588021280
3B29-910A-1665-AA56-ECC3-C706,B2,10,Standard,74420279105891662184779030154072261334494257868811965590417983596592713488763814
3A03-7107-6763-6485-95D0-110B,A0,10,Standard,19778203561995008893963030442299751346565033625839266958446520848082666260898395
3BE5-910A-9BB7-CB8F-31B1-39E6,BE,10,Standard,54726800772028513800770053021637913840405045081163121534943476981486770075450904
3C55-210D-2559-A11F-2C24-0FF4,C5,10,Standard,49296495785216903431300062484743639439951612829820752723837028862916128192828036
3AED-310F-7E29-9AFA-3400-206C,AE,10,Standard,63755125984876889897070956409699483383324721191060690418875934446630415850391291
3A21-8108-F337-06EE-79DB-C239,A2,10,Standard,68723121655243466781805873700640537748469516024245831090676192434620869034793318
3A47-5103-60A4-E7BE-0652-C50E,A4,10,Standard,82508030528095212108129350890607724873061971071259473564387483957358263945840431
3A74-E104-8780-5B40-1A65-A038,A7,10,Standard,73016715468021222865146785737283514995241266501658706268719344705209346569212043
3ACF-6105-624B-ADE6-91E1-615F,AC,10,Standard,31055416560712569913505952643704774680614466057043019200875286618689222315508701
3C33-D102-5940-7ED7-0A67-2665,C3,10,Standard,61973183324125208518457757512568144451106534128824842062417080171916173278617370
3A30-310F-DC69-6A39-C668-036E,A3,10,Standard,69689665539635935988287676216758365197888093447391162811763651956411124093665424
3C11-0109-A8CF-0581-7FD8-42AE,C1,10,Standard,91369157318707788195275899162701888659712891667478174954511552418282630066655693
3B1A-4101-9233-447E-35F7-BD20,B1,10,Standard,24963657773461681215180015806443330177679279324380406518878603502560685194262380
3C44-4101-B1E3-F336-F01A-FDE7,C4,10,Standard,56884000300557249626134349880768996112922481470750469300869139168173014095299450
3ADE-4101-0508-AB1A-8BB3-4647,AD,10,Standard,22349874360841375059383435366444605112561844979023243153178550384484782178018568
3C00-D102-77F8-720C-C724-6228,C0,10,Standard,35019840793696165205102194559155497977574423320055732601165687973791326631649357
3BF4-210D-134D-0B3D-2FE1-E8BF,BF,10,Standard,50784394686441084777682030428471927140974471365897414460958945843605256530410336
3B0B-D102-F717-3E46-77FE-A1ED,B0,10,Standard,66514145599054218318621081897500360930931447654847919877837287113554341132517378
3C22-6105-D720-9A72-E189-7102,C2,10,Standard,94620125921178046208436138980829571458233320536148782350735960139969042066663399
3B38-310F-1E07-09F0-50E0-4039,B3,10,Standard,79177520120056564889336091409250351556294196705449484212282226942194125786978322
AUTH
)
  # functions because I didn't want to mess with making the switch case more complicated

  function check_tools {
    command -v lipo >/dev/null 2>&1
    if [[ "${?}" > 0 ]]; then
      TOOLS=false
      echo -e "\033[0;34m
▶ Command Line Tools are not installed!  I recommended installing them, they are
required to automatically determine Live's architecture and to re-codesign Live
after patching. To proceed without them, please specify Architecture manually:\033[0m"
      echo -e "\033[0;30;42m 1 \033[0m\033[0;34m 32-bit (i386)"
      echo -e "\033[0m\033[0;30;43m 2 \033[0m\033[0;34m 64-bit (x86_64)"
      echo -e "Please press \033[0;30;42m 1 \033[0m\033[0;34m or \033[0m\033[0;30;43m 2 \033[0m\033[0;34m \033[0m\033[0;31m(press any other key to abort and for more info)\033[0m: "
      read -p "" -n 1
      echo ""
      if [[ "$REPLY" == "1" ]]; then
        ARCH="i386"
      elif [[ "$REPLY" == "2" ]]; then
        ARCH="x86_64"
      else
        echo -e "\033[1;97;41m  Aborted! Architecture not selected! \033[0m"
        echo -e "\033[0;34m▶ To install the Xcode Command Line Tools (Recommended):

OS X 10.9 and later, simply type in terminal and press enter:
\033[0m\033[0;31mxcode-select --install\033[0m

\033[0;34mOS X 10.8 and 10.7 require downloading the installer from apple:

10.8\033[0m
\033[0;31mhttp://devimages.apple.com/downloads/xcode/command_line_tools_for_osx_mountain_lion_april_2014.dmg\033[0m\033[0;34m

10.7\033[0m
\033[0;31mhttp://devimages.apple.com/downloads/xcode/command_line_tools_for_xcode_os_x_lion_april_2013.dmg\033[0m"
        echo -en "\033[0;97;44m▶ Press any key to exit this script \033[0m\n"
        read -p "" -n 1
        exit -1
      fi
    fi

  }

  function patch_it {
    if [[ -e "${LIVE}" ]]; then
      # determine live version number
      VERSION=$(/usr/libexec/PlistBuddy -c Print:CFBundleShortVersionString "${INFO}" | awk '{printf $1}')

      ARCH="?"
      TOOLS=true
      check_tools
      if [[ "$ARCH" == "?" ]]; then
        # determine live architecture, will be "x86_64" or "i386"
        ARCH=$(lipo -info "${BINARY}" | awk -F ": " '{printf $3}')
        # output version and architecture
        echo -e "\033[0;34m▶ Ableton Live Suite ${VERSION} ${ARCH} Detected...\033[0m"
      else
        echo -e "\033[0;34m▶ Ableton Live Suite ${VERSION} ${ARCH} Specified...\033[0m"
      fi

      # drop current sudo permissions just in case
      sudo -k

      # get sudo permissions
      echo -e "\033[0;31m▶ This script requires Administrator Privileges!\033[0m"
      sudo -v

      # exit if sudo permissions can't be attained in order to avoid errors
      if [[ "$(sudo whoami)" != "root" ]]; then
        echo -e "\033[1;97;41m  Aborted! Script didn't have sudo privileges !!! \033[0m"
        echo -en "\033[0;97;44m▶ Press any key to exit this script \033[0m\n"
        read -p "" -n 1
        exit -1
      fi

      if [[ "$ARCH" = "x86_64" ]]; then
        # patch 64-bit version
        echo -e "\033[0;34m▶ Patching 64-bit Ableton Live Suite...\033[0m"
        sudo perl -pi -e 's|\x55\x48\x89\xE5\x41\x57\x41\x56\x41\x55\x41\x54\x53\x48\x83\xEC\x78\x49\x89\xCE\x49\x89\xD7\x41\x89\xF5|\xB8\x01\x00\x00\x00\xC3\x41\x56\x41\x55\x41\x54\x53\x48\x83\xEC\x78\x49\x89\xCE\x49\x89\xD7\x41\x89\xF5|g' "${BINARY}"
      elif [[ "$ARCH" = "i386" ]]; then
        # patch 32-bit version
        echo -e "\033[0;34m▶ Patching 32-bit Ableton Live Suite...\033[0m"
        sudo perl -pi -e 's|\x55\x53\x57\x56\x83\xEC\x7C\xE8\x00\x00\x00\x00\x5D\x8B\xB4\x24\x9C\x00\x00\x00|\x31\xc0\x40\xc3\x83\xEC\x7C\xE8\x00\x00\x00\x00\x5D\x8B\xB4\x24\x9C\x00\x00\x00|g' "${BINARY}"
      else
        echo -e "\033[1;97;41m  Error! Architecture could not be determined !!!  \033[0m"
        echo -en "\033[0;97;44m▶ Press any key to exit this script \033[0m\n"
        read -p "" -n 1
        exit -1
      fi
      # remove quarantine attribute from Live
      sudo xattr -rc "${LIVE}"

      # patch success message
      echo -e "\033[0;30;42m▶ Ableton Live Suite ${VERSION} ${ARCH} Successfully Patched! \033[0m"

      if [[ "$TOOLS" == true ]]; then
        # re-codesign the app, to avoid errors, requires gatekeeper to be disabled
        echo -e "\033[0;34m▶ Re-Codesigning Ableton Live Installation.cfg... (please be patient!)\033[0m"
        codesign -f -s - -v "${LIVE}/Contents/MacOS/Installation.cfg" >/dev/null 2>&1
        echo -e "\033[0;30;42m▶ Ableton Live Installation.cfg Successfully Codesigned! \033[0m"
        echo -e "\033[0;34m▶ Re-Codesigning Ableton Live 9 Suite.app... (please be patient!)\033[0m"
        codesign -f -s - -v "${LIVE}" >/dev/null 2>&1
        echo -e "\033[0;30;42m▶ Ableton Live 9 Suite.app Successfully Codesigned! \033[0m"
      else
        echo -e "\033[0;34m▶ Command Line Tools are not installed, skipping codesigning.\033[0m"
        echo -e "\033[0;34m▶ (Live should still work without re-codesigning)\033[0m"
      fi
    else
      echo -e "\033[1;97;41m  Ableton Live 9 Suite.app is not in /Applications !!!\033[0m"
      echo -en "\033[0;97;44m▶ Press any key to exit this script \033[0m\n"
      read -p "" -n 1
      exit -1
    fi
  }

  function gen_auth {
    # create the authorize.auz file on the desktop
    echo -e "\033[0;34m▶ Creating Authorization file...\033[0m"
    echo -n "$AUZ" > "${AUTHFILE}"
    echo -e "\033[0;30;42m▶ Authorization File Created: $AUTHFILE \033[0m"
  }
  function complete {
    # displays final instructions, reveals files, and exits
    echo -e "\033[0;30;43m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m
\033[0;30;43m┃     PLEASE OPEN LIVE AND DOUBLE CLICK THE AUTHORIZE.AUZ FILE TO REGISTER     ┃\033[0m
\033[0;30;43m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m"
    sleep 1
    # reveal live and authorize files in Finder, if they exist
    if [[ -e "${LIVE}" ]]; then
      open -R "${LIVE}"
    fi
    if [[ -e "${AUTHFILE}" ]]; then
      open -R "${AUTHFILE}"
    fi
    echo -en "\033[0m\033[0;97;44m▶ Press any key to exit this script \033[0m"
    read -p "" -n 1
    exit 0
  }

  # determine which functions to run, based on menu choice
  case "$REPLY" in
    "1")
      patch_it
      gen_auth
    ;;
    "2")
      gen_auth
    ;;
    "3")
      patch_it
    ;;
    *)
      #do nothing
    ;;
  esac
  # run 'complete' function regardless, as choice 1-3 has to be run to reach this
  complete
fi
# abort if menu choic is not 1, 2, or 3
echo -e "\033[1;97;41m  Script was Aborted !!! No changes have been made to your system \033[0m"
echo -en "\033[0;97;44m▶ Press any key to exit this script \033[0m\n"
read -p "" -n 1
exit -1
