Created
April 25, 2012 22:38
-
-
Save seniorihor/2494041 to your computer and use it in GitHub Desktop.
Автовимикалка компа — надпотрібна утиліта! pre-alpha 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
class Shutdown | |
def initialize(arguments) | |
@argv1 = arguments[0].to_s | |
if arguments.size > 1 | |
@argv2 = arguments[1].to_i | |
end | |
end | |
def check_arguments | |
info = {:'-r' => 'reboot after shutdown', | |
:'-h' => 'halt or power off after shutdown', | |
:'-H' => 'halt after shutdown (implies -h)', | |
:'-P' => 'power off after shutdown (implies -h)', | |
:'-c' => 'cancel a running shutdown', | |
:'-k' => 'only send warnings, don\'t shutdown', | |
:'-q' => 'reduce output to errors only', | |
:'-v' => 'increase output to include informational messages', | |
:'--help' => 'display this help and exit', | |
:'--version' => 'output version information and exit'} | |
if @argv1 | |
puts "Your choice: #{info[@argv1.to_sym]}\n" | |
end | |
if @argv2 | |
puts "Set time: #{@argv2}" | |
end | |
if @argv1 == '--version' | |
puts "ShutDowner pre-alpha 1\nCopyright (C) 2012 seniorihor" | |
exit 0 | |
elsif @argv1 == '--help' | |
puts info | |
exit 0 | |
end | |
end | |
def check_sudo | |
begin | |
`sudo echo` | |
rescue Interrupt | |
puts "\nERROR: Aborted input password. Sudo does not work!" | |
exit 2 | |
end | |
end | |
def start | |
begin | |
@argv2.downto(0) do |minute| | |
if @argv2 > 1 | |
puts "Time remaining to shutdown: #{minute} minutes" | |
sleep 60 | |
elsif @argv2 == 1 | |
puts "Attention! Last minute..." | |
`eject -r && eject -t` | |
sleep 60 | |
else | |
puts "Bye! See you..." | |
`sudo shutdown #{argv1} #{argv2}` | |
end | |
end | |
rescue Interrupt | |
puts "\nTurning off the computer stopped!" | |
exit 3 | |
end | |
end | |
end | |
off = Shutdown.new(ARGV) | |
off.check_arguments | |
off.check_sudo | |
off.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment