2024-10-19 02:04:58 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
# BEGIN: Includes & Requires
|
|
|
|
|
|
|
|
require 'erb'
|
2024-10-19 03:48:44 +00:00
|
|
|
require 'time'
|
2024-10-19 02:04:58 +00:00
|
|
|
require 'json'
|
|
|
|
require 'net/http'
|
|
|
|
require 'English'
|
|
|
|
require 'rubygems/package'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'optparse'
|
2024-10-19 03:48:44 +00:00
|
|
|
require 'zlib'
|
2024-10-19 02:04:58 +00:00
|
|
|
|
|
|
|
# END: Includes & Requires
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Overrides
|
|
|
|
|
|
|
|
class Dir
|
|
|
|
def / (path)
|
|
|
|
output_path = File.absolute_path(self.path) + '/' + path
|
|
|
|
if File.directory? output_path
|
|
|
|
Dir.new(output_path)
|
|
|
|
else
|
|
|
|
output_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class String
|
|
|
|
def / (other)
|
|
|
|
self + '/' + other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# END: Overrides
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Variables
|
|
|
|
URL_DOWNLOAD_BASE='https://dl.discordapp.net/apps/linux/{VERSION}/discord-{VERSION}.tar.gz'
|
|
|
|
DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.gz'
|
|
|
|
|
|
|
|
$CONFIG = Hash.new
|
|
|
|
$CONFIG[:icon_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'icons'
|
|
|
|
File.mkdir($CONFIG[:icon_dir]) if not File.exist?($CONFIG[:icon_dir])
|
2024-10-19 03:48:44 +00:00
|
|
|
$CONFIG[:discord_config_path] = Dir.new(Dir.home) / '.config' / 'discord'
|
2024-10-19 02:04:58 +00:00
|
|
|
$CONFIG[:local_application_install_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'applications'
|
|
|
|
File.mkdir($CONFIG[:local_application_install_dir]) if not File.exist?($CONFIG[:local_application_install_dir])
|
|
|
|
$CONFIG[:desktop_path] = Dir.new(Dir.home) / '.local' / 'share' / 'applications' / 'discord.desktop'
|
|
|
|
$CONFIG[:install_dir] = Dir.new(Dir.home) / '.opt'
|
|
|
|
File.mkdir($CONFIG[:install_dir]) if not File.exist?($CONFIG[:install_dir])
|
|
|
|
$CONFIG[:discord_path] = $CONFIG[:install_dir] / 'Discord'
|
|
|
|
$CONFIG[:desktop_file_path] = $CONFIG[:discord_path] / 'discord.desktop'
|
|
|
|
$CONFIG[:discord_version_file] = $CONFIG[:discord_path] / 'resources' / 'build_info.json'
|
|
|
|
$CONFIG[:action] = 'install'
|
|
|
|
$CONFIG[:keep_installer] = false
|
|
|
|
DISCORD_DESKTOP_CONTENTS=<<EOF
|
|
|
|
[Desktop Entry]
|
|
|
|
Name=Discord
|
|
|
|
StartupWMClass=discord
|
|
|
|
Comment=All-in-one voice and text chat for gamers that's free, secure, and works on both your desktop and phone.
|
|
|
|
GenericName=Internet Messenger
|
|
|
|
Exec=#{$CONFIG[:install_dir].path}/Discord/Discord
|
|
|
|
Icon=discord
|
|
|
|
Type=Application
|
|
|
|
Categories=Network;InstantMessaging;
|
|
|
|
Path=#{$CONFIG[:install_dir].path}/Discord
|
|
|
|
EOF
|
|
|
|
# END: Variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Handle CLI ARgs
|
|
|
|
|
|
|
|
OptionParser.new do |parser|
|
|
|
|
parser.banner = "#{ File.basename($0) } [action]"
|
|
|
|
parser.separator ""
|
|
|
|
parser.separator " Actions"
|
|
|
|
parser.separator "------------------------------------------------------------------------"
|
|
|
|
|
|
|
|
parser.on('-c', '--check', "Check and report installed & remote versions") do
|
|
|
|
$CONFIG[:action] = 'check'
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('-i', '--install', "Check for & Install new version of discord (default)") do
|
|
|
|
$CONFIG[:action] = 'install'
|
|
|
|
end
|
|
|
|
|
2024-10-19 03:48:44 +00:00
|
|
|
parser.on('-u', '--uninstall', "Uninstall Discord (not-implemented)") do
|
|
|
|
$CONFIG[:action] = 'uninstall'
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('-b', '--backup', "Backup Discord Installation (not-implemented)") do
|
|
|
|
$CONFIG[:action] = 'backup'
|
|
|
|
$CONFIG[:backup_filename] = 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
|
|
|
|
end
|
|
|
|
|
2024-10-19 02:04:58 +00:00
|
|
|
parser.separator ""
|
|
|
|
parser.separator " General Flags"
|
|
|
|
parser.separator "------------------------------------------------------------------------"
|
|
|
|
parser.on('-h', '--help', "Show this help output") do
|
|
|
|
puts parser
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
parser.on('--keep', 'Keep discord tar.gz file (default: False)') do
|
|
|
|
$CONFIG[:keep_installer] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
end.parse!
|
|
|
|
|
|
|
|
# BEGIN: Handle CLI ARgs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Helper Functions
|
|
|
|
|
|
|
|
def get_remote_version
|
|
|
|
uri = URI(DOWNLOAD_URL)
|
|
|
|
response = Net::HTTP.get(uri)
|
|
|
|
if response =~ /discord-(?<version>\d+\.\d+\.\d+).tar.gz/
|
|
|
|
Gem::Version.new($LAST_MATCH_INFO['version'])
|
|
|
|
else
|
|
|
|
raise "Failed to retrieve remote version"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_local_version
|
|
|
|
if File.exist? $CONFIG[:discord_version_file]
|
|
|
|
data = JSON.load_file($CONFIG[:discord_version_file])
|
|
|
|
Gem::Version.new(data['version'])
|
|
|
|
else
|
|
|
|
Gem::Version.new('0.0.0')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-19 03:48:44 +00:00
|
|
|
|
2024-10-19 02:04:58 +00:00
|
|
|
def download_installer(version)
|
|
|
|
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s))
|
|
|
|
puts "download_installer: Downloading The discord #{version.to_s} tar.gz"
|
|
|
|
file_contents = Net::HTTP.get(uri)
|
|
|
|
output_file = "discord-#{version}.tar.gz"
|
|
|
|
puts "download_installer: Output file will #{output_file}"
|
|
|
|
$CONFIG[:install_dir].chdir do
|
|
|
|
File.open(output_file, 'wb') do |file|
|
|
|
|
file.write(file_contents)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "download_installer: tar.gz was downloaded"
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_install(version)
|
|
|
|
installer_file = "discord-#{version}.tar.gz"
|
|
|
|
puts "do_install: Extracting new installer"
|
|
|
|
$CONFIG[:install_dir].chdir do
|
|
|
|
File.open(installer_file, 'rb') do |file|
|
|
|
|
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not $CONFIG[:keep_installer]
|
|
|
|
puts "do_install: Removing new installer (#{installer_file})"
|
|
|
|
File.unlink(installer_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_desktop_file
|
|
|
|
file_needs_updating = true
|
|
|
|
puts "update_desktop_file: Checking if desktop entry needs updating"
|
|
|
|
if File.exist?($CONFIG[:desktop_path])
|
|
|
|
puts "update_desktop_file: Desktop file exists, checking if it matches template"
|
|
|
|
File.open($CONFIG[:desktop_path], 'r') do |file|
|
|
|
|
if file.read() == DISCORD_DESKTOP_CONTENTS
|
|
|
|
puts "update_desktop_file: Desktop file matches template. Not updating"
|
|
|
|
file_needs_updating = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if file_needs_updating
|
|
|
|
File.open($CONFIG[:desktop_path], 'w') do |file|
|
|
|
|
puts "update_desktop_file: Desktop File was written"
|
|
|
|
file.write(DISCORD_DESKTOP_CONTENTS)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "update_desktop_file: Triggering desktop to install new desktop entry and refresh entries"
|
|
|
|
%x[ desktop-file-install --dir=#{$CONFIG[:local_application_install_dir].path} #{$CONFIG[:desktop_path]} ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-19 03:48:44 +00:00
|
|
|
def make_backup
|
|
|
|
files = Array.new
|
|
|
|
Dir.glob("#{$CONFIG[:discord_config_path]}/**").each{|item|
|
|
|
|
if File.file?(item) or File.directory?(item)
|
|
|
|
files << File.new(item)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
Dir.glob("#{$CONFIG[:discord_path]}/**").each {|item|
|
|
|
|
if File.file?(item) or File.directory?(item)
|
|
|
|
files << File.new(item)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
Dir.chdir(Dir.home) do
|
|
|
|
File.open($CONFIG[:backup_filename], "wb") do |file|
|
|
|
|
Zlib::GzipWriter.wrap(file) do |gzip|
|
|
|
|
Gem::Package::TarWriter.new(gzip) do |tar|
|
|
|
|
files.each do |file|
|
|
|
|
if File.file?(file)
|
|
|
|
content = file.read()
|
|
|
|
tar.add_file_simple(file.path.sub("#{Dir.home}/",''), file.stat.mode, content.length) do |io|
|
|
|
|
io.write(content)
|
|
|
|
end
|
|
|
|
elsif File.directory?(file)
|
|
|
|
tar.mkdir(file.path.sub("#{Dir.home}/",""), file.stat.mode)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2024-10-19 02:04:58 +00:00
|
|
|
# END: Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BEGIN: Work
|
|
|
|
|
|
|
|
local_version = get_local_version
|
|
|
|
remote_version = get_remote_version
|
|
|
|
|
|
|
|
case $CONFIG[:action]
|
|
|
|
when 'install'
|
|
|
|
if local_version < remote_version
|
|
|
|
puts "main: Installed Version will be updated"
|
|
|
|
download_installer(remote_version)
|
|
|
|
do_install(remote_version)
|
|
|
|
elsif local_version == remote_version
|
|
|
|
puts "main: Installed Version is up-to-date"
|
|
|
|
else
|
|
|
|
puts "main: What?"
|
|
|
|
end
|
|
|
|
when 'check'
|
|
|
|
puts
|
|
|
|
puts " Report"
|
|
|
|
puts "-------------------------------------"
|
|
|
|
puts " Remote Version: #{remote_version.to_s}"
|
|
|
|
puts " Local Version: #{local_version.to_s}"
|
|
|
|
puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}"
|
2024-10-19 03:48:44 +00:00
|
|
|
when 'backup'
|
|
|
|
make_backup
|
2024-10-19 02:04:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# END: Work
|