#!/bin/bash
#
# LG G Watch Utility for Linux and OSX
# Ported from RootsJunky
 
 
 
# Lets check what platform we are running on
# We will check what we are running on and then set some variables for some funtimes
function platform
{
        platform=`uname`
        if [ $(uname -p) = 'powerpc' ]; then
        echo "[-] PowerPC is not supported."
        exit 1
        fi
 
        if [ "$platform" = 'Darwin' ]; then
        ADB="./adbosx"
        FASTBOOT="./fastbootosx"
        version="OS X"
        else
        ADB="./adblinux"
        FASTBOOT="./fastbootlinux"
        version="Linux"
               
        fi
}
 
platform
 
 
echo "__________________________________________________"
echo [*] Simple LG G Watch Tool
echo [*] Linux / OSX Version
echo [*] Remember to chmod +x this script
echo [*] Ported by @rhcp011235
echo "--------------------------------------------------"
echo "What would you like to do:"
echo "Your choices are: unlock / relock / root / restore / twrp "
echo "enter your choice"
read choice
if [ "$choice" = "unlock" ]; then
        echo "--------------------------------------------------------------------------------"
        echo [*] Before continuing, ensure USB debugging is enabled,
        echo [*] device settings/about then click on build number 10 times
        echo [*] back to settings/developer options enable usb debugging
        echo [*] have the Universal drivers installed and your G Watch
        echo [*] connected via USB charging dock to the computer
        echo [*] WARNING THIS WILL FACTORY RESET YOUR DEVICE
        echo --------------------------------------------------------------------------------
        read -rsp $'PRESS ENTER to continue...\r\n'
        $ADB kill-server
        $ADB wait-for-device
        echo [*] Device found.
        $ADB reboot bootloader
        echo --------------------------------------------------------------------------------
        echo [*] on the next screen you see the unlock No. press the little
        echo [*] arrow at the bottom of screen to yes then press the circle
        echo [*] when its done you will be back in boot menu
        echo [*] and it will say unlocked.
        echo --------------------------------------------------------------------------------
        $FASTBOOT oem unlock
        echo --------------------------------------------------------------------------------
        echo [*] First boot will take up to 10 minutes and boot twice.
        echo --------------------------------------------------------------------------------
        $FASTBOOT reboot
        read -rsp $'PRESS ENTER to exit...\r\n'
        exit
        elif [ "$choice" = "relock" ]; then
                echo --------------------------------------------------------------------------------
                echo [*] Before continuing, ensure USB debugging is enabled,
                echo [*] device settings/about then click on build number 10 times
                echo [*] back to settings/developer options enable usb debugging
                echo [*] have the Universal drivers installed and your G Watch
                echo [*] connected via USB charging dock to the computer
                echo --------------------------------------------------------------------------------
                read -rsp $'PRESS ENTER to continue...\r\n'
                $ADB kill-server
                $ADB wait-for-device
                $ADB reboot bootloader
                $FASTBOOT oem lock
                echo --------------------------------------------------------------------------------
                echo [*] First boot will take up to 5 minutes
                echo --------------------------------------------------------------------------------
                echo [*] press any key to reboot the device once locked.
                read -rsp $'PRESS ENTER to continue...\r\n'
                $FASTBOOT reboot
                echo DONE
                read -rsp $'PRESS ENTER to exit...\r\n'
                exit
        elif [ "$choice" = "root" ]; then
                echo --------------------------------------------------------------------------------
                echo [*] Big thanks to Jcase for Modded boot image
                echo [*] Make sure USB Debugging is enabled on your watch
                echo [*] this will boot a root boot img and reboot
                echo [*] your device like normal
                echo --------------------------------------------------------------------------------
                read -rsp $'PRESS ENTER to continue...\r\n'
                echo [*]
                echo [*] waiting for device
                $ADB wait-for-device...
                $ADB reboot bootloader
                $FASTBOOT boot LGGW-rootboot.img
                echo --------------------------------------------------------------------------------
                echo [*] to test root on your watch open a commadn window
                echo [*] type = adb shell
                echo [*] type = su
                echo [*] if it returns
                echo [*] root@dory #
                echo [*] then you have root access
                echo ---------------------------------------------------------------------------------
                echo DONE
                read -rsp $'PRESS ENTER to exit...\r\n'
                exit
        elif [[ "$choice" = "restore" ]]; then
                echo --------------------------------------------------------------------------------
                echo [*] Restore G watch to stock to fix a brick or just return to stock
                echo [*] After restore runs you will boot to recovery where you can either
                echo [*] reboot the device or do a factory reset
                echo [*] Before continuing, ensure your LG G Watch is in
                echo [*] "Fastboot" mode and connected via USB with the drivers installed.
                echo --------------------------------------------------------------------------------
                echo [*] To Get into bootloader mode you have to press a pen
                echo [*] into the back reset button and wait for the device to restart.
                echo [*] as soon as you see the LG logo press both the top left and
                echo [*] bottom right corners of the screen repeatedly while still holding
                echo [*] the reset button and the bootloader screen will pop up.
                echo [*] release the pen holding the reset button and you are in bootloader.
                echo --------------------------------------------------------------------------------
                echo [*] Another option to enter bootloader mode is to open a command
                echo [*] window and type adb reboot bootloader. only if the device is on.
                echo --------------------------------------------------------------------------------
                read -rsp $'PRESS ENTER to continue...\r\n'
                $ADB kill-server
                $ADB reboot bootloader
                $FASTBOOT devices
                $FASTBOOT flash system 78Ysystem.img
                $FASTBOOT flash boot 78Yboot.img
                $FASTBOOT flash recovery 78yrecovery.img
                echo --------------------------------------------------------------------------------
                echo [*] Press the arrow bottom right to scroll to recovery
                echo [*] then the circle to boot recovery to factory reset
                echo [*] in recovery you will see a android guy on his back
                echo [*] press the top left and bottom right of screen again
                echo [*] to enter recovery menu. now scroll up and down adn swipe
                echo [*] right to select. when done in recovery just reboot.
                echo --------------------------------------------------------------------------------
                echo [*] If you dont want to factory reset just press the circle button
                echo [*] I recommend fastory resetting device at this time.
                echo --------------------------------------------------------------------------------
                read -rsp $'PRESS ENTER to exit...\r\n'
                exit
        elif [[ "$choice" = "twrp" ]]; then
                echo --------------------------------------------------------------------------------
                echo [*] This will flash Team win / TWRP recovery
                echo [*] to your device from fastboot.
                echo --------------------------------------------------------------------------------
                read -rsp $'PRESS ENTER to continue...\r\n'
                echo [*] waiting for device
                $ADB wait-for-device
                echo [*] device found
                $ADB reboot bootloader
                $FASTBOOT flash recovery openrecovery-twrp-2.7.1.1-dory.img
                echo --------------------------------------------------------------------------------
                echo [*] your device is on the boot menu screen now scoll to Recovery
                echo [*] with arrow buttons and select with circle to boot to recovery
                echo --------------------------------------------------------------------------------
                echo [*] If you want to keep TWRP recovery installed after
                echo [*] a reboot select reboot then system and choose yes
                echo [*] to keeping TWRP recovery.
                echo --------------------------------------------------------------------------------
                echo [*] RootJunky OUT 
                read -rsp $'PRESS ENTER to exit...\r\n'
                exit
        fi