#!/usr/bin/env ruby
-require 'pathname'
+
+# Copyright (C) 2009 Jonathan Schmidt-Dominé <devel@the-user.org>
+# Copyright (C) 2019 Harald Sitter <sitter@kde.org>
+#
+# 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 = ARGV[0]
-$servicedir = `qtpaths --writable-path GenericDataLocation`.strip! + "/kservices5/ServiceMenus/"
-FileUtils.mkdir_p($servicedir) if !File.exist?($servicedir)
-if archive[(archive.length - 8)..(archive.length - 1)] == ".desktop"
- puts "Single-File Service-Menu"
- puts archive
- puts $servicedir
- FileUtils.cp(archive, $servicedir);
- exit(0)
+
+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 mimeType(filename)
- IO.popen("file --mime-type -b " + filename).gets().strip!()
+
+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
-$archivetypes = { "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"
-}
+
def uncompress(filename, output)
- system(sprintf($archivetypes[mimeType(filename)].to_s, 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
-dir = archive + "-dir"
-if File.exist?(dir)
- FileUtils.rm_r(dir)
+
+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)
-exit(-1) if !uncompress(archive, dir)
-# try: install-it.sh
-# try: install-it
-# try: installKDE4.sh
-# try: installKDE4
-# try: install.sh
-# try: install
-while true
- dd = Dir.new(dir)
- break if dd.count != 3
- odir = dir
- for entry in dd
- dir += "/" + entry if entry != "." && entry != ".."
- end
- if !File.directory? dir
- dir = odir
- break
- end
+
+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)
-def fail()
- system("kdialog --passivepopup \"Installation failed\" 15")
- exit(-1)
+
+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
-if !((File.exist?(file = "./install-it.sh") || File.exist?(file = "./install-it")) && system(file))
- fail() if !File.exist?(file = "./installKDE4.sh") && !File.exist?(file = "./installKDE4") && !File.exist?(file = "./install.sh") && !File.exist?(file = "./install")
- File.new(file).chmod(0700)
- fail() if !system(file + " --local") && !system(file + "--local-install") && !system(file + " --install")
-end