#!/usr/bin/env ruby # Copyright (C) 2009 Jonathan Schmidt-Dominé # Copyright (C) 2019 Harald Sitter # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require 'fileutils' ARCHIVE_UNCOMPRESSORS = { 'application/x-tar' => :"tar -xf %s -C %s", 'application/tar' => :"tar -xf %s -C %s", 'application/x-gzip' => :"tar -zxf %s -C %s", 'application/gzip' => :"tar -zxf %s -C %s", 'application/x-gzip-compressed-tar' => :"tar -zxf %s -C %s", 'application/gzip-compressed-tar' => :"tar -zxf %s -C %s", 'application/x-gzip-compressed' => :"tar -zxf %s -C %s", 'application/gzip-compressed' => :"tar -zxf %s -C %s", 'application/bzip' => :"tar -jxf %s -C %s", 'application/bzip2' => :"tar -jxf %s -C %s", 'application/x-bzip' => :"tar -jxf %s -C %s", 'application/x-bzip2' => :"tar -jxf %s -C %s", 'application/bzip-compressed' => :"tar -jxf %s -C %s", 'application/bzip2-compressed' => :"tar -jxf %s -C %s", 'application/x-bzip-compressed' => :"tar -jxf %s -C %s", 'application/x-bzip2-compressed' => :"tar -jxf %s -C %s", 'application/bzip-compressed-tar' => :"tar -jxf %s -C %s", 'application/bzip2-compressed-tar' => :"tar -jxf %s -C %s", 'application/x-bzip-compressed-tar' => :"tar -jxf %s -C %s", 'application/x-bzip2-compressed-tar' => :"tar -jxf %s -C %s", 'application/zip' => :"unzip %s -d %s", 'application/x-zip' => :"unzip %s -d %s", 'application/x-zip-compressed' => :"unzip %s -d %s", 'multipart/x-zip' => :"unzip %s -d %s", 'application/tgz' => :"tar -zxf %s -C %s", 'application/x-compressed-gtar' => :"tar -zxf %s -C %s", 'file/tgz' => :"tar -zxf %s -C %s", 'multipart/x-tar-gz' => :"tar -zxf %s -C %s", 'application/x-gunzip' => :"tar -zxf %s -C %s", 'application/gzipped' => :"tar -zxf %s -C %s", 'gzip/document' => :"tar -zxf %s -C %s", 'application/x-bz2 ' => :"tar -jxf %s -C %s", 'application/x-gtar' => :"tar -xf %s -C %s", 'multipart/x-tar' => :"tar -xf %s -C %s" } ARCHIVE = ARGV[0] # @param log_msg [String] error that gets logged to CLI def fail(log_msg: nil) # FIXME: this is not translated... msg = 'Dolphin service menu installation failed' warn log_msg if log_msg system('kdialog', '--passivepopup', msg, '15') abort end def mime_type(filename) ret = `xdg-mime query filetype #{filename}`.strip return ret if $?.success? warn 'Failed to xdg-mime' fail(log_msg: "Failed to xdg-mime #{filename}: #{ret}") end def uncompress(filename, output) uncompressor = ARCHIVE_UNCOMPRESSORS.fetch(mime_type(filename)).to_s system(format(uncompressor, filename, output)) rescue KeyError => e # If a mimetype doesn't have an uncompressor mapped we'll get a keyerror. # we'll log the error but visually report the failure. fail(log_msg: "Unmapped compression format #{filename}; #{e.message}") end data_location = `qtpaths --writable-path GenericDataLocation`.strip unless $?.success? fail(log_msg: "Could not get GenericDataLocation #{data_location}") end servicedir = "#{data_location}/kservices5/ServiceMenus/" FileUtils.mkdir_p(servicedir) unless File.exist?(servicedir) if ARCHIVE.end_with?('.desktop') puts 'Single-File Service-Menu' puts ARCHIVE puts servicedir FileUtils.cp(ARCHIVE, servicedir) exit end dir = "#{ARCHIVE}-dir" FileUtils.rm_r(dir) if File.exist?(dir) FileUtils.mkdir(dir) fail(log_msg: 'uncompress failed') unless uncompress(ARCHIVE, dir) install_it = nil %w[install-it.sh install-it].find do |script| install_it = Dir.glob("#{dir}/**/#{script}")[0] end installer = nil %w[installKDE4.sh installKDE4 install.sh install].find do |script| installer = Dir.glob("#{dir}/**/#{script}")[0] end Dir.chdir(dir) do installed = false [install_it, installer].uniq.compact.each { |f| File.chmod(0o700, f) } if install_it puts "[servicemenuinstallation]: Trying to run install_it #{install_it}" installed = system(install_it) elsif installer puts "[servicemenuinstallation]: Trying to run installer #{installer}" %w[--local --local-install --install].any? do |arg| installed = system(installer, arg) end end fail unless installed end