Commit db234e8e16ef8f84039bc4884016f02342840506

Thomas de Grivel 2023-02-21T23:20:39

install, uninstall

diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index e418720..0d5f574 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -10,11 +10,11 @@ module Rbpkg
   end
 
   def self.cc
-    if `which cc`
+    if system("which cc >/dev/null 2>&1")
       "cc"
-    elsif `which gcc`
+    elsif system("which gcc >/dev/null 2>&1")
       "gcc"
-    elsif `which egcc`
+    elsif system("which egcc >/dev/null 2>&1")
       "egcc"
     end
   end
@@ -150,6 +150,10 @@ module Rbpkg
     "#{dir}/var/db/installed"
   end
 
+  def self.lib_dir
+    "#{dir}/lib"
+  end
+
   def self.lock(name)
     path = lock_path(name)
     #verbose(2, "Locking #{path}")
@@ -189,6 +193,31 @@ module Rbpkg
     "#{target_dir}/obj"
   end
 
+  @@os = nil
+  def self.os
+    os_fetch unless @@os
+    @@os
+  end
+
+  @@os_dir = nil
+  def self.os_dir
+    os_fetch unless @@os_dir
+    @@os_dir
+  end
+
+  def self.os_fetch
+    uname = `uname`
+    raise "uname failed" unless $?.success?
+    if uname == "Linux"
+      distrib = `uname -a | grep -io -e Debian -e gentoo -e Ubuntu | sed -e 's|gentoo|Gentoo|'`
+      @@os_dir = "#{uname}/#{distrib}"
+      @@os = "#{uname}_#{distrib}"
+    else
+      @@os = uname
+      @@os_dir = uname
+    end
+  end
+
   def self.package(repos)
     repos.each do |name|
       Repos.repo(name).package()
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index d756a44..ecc0ea8 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -2,8 +2,8 @@ require 'fileutils'
 
 class Rbpkg::Repo
 
-  @@git_remote_default = "origin"
-  @@git_branch_default = "master"
+  @@remote_default = "origin"
+  @@branch_default = "master"
 
   def autogen()
     r = if File.executable?("#{src_dir}/autogen")
@@ -17,6 +17,10 @@ class Rbpkg::Repo
     true
   end
 
+  def branch
+    @@branch_default
+  end
+
   def build
     if ! tag_present?("build-done")
       configure()
@@ -31,7 +35,7 @@ class Rbpkg::Repo
 
   def checkout(branch)
     sh_branch=sh_quote(branch)
-    cmd! "cd #{sh_quote(src_dir!)} && git fetch #{sh_quote(git_remote)} #{sh_branch} && git checkout #{sh_branch} && git submodule update"
+    cmd! "cd #{sh_quote(src_dir!)} && git fetch #{sh_quote(remote)} #{sh_branch} && git checkout #{sh_branch} && git submodule update"
   end
 
   def clean_all
@@ -108,6 +112,10 @@ class Rbpkg::Repo
     end
   end
 
+  def dependencies
+    nil
+  end
+
   def dir
     "#{File.basename(File.dirname(git_url))}/#{name}"
   end
@@ -133,10 +141,6 @@ class Rbpkg::Repo
     cmd! "cd #{sh_quote(src_dir!)} && git fetch"
   end
 
-  def git_branch
-    @@git_branch_default
-  end
-
   def git_clone
     if File.directory?(src_dir)
       false
@@ -144,13 +148,13 @@ class Rbpkg::Repo
       if ! File.directory?(src_parent_dir)
         cmd! "mkdir -p #{sh_quote(src_parent_dir)}"
       end
-      cmd! "cd #{sh_quote(src_parent_dir)} && git clone #{sh_quote(git_url)} -b #{sh_quote(git_branch)} #{sh_quote(name)}"
+      cmd! "cd #{sh_quote(src_parent_dir)} && git clone #{sh_quote(git_url)} -b #{sh_quote(branch)} #{sh_quote(name)}"
       cmd! "cd #{sh_quote(src_dir)} && git submodule init && git submodule update"
     end
   end
 
-  def git_remote
-    @@git_remote_default
+  def remote
+    @@remote_default
   end
 
   def git_url
@@ -186,7 +190,30 @@ class Rbpkg::Repo
   end
 
   def install
-    
+    return if system_package?
+    return if installed?
+    install_dependencies
+    build
+    package
+    cmd! "cd #{sh_quote(Rbpkg.prefix)} && unxz < #{sh_quote(package_tar_xz)} | pax -rd"
+    if ! File.directory?(installed_dir)
+      cmd! "mkdir -p #{sh_quote(installed_dir)}"
+    end
+    File.open(installed, "w") do |output|
+      output.puts "Version: #{version}"
+      File.open(package_checksum, "r") do |input|
+        while line = input.gets
+          output.write(line)
+        end
+      end
+    end
+    cmd! "libtool --mode=finish #{Rbpkg.lib_dir}"
+  end
+
+  def install_dependencies
+    if dependencies
+      Rbpkg.install(dependencies)
+    end
   end
 
   def installed
@@ -197,6 +224,10 @@ class Rbpkg::Repo
     File.file?(installed)
   end
 
+  def installed_dir
+    File.dirname(installed)
+  end
+
   def name
     self.class.name.to_s.downcase.scan(/::([^:]*)$/)[0][0]
   end
@@ -281,6 +312,10 @@ class Rbpkg::Repo
     File.dirname(src_dir)
   end
 
+  def system_package?
+    false
+  end
+
   def tag_dir
     "#{Rbpkg.tag_dir}/#{dir}/#{version}"
   end
@@ -316,7 +351,23 @@ class Rbpkg::Repo
   end
 
   def uninstall
-    
+    if installed?
+      File.open(installed, "r") do |input|
+        while line = input.gets
+          if (scan = line.scan(/^(SHA256 \((.*)\) = ([0-9A-Fa-f]*))$/)[0])
+            line = scan[0]
+            file = scan[1]
+            hash = scan[2]
+            path = "#{Rbpkg.prefix}/#{file}"
+            `echo 'SHA256 (#{sh_quote(path)}) = #{sh_quote(hash)}' | sha256 -c`
+            if $?.success?
+              cmd! "rm #{sh_quote(path)}"
+            end
+          end
+        end
+      end
+      File.delete(installed)
+    end
   end
 
   def version
@@ -325,3 +376,7 @@ class Rbpkg::Repo
     end
   end
 end
+
+def def_repo(name, klass)
+  Rbpkg::Repos.def_repo(name, klass)
+end
diff --git a/lib/rbpkg/repos.rb b/lib/rbpkg/repos.rb
index b1d3d05..e03f558 100644
--- a/lib/rbpkg/repos.rb
+++ b/lib/rbpkg/repos.rb
@@ -4,7 +4,7 @@ module Rbpkg
     @@repo_class = {}
     @@repo = {}
 
-    def self.define_repo(name, klass)
+    def self.def_repo(name, klass)
       raise "repo already exists: #{name.inspect}" if @@repo_class[name]
       @@repo_class[name] = klass
     end
diff --git a/lib/rbpkg/repos/c3.rb b/lib/rbpkg/repos/c3.rb
index 8a6340c..4c53b97 100644
--- a/lib/rbpkg/repos/c3.rb
+++ b/lib/rbpkg/repos/c3.rb
@@ -2,10 +2,14 @@ require "#{__FILE__}/../../repo"
 
 class Rbpkg::Repos::C3 < Rbpkg::Repo
 
+  def dependencies
+    ["libbsd", "libmd"]
+  end
+
   def git_url
     "https://git.kmx.io/c3-lang/c3.git"
   end
 
 end
 
-Rbpkg::Repos.define_repo("c3", Rbpkg::Repos::C3)
+def_repo("c3", Rbpkg::Repos::C3)