┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/settings/services/servicemenuinstallation
blob: e2d42bfbf4f0f92f48d3085a58db888df2e7133d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby

# Copyright (C) 2009 Jonathan Schmidt-Dominé <[email protected]>
# Copyright (C) 2019 Harald Sitter <[email protected]>
#
# 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