Advertisement
Guest User

Untitled

a guest
Apr 9th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # How long to display countdown dialog
  4. cntDown=60
  5.  
  6. # Dialog timeout
  7. dlgTimeout=2
  8.  
  9. # Minimum battery level to reach before forcing hibernation
  10. minBattLvl=20
  11.  
  12. # Replacement text for OK dialog box button
  13. btntext="Proceed"
  14.  
  15. while [[ $cntDown -gt 0 ]] ; do
  16.  
  17.    ioreg -w0 -l | grep ExternalChargeCapable | grep -q Yes
  18.    if [[ $? -ne 0 ]] ; then
  19.       max="`ioreg -w0 -l | grep MaxCapacity | cut -d= -f 2`"
  20.       #echo "Max capacity: $max"
  21.  
  22.       avail="`ioreg -w0 -l | grep CurrentCapacity | cut -d= -f 2`"
  23.       #echo "Available capacity: $avail"
  24.  
  25.       pct=$(($avail*100/$max))
  26.       #echo "Percent available: $pct %"
  27.  
  28.       # Do nothing if our battery lvl is still high
  29.       [ $pct -gt $minBattLvl ] && exit 0
  30.    else
  31.       exit 0   # We are charging, nothing to do....
  32.    fi
  33.  
  34.    userResp="`/usr/bin/osascript 2>&1 <<-EOF
  35.    tell application "System Events"
  36.        activate
  37.        display dialog \
  38.         "Battery below min safe level of ${minBattLvl}%" \
  39.         & "\\n\\nForcing hibernation in: ${cntDown} seconds." \
  40.         as text buttons {"Cancel","$btntext"} \
  41.         default button "$btntext" \
  42.         with icon caution \
  43.         with title "WARNING: Battery Monitor" \
  44.         giving up after $dlgTimeout
  45.    end tell
  46. EOF`"
  47.  
  48. #   echo "Response was: '${userResp}'"
  49.  
  50.    echo $userResp | grep -q "User canceled"
  51.    abort=$?
  52.    #echo "Abort test exit val: $abort"
  53.    [[ $abort -eq 0 ]] && exit 0
  54.  
  55.    echo $userResp | grep -q $btntext
  56.    proceed=$?
  57. #   echo "$btntext test exit val: $proceed"
  58.    [[ $proceed -eq 0 ]] && let cntDown=0
  59.  
  60.    let cntDown-=2
  61. done
  62.  
  63. # Log it in syslog
  64. logger "BatteryMonitor: Battery level below ${minBattLvl}%. Forcing hibernation..."
  65.  
  66. # Activate sleep/hibernation`
  67. pmset sleepnow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement