#! /bin/tclsh
# <20200214.1840.37>
# a script to build the DEB file file given the RPM file
#
# This code assumes the following file directory structures:
# ~/rpmbuild/RPMS/noarch           contains the RPM file to convert
# ~/rpmbuild/DEB                   will be created (if needed) to hold result
set legal [list EMAIL NAME VERSION PACKAGE]
set legalmin {v h help}

proc echo {mess} {
  if {$::v} {puts $mess}
}

foreach parm $legalmin {
  set $parm 0
}

foreach parm $legal {
  if {[info exists env($parm)]} {
    set $parm $env($parm)
  }
}
foreach ent $argv {
  if {$ent == {} } {continue}
  lassign [split $ent "-" ] arg val ext
  if {$arg == {} } {
    # The following allows '--' instead of just '-'
    if {$val == {} } {set val $ext}
    if {$val ni $legalmin } {
      puts "Bad argument \"$ent\". Must be one of $legalmin."
      exit
    }
    set $val 1
    lappend changelist $val
    switch $val {
      help {set h 1}
    }
    continue
  }
  lassign [split $ent "="] arg val
  echo "$arg $val"
  if {$arg ni $legal} {
    puts "Bad argument \"$ent\". Must be one of $legal=path"
    exit 1
  }
  set $arg $val
  lappend changelist $arg
}
if {$h } {
  puts "
     This is the RPM to DEB conversion script.
     The following parameters may be either env or command line:

     NAME=<maintainers name> 
     EMAIL=<maintainers email address> 
     PACKAGE=<package name> 
     VERSION=<package version>

     We look for the RPM in ~/rpmbuild/RPMS/noarch
     and put the result in  ~/rpmbuild/DEB  

     We also accept the following command line arguments:
     -v        set verbose mode
     -h        prints this message and quits
     -help     prints this message and quits

     This process requires the two programs \"alien\" and \"fakeroot\"
"
exit 0
}
# this is a bit snitty but I don't want to spend a lot of time
# providing defaults

foreach ent $legal {
  if {![info exists $ent] || [set $ent] == {} } {
    puts "Must have $legal. Could not find $ent. Done!"
    exit 1
  }
}

set version $VERSION
set package $PACKAGE
set email   $EMAIL
set userName  " $NAME "

if {$v} {
  set alien1Ops "-g --verbose --scripts"
} else {
  set alien1Ops "-g --scripts"
}

set RPMfileDir [file normalize ~/rpmbuild/RPMS/noarch]
set file [glob -nocomplain $RPMfileDir/$package-$version* ]
echo "$file"
if {$file == {}} {
  puts "could not find RPM, looking for $RPMfileDir/$package-$version*"
  puts "Giving up..."
  exit 1
}
# Ok, lets try the conversion...
set env(EMAIL) $email
set targetDir [file normalize $RPMfileDir/../../DEB]
file mkdir $targetDir
set oldfiles [glob -nocomplain $targetDir/$package-$version*]
if {$oldfiles != {} } {file delete -force {*}$oldfiles}

cd  $targetDir
foreach ap {alien fakeroot} {
  if {[auto_execok $ap] == {}} {
    puts "Program \"$ap\" is required but could not be found."
    set fail 1
  }
}
if {[info exists fail] && $fail} {exit 1}

set s [exec fakeroot alien {*}$alien1Ops $file]
echo "$s"
# Fix names to be what we want...
# we assume the name we want to change is just before the email address
# we put in above
proc replaceName {data} {
  global email userName
  set idx [string first $email $data]
  if {$idx == -1} {return $data}
  # Now search back for "--" "\n" or ":"
  set find [list - \n : ]
  incr idx -1
  set idsx $idx
  incr idsx -1
  echo "Replacing... Maintainers name"
  while {[string range $data $idsx $idsx] ni $find && $idsx > 0} {
    incr idsx -1
  }
  return [string range $data 0 $idsx]$userName[string range $data $idx end]\n
}
package require fileutil
foreach fi [glob -nocomplain $targetDir/$package-$version/debian/*] {
  #puts "checking $fi"
  ::fileutil::updateInPlace $fi replaceName
}
# That done, lets finish the build
cd $targetDir/$package-$version
file attributes $targetDir/$package-$version/debian/rules -permissions ugo+x
set r [catch {exec fakeroot $targetDir/$package-$version/debian/rules binary-arch} rt]
echo "Errors from here down don't seem to matter as long as we have a *.deb file"
set t [glob -nocomplain $targetDir/$package*.deb]
if {$t != {} } {
  puts "SUCESS deb file is $t"
} else {  
  puts "$rt"
  puts "Something went wrong, there is no result package"
  exit 1
}
exit 0
#fakeroot alien -g --verbose --scripts /home/george/rpmbuild/RPMS/noarch/filerunner-13.6.17.16-1.noarch.rpm 
