' Gambas module file

Public Dir As String
Private Path As String
Public ChangeLog As String
Public Signature As String

Public Const ALL_TARGETS As String = "*"

Private RPMBUILD_PROG As String
Private FAKEROOT_PROG As String
Private DKPG_PROG As String
Private TAR_1_13_PROG As String
Private AUTOCONF_PROG As String
Private AUTOMAKE_PROG As String
Private MAKEPKG_PROG As String
'Private MAKESELF_PROG As String

Private $sOutput As String
Private $bEnd As Boolean

Private $sName As String                ' Project name
Private $sVendor As String              ' Vendor
Private $sVendorPrefix As String        ' Vendor prefix
Private $sComponentName As String       ' Project name with a vendor prefix
Private $sLibraryName As String         ' Library name, with the version
Private $sUnsafePackageName As String   ' Package name, i.e project name with prefix, but with possible forbidden characters inside
Private $sCategories As String          ' Categories list
Private $sHome As String                ' Home user directory
Private $sVersion As String             ' Package version
Private $sVersionWithRelease As String  ' Package version with release number
Private $sDescription As String
Private $bDescription As Boolean

Private $sSupportPackage As String

Private MIN_VERSION As String
Private Const MAX_VERSION As String = "3.99.0"

Private $sFormatSingle As String
Private $sFormatMin As String
Private $sFormatMax As String
Private $sFormatMinMax As String
Private $sFormatEqual As String

Private Const QT4_SUPPORT As String = "qt4"
Private QT4_SUPPORT_PACKAGES As String[] = ["gb.qt4:gb.gui", "gb.qt4:gb.gui.qt", "gb.qt4.opengl:gb.gui.opengl", "gb.qt4.webkit:gb.gui.qt.webkit"]
Private Const QT5_SUPPORT As String = "qt5"
Private QT5_SUPPORT_PACKAGES As String[] = ["gb.qt5:gb.gui", "gb.qt5:gb.gui.qt", "gb.qt5.opengl:gb.gui.opengl", "gb.qt5.webkit:gb.gui.qt.webkit"]
Private Const GTK_SUPPORT As String = "gtk"
Private GTK_SUPPORT_PACKAGES As String[] = ["gb.gtk:gb.gui", "gb.gtk.opengl:gb.gui.opengl"]
Private Const GTK3_SUPPORT As String = "gtk3"
Private GTK3_SUPPORT_PACKAGES As String[] = ["gb.gtk3:gb.gui"] ', "gb.gtk.opengl:gb.gui.opengl"]

Private Const DEBIAN_STANDARD As String = "3.9.6"
Private Const DEBIAN_DEBHELPER_VERSION As String = "9"
Private DEBIAN_ICON_SIZES As Integer[] = [16, 22, 32, 48, 64, 128, 256, 512, 1024]

Public Sub CheckProgram()
  
  'IF $bChecked THEN RETURN

  '$bChecked = TRUE
  
  If Not Project.CheckProgram("dh_install") Then DKPG_PROG = "dh_install"
  If Not Project.CheckProgram("rpmbuild") Then RPMBUILD_PROG = "rpmbuild"
  If Not Project.CheckProgram("fakeroot") Then FAKEROOT_PROG = "fakeroot"
  If Not Project.CheckProgram("tar-1.13") Then 
    TAR_1_13_PROG = "tar-1.13"
  Else If Not Project.CheckProgram("tar") Then 
    TAR_1_13_PROG = "tar"
  Endif
  If Not Project.CheckProgram("autoconf") Then AUTOCONF_PROG = "autoconf"
  If Not Project.CheckProgram("automake") Then AUTOMAKE_PROG = "automake"
  If Not Project.CheckProgram("makepkg") Then MAKEPKG_PROG = "makepkg"
  'If Not Project.CheckProgram("makeself") Then MAKESELF_PROG = "makeself"
  
End

Public Sub CheckSystem(sSys As String) As String
  
  Select Case sSys
  
    Case "mageia", "fedora", "suse"
      If Not RPMBUILD_PROG Then Return Subst(("'&1' is missing."), "rpmbuild")
      
    Case "debian", "ubuntu"
      If Not DKPG_PROG Then Return Subst(("'&1' is missing."), "debhelper")
      If Not FAKEROOT_PROG Then Return Subst(("'&1' is missing."), "fakeroot")

    Case "slackware"
      If Not TAR_1_13_PROG Then Return Subst(("'&1' is missing."), "tar-1.13")

    Case "autotools"
      If Not AUTOCONF_PROG And If Not AUTOMAKE_PROG Then Return Subst(("'&1' and '&2' are missing."), "autoconf", "automake")
      If Not AUTOCONF_PROG Then Return Subst(("'&1' is missing."), "autoconf")
      If Not AUTOMAKE_PROG Then Return Subst(("'&1' is missing."), "automake")
      
    Case "archlinux"
      If Not MAKEPKG_PROG Then Return Subst(("'&1' is missing."), "makepkg")
      
    'Case "self"
    '  If Not MAKESELF_PROG Then Return Subst(("'&1' is missing."), "makeself")

    Case Else
      Return
      
  End Select
  
End

Private Sub ClearLog()
  
  FMakeInstall.ClearLog
  
End


Private Sub AddLog(sText As String)

  FMakeInstall.AddLog(sText)

End

Private Sub RemoveEmptyLines(sText As String) As String
  
  Return Split(sText, "\n", "", True).Join("\n")
  
End


Private Sub GetShortDesc(sText As String, Optional iMax As Integer) As String

  Dim iPos As Integer
  
  iPos = InStr(sText, ". ")
  If iPos = 0 Then iPos = Len(sText) + 1
  sText = Trim(Replace(Left(sText, iPos), "\n", " "))
  
  If iMax And If Len(sText) > iMax Then
    sText = Left(sText, iMax - 3) & "..."
  Endif
  
  Return sText
  
End

Private Sub FormatDesc(sText As String, Optional iMax As Integer) As String[]

  Dim aLine As New String[]
  Dim iPos As Integer
  Dim I As Integer
  Dim sLine As String
  Dim sCar As String

  If iMax = 0 Then Return Split(sText, "\n")

  aLine.Add("")

  While sText
    
    sCar = String.Left(sText)
    sText = String.Mid$(sText, 2)
    
    If sCar = "\n" Then
      aLine.Add("")
      Continue
    Endif
    
    aLine[aLine.Max] &= sCar
    
    sLine = aLine[aLine.Max]
    If String.Len(sLine) > iMax Then
      For I = Len(sLine) - 1 DownTo 1
        iPos = InStr(" .,;", Mid$(sLine, I, 1))
        If iPos Then
          aLine[aLine.Max] = Left(sLine, I)
          aLine.Add(Mid$(sLine, I + 1))
          Break
        Endif
      Next
      If I = 0 Then
        aLine[aLine.Max] = String.Left(sLine, iMax)
        aLine.Add(String.Mid$(sLine, iMax + 1))
      Endif
    Endif
    
  Wend
  
  Return aLine
  
End



Private Sub GetVersion() As String

  Return Project.FormatVersion()
        
End  

Private Sub InitMake(sSys As String)
  
  If Project.CreateEachDirectory Then
    Path = Dir &/ sSys
    Try Mkdir Dir
    Try Mkdir Path
  Else 
    Path = Dir
    Try Mkdir Path
  Endif

  $sName = Project.Name
  $sComponentName = $sName
  $sVendor = Project.Vendor
  $sVendorPrefix = Project.VendorPrefix
  If Not $sVendorPrefix Then $sVendorPrefix = $sVendor

  If Project.Type = Project.TYPE_COMPONENT Then
    If $sVendorPrefix Then
      If $sName Not Begins $sVendorPrefix & "." Then $sComponentName = $sVendorPrefix & "." & $sName
    Endif
    $sComponentName = String.LCase($sComponentName)
  Else If Project.Type = Project.TYPE_LIBRARY Then
    $sLibraryName = $sName & ":" & Project.MajorVersion & "." & Project.MinorVersion
  Endif

  '$sPrefix = GetPackagePrefix(Project.Vendor, Project.Prefix)
  $sUnsafePackageName = GetPackageFullName($sVendor, $sVendorPrefix, Project.Prefix, Project.PackageNameWithMajor, False)

  $sHome = System.User.Home

  $sCategories = Project.Categories[sSys]
  If $sCategories Then $sCategories &= ";"

  $sVersion = GetVersion()
  If sSys = "ubuntu" Then
    $sVersionWithRelease = $sVersion & "-0ubuntu" & CStr(Project.PackageVersion)
  Else
    $sVersionWithRelease = $sVersion & "-" & CStr(Project.PackageVersion)
  Endif
  
  $sDescription = RemoveEmptyLines(Project.Description)
  If $sDescription Then
    $bDescription = True
  Else
    $sDescription = "Missing description."
    $bDescription = False
  Endif

  With Project.Components
    If .Exist("gb.gui") Then
      $sSupportPackage = "gb.gui"
    Else If .Exist("gb.gui.qt") Then
      $sSupportPackage = "gb.gui.qt"
    Endif
  End With
  
End

Public Function Make() As Boolean

  Dim sSys As String
  Dim sMacro As String
  Dim bAdd As Boolean
  Dim sPath As String
  Dim aVer As String[]
  
  If Project.RuntimeVersion Then
    MIN_VERSION = Project.RuntimeVersion
  Else
    aVer = Split(System.FullVersion, ".")
    MIN_VERSION = aVer[0] & "." & aVer[1]
  Endif

  If Project.Systems.Exist("slackware") Then 
    If Project.MakeExecutable(True, True) Then Return True
  Endif  
  
  ' Save old rpm macro file
  sMacro = User.Home &/ ".rpmmacros"
  Try Move sMacro To sMacro & ".save"
  Try Kill sMacro

  ClearLog
  AddLog(("Saving CHANGELOG file."))
  sPath = Project.Dir &/ ".hidden" &/ "CHANGELOG"
  bAdd = Not Exist(sPath)
  'Try Mkdir Project.Dir &/ ".hidden"
  File.Save(sPath, ChangeLog)
  Project.RefreshKey(sPath)
  If bAdd Then VersionControl.AddFile(sPath)
  
  For Each sSys In Project.Systems
    If MakePackage(sSys) Then Return True
  Next

  ' Restore old rpm macro file
  Try Kill sMacro
  Try Move sMacro & ".save" To sMacro

  ' sPath = Project.Dir &/ ".extratest"
  ' bAdd = Not Exist(sPath)
  ' If Len(ExtraTest) > 0 Then
  '   AddLog(("Saving extra tests settings."))
  '   File.Save(sPath, ExtraTest)
  '   If bAdd Then VersionControl.AddFile(sPath)
  ' Else
  '   If Not bAdd Then VersionControl.RemoveFile(sPath)
  ' Endif
  
  AddLog("\n" & String(40, "=") & "\n")
  AddLog(("The packages have been successfully created.") & "\n")

End

Public Function GetChangeDate() As String

  Dim sLang As String
  Dim sDate As String

  sLang = System.Language
  System.Language = "C"
  sDate = Format$(Now, "ddd mmm dd yyyy") & " " & Project.Maintainer & " "
  If Project.Address Then sDate = sDate & "<" & Project.Address & "> "
  sDate &= GetVersion()
  System.Language = sLang
  Return sDate

End

Private Function IsConsoleProject() As Boolean

  Dim sLib As String
  Dim hComp As CComponent

  For Each sLib In Project.Components
    hComp = Project.Documentation[sLib]
    If hComp.IsForm() Then Return False
  Next

  Return True

End

Private Sub ComponentToPackageName(sComp As String) As String
      
  Return "gambas" & System.Version & "-" & Replace(sComp, ".", "-")

End

Private Sub IsRuntimeComponent(sComp As String) As Boolean
  
  If sComp = "gb.debug" Or If sComp = "gb.eval" Or If sComp = "gb.test" Then Return True
  If sComp = "gb.gui" Or If sComp Begins "gb.gui." Then Return True
  
End

Private Function GetDependencies(Optional aComp As String[]) As String
  
  Dim sRes As String
  Dim sComp As String
  Dim iPos As Integer
  Dim sTest As String
  
  If Not aComp Then 
    If Project.Type = Project.TYPE_COMPONENT Then
      aComp = Project.ReadComponentFile()["Require"]
    Else
      aComp = Project.Components
    Endif
  Endif
  
  If aComp Then
    For Each sComp In aComp
      
      iPos = InStr(sComp, ":")
      If iPos Then
        sTest = Mid$(sComp, iPos + 1)
        If Not Project.Components.Exist(sTest) Then Continue
        sComp = Left(sComp, iPos - 1)
      Endif
      
      If IsRuntimeComponent(sComp) Then Continue
      
      sRes &= GetDependency(sComp)
      'If sComp = "gb.form" Then sRes &= GetDependency("gb.form.stock")
    Next
  Endif
  
  For Each sComp In Project.Libraries
    sRes &= GetDependency(sComp)
  Next
  
  Return sRes
  
End

Private Function GetExtraDependencies(sSys As String) As String
  
  Dim sDep As String
  Dim aDep As String[]
  Dim sRes As String
  Dim sFormat As String
  
  If Project.SameDependencies Then sSys = ALL_TARGETS 'Project.Systems[0]
  
  'If Not sFormatMinMax Then sFormatMinMax = sFormatMin & sFormatMax
  
  For Each sDep In Split(Project.ExtraDependencies[sSys], "\n")
    
    aDep = Split(sDep, "\t")
    aDep.Resize(3)
    
    If aDep[1] And If aDep[2] Then
      If aDep[1] = aDep[2] Then
        sFormat = $sFormatEqual
      Else
        sFormat = $sFormatMinMax
      Endif
    Else If aDep[1] Then
      sFormat = $sFormatMin
    Else If aDep[2] Then
      sFormat = $sFormatMax
    Else
      sFormat = $sFormatSingle
    Endif
    
    sRes &= Subst(sFormat, aDep[0], aDep[1], aDep[2])
    
  Next
  
  Return sRes
  
End

Private Function GetExtraFiles(sSys As String, sFormat As String, Optional sFormatDir As String) As String
  
  Dim sFile As String
  Dim aFile As String[]
  Dim sRes As String
  Dim sFmt As String
  
  If Project.SameFiles Then sSys = ALL_TARGETS 'Project.Systems[0]
  
  For Each sFile In Split(Project.ExtraFiles[sSys], "\n")
    aFile = Split(sFile, "\t")
    aFile.Resize(2)
    
    sFmt = If(IsDir(Project.Dir &/ ".hidden" &/ aFile[0]), sFormatDir, sFormat)
    sRes &= Subst(sFmt, Shell$(aFile[0]), Shell$(aFile[1]), Shell$(Mid$(aFile[0], RInStr(aFile[0], "/") + 1))) & "\n"
    
  Next
  
  Return sRes
  
End

Private Sub FormatFileInList(sFile As String, Optional bDebian As Boolean) As String
  
  If InStr(sFile, " ") Then
    If bDebian Then
      sFile = Replace(sFile, " ", "[!#-~.\"!]")
    Else
      sFile = Chr$(34) & Replace(sFile, Chr$(34), "\\\"") & Chr$(34)
    Endif 
  Endif
  Return sFile
  
End


Private Function GetExtraFilesList(sSys As String, sPrefix As String, sFormat As String, Optional bDebian As Boolean) As String
  
  Dim sFile As String
  Dim aFile As String[]
  Dim sRes As String
  Dim sSrc, sDest, sDestDir As String
  
  If Project.SameFiles Then sSys = ALL_TARGETS 'Project.Systems[0]
  
  For Each sFile In Split(Project.ExtraFiles[sSys], "\n")
    aFile = Split(sFile, "\t")
    aFile.Resize(2)
    
    sSrc = FormatFileInList(sPrefix &/ aFile[0], bDebian)
    sDest = FormatFileInList(aFile[1] &/ File.Name(aFile[0]), bDebian)
    sDestDir = FormatFileInList(aFile[1], bDebian)
    sRes &= Subst(sFormat, sSrc, sDest, sDestDir) & "\n"
    
  Next
  
  Return sRes
  
End


Private Function CopyExtraFiles(sSys As String, sBuildDir As String)
  
  Dim sFile As String
  Dim aFile As String[]
  Dim sSrc As String
  Dim sDst As String
  
  If Project.SameFiles Then sSys = ALL_TARGETS 'Project.Systems[0]
  
  For Each sFile In Split(Project.ExtraFiles[sSys], "\n")
    aFile = Split(sFile, "\t")
    'aFile.Resize(2)
    sSrc = Project.Dir &/ ".hidden" &/ aFile[0]
    sDst = File.Dir(sBuildDir &/ aFile[1] &/ File.Name(aFile[0]))

    Project.MakeDir(sDst)
    If IsDir(sSrc) Then
      Shell "cp -R --preserve=timestamp " & Shell$(sSrc) & " " & Shell$(sDst) Wait
    Else
      Shell "cp --preserve=timestamp " & Shell$(sSrc) & " " & Shell$(sDst) Wait
      'Copy sSrc To sBuildDir &/ aFile[1] &/ File.Name(aFile[0])
    Endif
  Next
  
End

Private Function GetExtraFilesDir(sSys As String, sFormat As String, Optional bDebian As Boolean) As String
  
  Dim sFile As String
  Dim aFile As String[]
  Dim sRes As String
  
  If Project.SameFiles Then sSys = ALL_TARGETS 'Project.Systems[0]
  
  For Each sFile In Split(Project.ExtraFiles[sSys], "\n")
    
    aFile = Split(sFile, "\t")
    aFile.Resize(2)
    
    If bDebian Then
      sRes &= Subst(sFormat, aFile[1]) & "\n"
    Else
      sRes &= Subst(sFormat, Shell$(aFile[1])) & "\n"
    Endif
    
  Next
  
  Return sRes
  
End

Private Function GetDependency(sLib As String) As String
        
  Dim hLibrary As CLibraryInfo
  Dim sVersion As String
  Dim aMinVersion As String[]
  Dim aMaxVersion As String[]
  Dim sPackageName As String
  
  If Left(sLib) = "/" Or If Left(sLib) = ":" Then
    
    ' The package name dependency must not include the version!
    
    hLibrary = CLibraryInfo[sLib]
    sPackageName = hLibrary.GetDependencyName()
    sVersion = hLibrary.Version
    
    aMinVersion = Split(sVersion, ".")
    If aMinVersion[1] = "0" Then
      If aMinVersion[0] <> "0" Then
        aMinVersion[0] = CStr(CInt(aMinVersion[0]) - 1)
        aMinVersion[1] = "90"
      Endif
    Else
      aMinVersion[1] = CStr(CInt(aMinVersion[1]) - 1)
    Endif
    aMinVersion.Resize(3)
    aMinVersion[2] = "0"
    
    If hLibrary.CompatibleUntil Then
      aMaxVersion = Split(hLibrary.CompatibleUntil, ".")
    Else
      aMaxVersion = Split(sVersion, ".")
      aMaxVersion.Resize(3)
      aMaxVersion[2] = "90"
    Endif
    
  ' Else If Left(sLib) = ":" Then
  '     
  '   sLib = File.Name(sLib)
  '   iPos = RInStr(sLib, ":")
  '   sVersion = Mid$(sLib, iPos + 1)
  '   sLib = Left(sLib, iPos - 1)
  '     
  '   sPackageName = ComponentToPackageName(sLib)
  '   
  '   aMinVersion = Split(sVersion, ".")
  '   aMaxVersion = Split(sVersion, ".")
    
  Else
    
    sPackageName = ComponentToPackageName(sLib)
    
    aMinVersion = Split(MIN_VERSION, ".")
    aMaxVersion = Split(MAX_VERSION, ".")
    
  Endif
       
  Return Subst($sFormatMinMax, sPackageName, aMinVersion.Join("."), aMaxVersion.Join("."))

End

Private Sub PrintOnlyShowIn(cLine As Collection, sCategories As String)
  
  Dim aCat As String[] = Split(sCategories, ";", "", True)
  Dim sCat As String
  Dim aOnly As New String[]
  
  If aCat.Exist("Screensaver") Or If aCat.Exist("TrayIcon") Or If aCat.Exist("Applet") Or If aCat.Exist("Shell") Then
  
    For Each sCat In aCat
      If sCat = "KDE" Or If sCat = "GNOME" Or If sCat = "ROX" Or If sCat = "XFCE" Then
        aOnly.Add(sCat)
      Endif
    Next
    
    If aOnly.Count Then cLine["ShowOnlyIn"] = aOnly.Join(";") & ";"
    
  Endif
  
End

Private Sub MakeOneLine(sStr As String) As String

  Dim iLen As Integer
  
  sStr = Replace(sStr, "\r", "")
  sStr = Replace(sStr, "\n", " ")
  sStr = Replace(sStr, "\t", " ")
  
  Repeat
    iLen = Len(sStr)
    sStr = Replace(sStr, "  ", " ")
  Until Len(sStr) = iLen
  
  Return Trim(sStr)

End

Private Sub PrintTranslatedDesktopEntry(cLine As Collection, sEntry As String, sText As String)

  Dim sLang As String
  Dim sTrans As String
  
  cLine[sEntry] = MakeOneLine(sText)
  
  For Each sLang In Project.GetTranslations()
    sTrans = CTranslation.FindTranslation(Language.GetPath(sLang, "po"), sText)
    If sTrans Then cLine[sEntry & "[" & sLang & "]"] = MakeOneLine(sTrans)
  Next

End

Private Sub PrintDesktopFile(hFile As File, sExec As String, sIcon As String, Optional sExtraCat As String)

  Dim cLine As New Collection
  Dim sExtra As String
  Dim iPos As Integer
    
  Print #hFile, "[Desktop Entry]"
  Print #hFile, "Version=1.0"

  If Project.Title Then
    PrintTranslatedDesktopEntry(cLine, "Name", Project.Title)
  Else
    PrintTranslatedDesktopEntry(cLine, "Name", Project.Name)
  Endif

  If $bDescription Then PrintTranslatedDesktopEntry(cLine, "Comment", Project.Description)
  
  cLine["Exec"] = sExec
  cLine["Icon"] = sIcon
  cLine["Terminal"] = IIf(IsConsoleProject(), "true", "false")
  cLine["Type"] = "Application"
  cLine["StartupNotify"] = "true"
  cLine["Categories"] = sExtraCat & $sCategories
  If Project.MimeTypes.Count Then cLine["MimeType"] = Project.MimeTypes.Join(";")
  PrintOnlyShowIn(cLine, $sCategories)
  
  If Trim(Project.ExtraDesktop) Then
    For Each sExtra In Split(Project.ExtraDesktop, "\n")
      sExtra = Trim(sExtra)
      If Not sExtra Then Continue
      If InStr("#;=", Left(sExtra)) Then Continue
      iPos = InStr(sExtra, "=")
      cLine[RTrim(Left(sExtra, iPos - 1))] = LTrim(Mid$(sExtra, iPos + 1))
    Next
  Endif
  
  For Each sExtra In ["Name", "Comment", "Exec", "Icon", "Terminal", "Type", "StartupNotify", "Categories", "MimeType", "ShowOnlyIn"]
    If cLine[sExtra] Then
      Print #hFile, sExtra; "="; cLine[sExtra]
      cLine.Remove(sExtra)
    Endif
  Next
  
  For Each sExtra In cLine
    Print #hFile, cLine.Key; "="; sExtra
  Next
  
End


Private Sub RunCommand(sCmd As String, Optional sDir As String, Optional bIgnoreStderr As Boolean)

  Dim aCmd As New String[]
  
  $sOutput = ""
  $bEnd = False
  If sDir Then
    AddLog("cd " & Shell$(sDir))
    aCmd.Push("cd " & SConv(Shell$(sDir)))
  Endif
  AddLog(sCmd)
  aCmd.Push(sCmd)
  If bIgnoreStderr = False Then aCmd.Last &= " 2>&1"
  Shell aCmd.Join(" && ") For Input As "Process"
  Repeat
    Sleep 0.02 
    Wait
  Until $bEnd
  
End

Private Sub MakeSupportPackage(sSys As String)

  Dim sSupport As String
  Dim aPackage As String[]
  'slackware doesn't use support packages
  If sSys = "slackware" Then Return
  
  If $sSupportPackage = "gb.gui" Then
  
    sSupport = GTK_SUPPORT
    aPackage = GTK_SUPPORT_PACKAGES
    AddLog("\n============ " & Subst(("Making &1 support package..."), "GTK+"))
    GoSub MAKE_PACKAGE
  
    If Not Project.Components.Exist("gb.gui.opengl") Then
      sSupport = GTK3_SUPPORT
      aPackage = GTK3_SUPPORT_PACKAGES
      AddLog("\n============ " & Subst(("Making &1 support package..."), "GTK+3"))
      GoSub MAKE_PACKAGE
    Endif
    
  Endif

  sSupport = QT4_SUPPORT
  aPackage = QT4_SUPPORT_PACKAGES
  AddLog("\n============ " & Subst(("Making &1 support package..."), "QT4"))
  GoSub MAKE_PACKAGE

  sSupport = QT5_SUPPORT
  aPackage = QT5_SUPPORT_PACKAGES
  AddLog("\n============ " & Subst(("Making &1 support package..."), "QT5"))
  GoSub MAKE_PACKAGE
  
  Return
  
MAKE_PACKAGE:
  
  Select Case sSys
  
    Case "mageia", "fedora", "suse"
      MakeRpmSupportPackage(sSys, sSupport, aPackage)
  
    Case "debian", "ubuntu"
      MakeDebSupportPackage(sSys, sSupport, aPackage)
      
    Case "slackware"
      'we shouldn't get here, but if we do, do nothing as not needed for slackware
      'MakeTgzSupportPackage(sSys, sSupport, aPackage)
   
    Case "archlinux"
      MakeArchSupportPackage(sSys, sSupport, aPackage)
      
    Default
      
  End Select
  
  Return
  
End


Private Function MakePackage(sSys As String) As Boolean

  Dim sErr As String
  
  AddLog("\n" & String(78, "=") & "\n")
  AddLog(String.UCase(Subst(("Creating package for &1."), FMakeInstall.GetSystemName(sSys))))
  AddLog("\n")

  InitMake(sSys)
  
  Select Case sSys
  
    Case "mageia", "fedora", "suse"
      MakeRpmPackage(sSys)
  
    Case "debian", "ubuntu"
      MakeDebPackage(sSys)
      
    Case "slackware"
      MakeTgzPackage(sSys)
   
    Case "autotools"
      MakeAutotoolsPackage(sSys)
       
    Case "archlinux"
      MakeArchPackage(sSys)

    Default
      Return True
    
  End Select
  
  If $sSupportPackage Then MakeSupportPackage(sSys)
  
Catch

  sErr = Error.Text
  AddLog(("The package build has failed.") & "\n" & Error.Where & ": " & sErr)
  FMakeInstall.ShowError("<b>" & ("The package build has failed.") & "</b><br>" & sErr)
  Application.Busy = 0
  Return True

End

Private Sub GetDebianBuildPackageCommand() As String

  Dim sCmd As String

  sCmd = "dpkg-buildpackage -d -rfakeroot"
  If Project.PackageSignature Then
    sCmd &= " -k" & Shell(Project.Address)
  Else
    sCmd &= " -uc -us"
  Endif
  Return sCmd

End

' From Debian Policy Manual:
' Package names (both source and binary, see Package, Section 5.6.7) must consist only of lower case letters (a-z), digits (0-9), plus (+) and minus (-) signs, and periods (.). 
' They must be at least two characters long And must start With an alphanumeric character.


Private Sub FormatDebPackageName(sName As String) As String

  Dim I As Integer
  Dim sCar As String
  Dim sRes As String

  For I = 1 To Len(sName)
    sCar = Mid$(sName, I, 1)
    If Not IsLetter(sCar) And If Not IsDigit(sCar) And If InStr("+-", sCar) = 0 Then sCar = "-"
    sRes &= sCar
  Next
  
  Return sRes

End

Private Function MakeDebPackage(sSys As String)

  Dim hFile As File
  Dim sCmd As String
  Dim sIcon As String
  Dim sIconDir As String
  Dim sArch As String
  Dim sPackageName As String
  Dim sPackagePath As String
  Dim sLine As String
  Dim hImageStat As ImageStat
  Dim iSize As Integer
  Dim sPath As String
  Dim W As Integer
  Dim H As Integer
  Dim sExec As String
      
  Inc Application.Busy
  
  $sFormatSingle = ", &1"
  $sFormatMin = ", &1 (>= &2)"
  $sFormatMax = ", &1 (<< &3)"
  $sFormatMinMax = $sFormatMin & $sFormatMax
  $sFormatEqual = $sFormatMinMax
  
  sPackageName = FormatDebPackageName($sUnsafePackageName)
  
  '$sName = Replace$($sName, "_", "-")

  ' Step 1. Create the build directory

  AddLog(("Making build directory.")) 

  RunCommand("rm -f " & Shell(Path &/ Subst("&1_&2", sPackageName, $sVersion)) & "*")

  sPackagePath = Path &/ Subst("&1-&2", sPackageName, $sVersion)
  RunCommand("rm -Rf " & Shell$(sPackagePath))
  RunCommand("rm -Rf " & Shell$(sPackagePath & ".orig"))

  Mkdir sPackagePath
  Mkdir sPackagePath &/ "src"
  Mkdir sPackagePath &/ "debian"
  
  Mkdir sPackagePath &/ "debian/source"
  File.Save(sPackagePath &/ "debian/source/format", "1.0")

  sArch = sPackagePath &/ "src/raw.tar.gz"
  
  ' Create icons
    
  sIconDir = Project.Dir &/ ".hidden/.icons"
  Try Shell.RmDir(sIconDir)
  Shell.MkDir(sIconDir)
  
  sIcon = sPackageName
  
  Project.StretchIcon(Project.Dir &/ Project.Icon, 32).Save(sIconDir &/ sIcon & ".png")
  
  Try hImageStat = ImageStat(Project.Dir &/ Project.Icon)
  If Error Then
    W = 32
    H = 32
  Else
    W = hImageStat.Width
    H = hImageStat.Height
  Endif
  
  For Each iSize In DEBIAN_ICON_SIZES
    If W >= iSize And If H >= iSize Then
      Mkdir sIconDir &/ CStr(iSize)
      Project.StretchIcon(Project.Dir &/ Project.Icon, iSize).Save(sIconDir &/ CStr(iSize) &/ sIcon & ".png")
    Endif
  Next
  
  Project.MakeSourcePackageTo(sArch)
  
  ' Remove the icon dir from the project now it has been archived
  Shell.RmDir(sIconDir)
  
  sCmd = "cd " & Shell$(sPackagePath &/ "src") & "; tar xzf raw.tar.gz >/dev/null" 
  Shell sCmd Wait 
  
  ' Rename the source archive project directory to the package name
  'TRY MOVE Path &/ (sPrefix & Subst("&1", Project.Name)) TO sPackagePath
  Try Kill sArch
  Try Shell "cp -Rf " & Shell(sPackagePath) & " " & Shell(sPackagePath & ".orig") Wait


  ' Step 2. Make the desktop file
  If Project.Type = Project.TYPE_NORMAL Then
    AddLog(("Creating desktop file..."))
    hFile = Open sPackagePath &/ "debian/" & sPackageName & ".desktop" For Create
    PrintDesktopFile(hFile, $sName, sIcon)
    Close #hFile
  Endif 
  
  'Step 3. Create debianization needed files: control, rules
  'debian/copyright
  AddLog(("Sources are being debianizated."))
  hFile = Open sPackagePath &/ "debian/copyright" For Create 
  Print #hFile, "This package was debianized by " & Project.Maintainer & " <" & Project.Address & "> on"
  Print #hFile, Format$(Date, "ddd, dd mmm yyyy hh:nn:ss") & "+0000"
  Print #hFile
  Print #hFile, "Copyright Holder: " & Project.Maintainer & " <" & Project.Address & ">"
  Print #hFile
  Print #hFile, "License:"
  Print #hFile
  Print #hFile, If(Project.License, Project.License, "GNU General Public License") 
  Print #hFile
  Print #hFile, "Created by: "; Project.Authors
  Close #hFile
  
  'debian/changelog
  hFile = Open sPackagePath &/ "debian/changelog" For Create 
  Print #hFile, ChangelogToDebChangelog(sPackageName)
  Close #hFile

  'debian/compat
  hFile = Open sPackagePath &/ "debian/compat" For Create 
  Print #hFile, DEBIAN_DEBHELPER_VERSION
  Close #hFile
    
  'debian/menu
  If Project.Type = Project.TYPE_NORMAL Then
    hFile = Open sPackagePath &/ "debian/menu" For Create 
    Print #hFile, "?package("; sPackageName; "):needs=\""; IIf(IsConsoleProject(), "text", "X11"); "\" section=\""; Project.Menus[sSys]; "\"\\"
    Print #hFile, "title=\""; IIf(Project.Title, Project.Title, Project.Name); "\"\\" 
    Print #hFile, "command=\"/usr/bin/"; $sName; "\"\\"
    Print #hFile, "icon=\"/usr/share/pixmaps/"; sIcon; ".png\""
    Close #hFile
  Endif 
  
  'debian/dirs
  hFile = Open sPackagePath &/ "debian/dirs" For Create
  Select Case Project.Type
    Case Project.TYPE_COMPONENT 
      Print #hFile, "usr/share/gambas"; System.Version; "/info"
      Print #hFile, "usr/lib/gambas"; System.Version 
      If Exist(Project.Dir &/ ".hidden/control") Then Print #hFile, "usr/share/gambas" & System.Version & "/control/" & $sComponentName
    Case Project.TYPE_LIBRARY
      Print #hFile, "usr/lib/gambas"; System.Version; "/"; LCase($sVendor)
    Case Project.TYPE_NORMAL
      Print #hFile, "usr/bin"
      Print #hFile, "usr/share/applications"
      Print #hFile, "usr/share/pixmaps"
      For Each iSize In DEBIAN_ICON_SIZES
        Print #hFile, "usr/share/icons/hicolor/"; iSize; "x"; iSize; "/apps"
      Next
  End Select
  Print #hFile, GetExtraFilesDir(sSys, "&1", True)
  Close #hFile
  
  'debian/install
  hFile = Open sPackagePath &/ "debian/install" For Create
  
  Select Case Project.Type
    
    Case Project.TYPE_COMPONENT
      
      Print #hFile, "src/"; $sName; "/"; $sComponentName; ".gambas usr/lib/gambas" & System.Version
      If Exist(Project.Dir &/ ".hidden/control") Then Print #hFile, "src/"; $sName; "/.hidden/control/*.png usr/share/gambas" & System.Version &/ "control" &/ $sComponentName
      
    Case Project.TYPE_LIBRARY
      
      Print #hFile, "src/"; $sName; "/"; $sLibraryName; ".gambas usr/lib/gambas" & System.Version; "/"; LCase($sVendor)
      
    Case Project.TYPE_NORMAL
    
      Print #hFile, "src/"; $sName; "/"; $sName; " usr/bin"
      Print #hFile, "debian/"; sPackageName; ".desktop usr/share/applications"

      Print #hFile, "src/"; $sName; "/.hidden/.icons/"; sIcon; ".png usr/share/pixmaps"
      For Each iSize In DEBIAN_ICON_SIZES
        sPath = "src" &/ $sName &/ ".hidden/.icons" &/ CStr(iSize) &/ sIcon & ".png"
        If Exist(sPackagePath &/ sPath) Then Print #hFile, sPath; " /usr/share/icons/hicolor/"; iSize; "x"; iSize; "/apps"
      Next

  End Select
  
  Print #hFile, GetExtraFilesList(sSys, "src/" & $sName & "/.hidden", "&1 &3", True)
  
  Close #hFile  
 
  'debian/control
  hFile = Open sPackagePath &/ "debian/control" For Create
  Print #hFile, "Source: "; sPackageName
  Print #hFile, "Section: contrib/"; Project.Groups[sSys]
  Print #hFile, "Priority: optional"
  Print #hFile, "Maintainer: "; Project.Maintainer; " <"; Project.Address; ">"
  Print #hFile, "Build-Depends: debhelper (>= "; DEBIAN_DEBHELPER_VERSION; "), gambas"; System.Version; "-dev (>= "; MIN_VERSION; "), gambas"; System.Version; "-dev (<< "; MAX_VERSION; ")";
  Print #hFile, GetDependencies();
  Print #hFile, GetExtraDependencies(sSys);
  Print #hFile

  Print #hFile, "Standards-Version: "; DEBIAN_STANDARD
  Print #hFile
  Print #hFile, "Package: "; sPackageName
  Print #hFile, "Architecture: all"
  Print #hFile, "Depends: gambas"; System.Version; "-runtime (>= "; MIN_VERSION; "), gambas"; System.Version; "-runtime (<< "; MAX_VERSION; ")";
  
  Print #hFile, GetDependencies();
  Print #hFile, GetExtraDependencies(sSys);
  Print #hFile

  Print #hFile, GetShortDesc("Description: " & $sDescription, 80)
  ' If Project.Title Then 
  '   Print #hFile, Project.Title
  ' Else 
  '   Print #hFile, Project.Name
  ' Endif 
  
  For Each sLine In FormatDesc($sDescription, 80)
    Print #hFile, " "; sLine
  Next

  Select Case Project.Type
    Case Project.TYPE_NORMAL
      Print #hFile, " This program is written in Gambas."
    Case Project.TYPE_LIBRARY
      Print #hFile, " This is a Gambas library."
    Case Project.TYPE_COMPONENT
      Print #hFile, " This is a Gambas component written in Gambas."
  End Select
  
  Close #hFile
  
  'debian/rules
  hFile = Open sPackagePath &/ "debian/rules" For Create
  Output To #hFile
  Print "#!/usr/bin/make -f"
  Print "build: build-stamp"
  Print "build-stamp:"
  Print "\tdh_testdir"
  Print "\ttouch build-stamp"
  Print "\t"; Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False) & " src" &/ $sName
  Print "\tgba"; System.Version; " -o ";
  
  Select Case Project.Type
    Case Project.TYPE_COMPONENT
      sExec = "src" &/ $sName &/ $sComponentName & ".gambas"
    Case Project.TYPE_LIBRARY
      sExec = "src" &/ $sName &/ $sLibraryName & ".gambas"
    Case Project.TYPE_NORMAL
      sExec = "src" &/ $sName &/ $sName
  End Select
  Print Shell(sExec); " src" &/ $sName
  
  ' If Project.Type = Project.TYPE_COMPONENT 
  '   If $sName <> $sComponentName Then
  '     Print "\tmv "; Shell$("src" &/ $sName &/ $sName & ".gambas"); " "; Shell$("src" &/ $sName &/ $sComponentName & ".gambas")
  '   Endif
  ' Else If Project.Type = Project.TYPE_LIBRARY Then
  '   Print "\tmv "; Shell$("src" &/ $sName &/ $sName & ".gambas"); " "; Shell$("src" &/ $sName &/ $sLibraryName & ".gambas")
  ' Endif
  
  Print 
  Print "clean:"
  Print "\tdh_testdir"
  Print "\tdh_testroot"
  Print "\trm -Rf "; Shell("src" &/ $sName &/ ".gambas")  
  Print "\trm -f "; Shell(sExec)
  Print "\trm -f build-stamp install-stamp"
  Print "\tdh_clean"
  Print
  Print "install: build"
  Print "\tdh_testdir"
  Print "\tdh_testroot"
  Print "\tdh_prep"
  Print "\tdh_installdirs" 
  If Project.Type = Project.TYPE_NORMAL Then
    If $sName <> sPackageName Then Print "\tln -s " & Shell$($sName) & " \"$(CURDIR)\"/debian" &/ sPackageName &/ "/usr/bin" &/ Shell$(sPackageName)
  Else If Project.Type = Project.TYPE_COMPONENT Then 
    Print "\tcp src" &/ $sName & "/.component \"$(CURDIR)\"/debian/" & sPackageName & "/usr/lib/gambas" & System.Version & "/" & Shell$($sComponentName) & ".component"
    Print "\tcp src" &/ $sName & "/.info \"$(CURDIR)\"/debian/" & sPackageName & "/usr/share/gambas" & System.Version & "/info/" & Shell$($sComponentName) & ".info " ""
    Print "\tcp src" &/ $sName & "/.list \"$(CURDIR)\"/debian/" & sPackageName & "/usr/share/gambas" & System.Version & "/info/" & Shell$($sComponentName) & ".list"
  Endif 
  Print "\ttouch install-stamp"
  Print 
  Print "binary-arch: build install"
  Print "\tdh_testdir"
  Print 
  Print "binary-indep: build install"
  Print "\tdh_testdir -i"
  Print "\tdh_testroot -i"
  Print "\tdh_installdocs -i"
  Print "\tdh_installchangelogs -i"
  Print "\tdh_install -i"
  Print "\tdh_installmenu"
  Print "\tdh_compress -i"
  Print "\tdh_fixperms -i"
  Print "\tdh_installdeb -i"
  Print "\tdh_gencontrol -i"
  Print "\tdh_md5sums -i"
  Print "\tdh_builddeb -i"
  Print 
  Print "binary: binary-indep binary-arch"
  Print ".PHONY: build clean binary-indep binary-arch binary install"
  Output To Default
  Close #hFile
  Try Shell "chmod +x " & Shell(sPackagePath &/ "debian/rules") Wait

  ' Step 4. Build package

  AddLog(("Creating package..."))
  'sCmd = "cd " & Shell$(sPackagePath) & "; fakeroot dpkg-buildpackage -d"
  'sCmd = "fakeroot dpkg-buildpackage -d"
  
  RunCommand(GetDebianBuildPackageCommand(), sPackagePath)
  
  Dec Application.Busy

  If Process.LastValue > 2 Then Error.Raise(("'dpkg-buildpackage' has failed."))

  Try Shell "rm -Rf " & Shell$(sPackagePath) Wait
  Try Shell "rm -f " & Shell$(Path) &/ "*.dsc.asc" Wait 

End

Private Function MakeDebSupportPackage(sSys As String, sSupport As String, aComp As String[])

  Dim hFile As File
  Dim sPackageName As String
  Dim sPackagePath As String
  Dim sDesc As String
  Dim sLine As String
  Dim sRelease As String
      
  Inc Application.Busy
  
  $sFormatSingle = ", &1"
  $sFormatMin = ", &1 (>= &2)"
  $sFormatMax = ", &1 (<< &3)"
  $sFormatMinMax = $sFormatMin & $sFormatMax
  $sFormatEqual = $sFormatMin & Replace($sFormatMax, "<<", "<=")
  
  sPackageName = FormatDebPackageName($sUnsafePackageName) & "-" & sSupport
  
  sRelease = CStr(Project.PackageVersion)
  
  ' Step 1. Create the build directory

  AddLog(("Making build directory.")) 
  sPackagePath = Path &/ Subst("&1-&2", sPackageName, $sVersion)
  
  Try Shell "rm -Rf " & Shell$(sPackagePath) Wait
  Mkdir sPackagePath
  Mkdir sPackagePath &/ "debian"

  'Step 3. Create debianization needed files: control, rules
  'debian/copyright
  AddLog(("Sources are being debianizated."))
  hFile = Open sPackagePath &/ "debian/copyright" For Create 
  Print #hFile, "This package was debianized by " & Project.Maintainer & " <" & Project.Address & "> on"
  Print #hFile, Format$(Date, "ddd, dd mmm yyyy hh:nn:ss") & "+0000"
  Print #hFile
  Print #hFile, "Copyright Holder: " & Project.Maintainer & " <" & Project.Address & ">"
  Print #hFile
  Print #hFile, "License:"
  Print #hFile
  Print #hFile, If(Project.License, Project.License, "GNU General Public License") 
  Close #hFile
  
  'debian/changelog
  hFile = Open sPackagePath &/ "debian/changelog" For Create 
  Print #hFile, ChangelogToDebChangelog(sPackageName)
  Close #hFile

  'debian/compat
  hFile = Open sPackagePath &/ "debian/compat" For Create 
  Print #hFile, DEBIAN_DEBHELPER_VERSION
  Close #hFile
    
  'debian/control
  hFile = Open sPackagePath &/ "debian/control" For Create
  Print #hFile, "Source: "; sPackageName
  Print #hFile, "Section: contrib/"; Project.Groups[sSys]
  Print #hFile, "Priority: optional"
  Print #hFile, "Maintainer: "; Project.Maintainer; " <"; Project.Address; ">"
  Print #hFile, "Build-Depends: debhelper (>= "; DEBIAN_DEBHELPER_VERSION; ")"
  Print #hFile, "Standards-Version: "; DEBIAN_STANDARD
  Print #hFile
  Print #hFile, "Package: "; sPackageName
  Print #hFile, "Architecture: all"
  Print #hFile, "Depends: "; FormatDebPackageName($sUnsafePackageName);; "(>=";; $sVersionWithRelease;; "), "; FormatDebPackageName($sUnsafePackageName);; "(<=";; $sVersionWithRelease; ")"; GetDependencies(aComp)
  
  sDesc = sSupport & " package for " & If(Project.Title, Project.Title, Project.Name)
  
  Print #hFile, GetShortDesc("Description: " & sDesc, 80)
  
  sDesc = "This is the dummy " & sDesc
  
  For Each sLine In FormatDesc(sDesc)
    Print #hFile, " "; sLine
  Next
  
  
  ' debian/rules
  hFile = Open sPackagePath &/ "debian/rules" For Create
  Output To #hFile
  Print "#!/usr/bin/make -f"
  Print
  Print "build build-arch build-indep:"
  Print
  Print "clean:"
  Print "\tdh_testdir"
  Print "\tdh_clean"
  Print
  Print "install: build"
  Print "\tdh_testdir"
  Print "\tdh_testroot"
  Print "\tdh_prep"
  Print
  Print "binary-arch: install"
  Print
  Print "binary-indep: install"
  Print "\tdh_testdir"
  Print "\tdh_testroot"
  Print "\tdh_install"
  Print "\tdh_installdocs"
  Print "\tdh_installchangelogs"
  Print "\tdh_compress"
  Print "\tdh_fixperms"
  Print "\tdh_installdeb"
  Print "\tdh_gencontrol"
  Print "\tdh_md5sums"
  Print "\tdh_builddeb"
  Print
  Print "binary: binary-indep binary-arch"
  Print ".PHONY: build clean binary-indep binary-arch binary install"
  Print
  Output To Default
  Close #hFile
  Try Shell "chmod +x " & Shell(sPackagePath &/ "debian/rules") Wait

  ' Step 4. Build package

  AddLog(("Creating package..."))
  RunCommand(GetDebianBuildPackageCommand(), sPackagePath)
  
  Dec Application.Busy

  If Process.LastValue > 2 Then Error.Raise(("'dpkg-buildpackage' has failed."))

  Try Shell "rm -Rf " & Shell$(sPackagePath) Wait
  Try Shell "rm -f " & Shell$(Path) &/ "*.dsc.asc" Wait 

End

Private Sub ChangelogToDebChangelog(sPackageName As String) As String
 
  Dim aLine As String[]
  Dim sLine As String
  Dim debChangelog As String
  Dim sDate As String
  Dim sBits As String[]
  Dim previousLine As String
  Dim sWord As String
  Dim sNextWord As String
  Dim aWord As String[]
  
  aLine = Split(ChangeLog, "\n")  
  
  For Each sLine In aLine
    
    If Left$(sLine, 1) = "*" Then 
      
      If sDate <> "" Then
        'Debian packages don't accept an empty changelog line for the release:
        If previousLine = sPackageName & " (" & $sVersionWithRelease & ") unstable; urgency=low" & "\n\n" Then 
          debChangelog = debChangelog & "  * New release version\n"
        Endif   
        debChangelog = debChangelog & "\n -- " & Project.Maintainer & " <" & Project.Address & ">  " & sDate & "\n\n"
      Endif
      sbits = Split(Trim$(sLine), " ")
      sDate = sbits[1] & ", " & sbits[3] & " " & sbits[2] & " " & sbits[4] & " 12:00:00 +0000"
      'sVersion = GetDebVersion()
      previousLine = sPackageName & " (" & $sVersionWithRelease & ") unstable; urgency=low" & "\n\n"
      debChangelog &= previousLine
    
    Else 
      
      If Trim$(sLine) <> "" Then
        
        aWord = Split(Right$(sLine, -2), " ")
        previousLine = "  * "
        sLine = ""
        For Each sWord In aWord
          Repeat
            If String.Len(sWord) > 76 Then
              sNextWord = String.Mid$(sWord, 77)
              sWord = String.Left(sWord, 76)
            Else
              sNextWord = ""
            Endif
            If String.Len(sLine & sWord) > 76 Then
              previousLine &= sLine & "\n    "
              sLine = sWord
            Else
              sLine = LTrim(sLine & " " & sWord)
            Endif
            sWord = sNextWord
          Until Not sWord
        Next
        If sLine Then previousLine &= sLine & "\n"
        
        debChangelog &= previousLine
        
      Endif
      
    Endif   
    
  Next 
  
  debChangelog &= "\n -- " & Project.Maintainer & " <" & Project.Address & ">  " & sDate & "\n"
  
  Return debChangelog 

End

Private Sub FormatRpmPackageName(sName As String) As String

  Dim I As Integer
  Dim sCar As String
  Dim sRes As String
  Dim iLen As Integer

  For I = 1 To Len(sName)
    sCar = Mid$(sName, I, 1)
    If Not IsLetter(sCar) And If Not IsDigit(sCar) Then sCar = "-"
    sRes &= sCar
  Next
  
  Do
    iLen = Len(sRes)
    sRes = Replace(sRes, "--", "-")
    If Len(sRes) = iLen Then Break
  Loop
  
  Return sRes

End

Private Function MakeRpmPackage(sSys As String)

  Dim sSrc As String
  Dim sDir As String
  Dim sCmd As String
  Dim hFile As File
  Dim sArch As String
  Dim sOutput As String
  Dim sLang As String
  Dim sPackage As String
  Dim sIcon As String
  Dim sDistribution As String
  Dim sVendor As String
  Dim sRelease As String
  Dim sPackageName As String

  Inc Application.Busy

  $sFormatSingle = ",&1"
  $sFormatMin = ",&1 >= &2"
  $sFormatMax = ",&1 < &3"
  $sFormatMinMax = $sFormatMin & $sFormatMax
  $sFormatEqual = $sFormatMin & Replace($sFormatMax, "<", "<=")
  
  sPackageName = FormatRpmPackageName($sUnsafePackageName)

  sRelease = CStr(Project.PackageVersion)
  
  Select Case sSys

    Case "mageia"
      sDistribution = "Mageia"
      sVendor = "Mageia"
      sRelease &= ".mga"

    Case "fedora"
      sDistribution = "Fedora"
      sVendor = "Fedora"
      sRelease &= ".fdr"

    Case "suse"
      sDistribution = "OpenSUSE"
      sVendor = "Novell"
      sRelease &= ".suse"

  End Select
  
    ' step 1, set up RPM dirs and macros if they're not there already

  AddLog(("Initializing ~/RPM directory."))

  For Each sCmd In ["RPM/", "RPM/BUILD/", "RPM/RPMS/", "RPM/SOURCES/", "RPM/SPECS/", "RPM/SRPMS/", "RPM/tmp/"]
    Try Mkdir $sHome &/ sCmd
  Next

  hFile = Open $sHome &/ ".rpmmacros" For Create
  Print #hFile, "%_topdir " & $sHome &/ "RPM"
  Print #hFile, "%_tmppath " & $sHome &/ "RPM/tmp"
    'PRINT #hFile, "%_signature gpg"
    'PRINT #hFile, "%_gpg_name Mandrivalinux"
    'PRINT #hFile, "%_gpg_path ~/.gnupg"
  Print #hFile, "%packager "; Project.Maintainer & "<" & SConv(Project.Address) & ">"
  Print #hFile, "%buildhost "; System.Host
  Print #hFile, "%distribution "; sDistribution
  Print #hFile, "%vendor "; If($sVendor, $sVendor, sVendor)
  Close #hFile

    ' step 2, write source tarball (with bzip) to SOURCES dir

  AddLog(("Creating source package."))

  sSrc = $sHome &/ "RPM/SOURCES"

  sDir = Project.Dir &/ ".icon"
  Shell "rm -rf " & Shell$(sDir) Wait
  Try Mkdir sDir

    ' IF Project.Icon THEN
    '   hIcon = Image.Load(Project.Dir &/ Project.Icon)
    ' ELSE
    '   hIcon = Image.Load("img/32/gambas.png")
    ' ENDIF

  Project.StretchIcon(Project.Dir &/ Project.Icon, 16).Save(sDir &/ "16.png")
  Project.StretchIcon(Project.Dir &/ Project.Icon, 32).Save(sDir &/ "32.png")
  Project.StretchIcon(Project.Dir &/ Project.Icon, 48).Save(sDir &/ "48.png")
  Project.StretchIcon(Project.Dir &/ Project.Icon, 64).Save(sDir &/ "64.png")

  sArch = sSrc &/ Subst("&1-&2.tar.bz2", sPackageName, $sVersion)
  Project.MakeSourcePackageTo(sArch)

  Shell "rm -rf " & Shell$(sDir) Wait

  AddLog(("Creating .spec file."))

  hFile = Open $sHome &/ "RPM/SPECS" &/ sPackageName & ".spec" For Create

  Print #hFile, "%define name " & sPackageName
  Print #hFile, "%define version " & $sVersion
  Print #hFile, "%define release "; sRelease

  Print #hFile
  Print #hFile, "Summary: " & IIf(Project.Title, Project.Title, Project.Name)
  Print #hFile, "Name: %{name}"
  Print #hFile, "Version: %{version}"
  Print #hFile, "Release: %{release}"
  Print #hFile, "Source0: %{name}-%{version}.tar.bz2"
  Print #hFile, "License: "; If(Project.License, Project.License, "GPL")
  Print #hFile, "Group: " & Project.Groups[sSys]
  Print #hFile, "BuildRoot: %{_tmppath}/%{name}-%{version}-${release}-buildroot"
  If sSys <> "mageia" Then
    Print #hFile, "Prefix: %{_prefix}"
  Endif
    'PRINT #hFile, "BuildRequires: gambas2-ide"
  Print #hFile, "BuildArch: noarch"

  Print #hFile, "Requires: gambas"; System.Version; "-runtime >= "; MIN_VERSION; ",gambas"; System.Version; "-runtime < "; MAX_VERSION;

  Print #hFile, GetDependencies();
  Print #hFile, GetExtraDependencies(sSys);

  Print #hFile

  Print #hFile, "AutoReqProv: no"

  Print #hFile, "\n%description\n" & $sDescription & "\nThis program is written in Gambas, so you will need Gambas to be installed."

    Print #hFile, "\n%prep"
  If sSys <> "mageia" Then
    Print #hFile, "rm -rf %{buildroot}"
  Endif

  Print #hFile, "%setup -q -n " & Shell$($sName)

  If Project.Type = Project.TYPE_COMPONENT Then

    ' Components must be installed in Gambas prefix, which is not known at package creation time.
    ' How can we do that? Hardcode installation path according to the distribution?

    Print #hFile, "\n%build"
    Print #hFile, Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False) & "\ngba"; System.Version
     
    Print #hFile, "\n%install"
    Print #hFile, GetExtraFilesDir(sSys, "install -d %{buildroot}/&1")
    Print #hFile, GetExtraFiles(sSys, "install -p .hidden/&1 %{buildroot}/&2/&1", "cp -R --preserve=timestamp .hidden/&1 %{buildroot}/&2/&1")
    Print #hFile, "install -d %{buildroot}/%{_libdir}/gambas"; System.Version
    Print #hFile, "install -p "; Shell$($sName); ".gambas %{buildroot}/%{_libdir}/gambas" & System.Version; "/"; Shell$($sComponentName); ".gambas"
    Print #hFile, "install -p .component %{buildroot}/%{_libdir}/gambas"; System.Version; "/"; Shell$($sComponentName); ".component"

    Print #hFile, "install -d %{buildroot}/%{_datadir}/gambas"; System.Version; "/info"
    Print #hFile, "install -p .info %{buildroot}/%{_datadir}/gambas"; System.Version; "/info/"; Shell$($sComponentName); ".info"
    Print #hFile, "install -p .list %{buildroot}/%{_datadir}/gambas"; System.Version; "/info/"; Shell$($sComponentName); ".list"

    If Exist(Project.Dir &/ ".hidden/control") Then 
      Print #hFile, "install -d %{buildroot}/%{_datadir}/gambas"; System.Version; "/control/"; Shell$($sComponentName)
      Print #hFile, "install -p "; ".hidden/control/*.png %{buildroot}/%{_datadir}/gambas" & System.Version &/ "control" &/ Shell$($sComponentName)
    Endif

  Else 
  
    Print #hFile, "\n%build"
    Print #hFile, Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False) & "\ngba"; System.Version
  
    Print #hFile, "\n%install"
  
    If Project.Type = Project.TYPE_NORMAL Then
      Print #hFile, "install -Dp "; Shell$($sName); ".gambas %{buildroot}/%{_bindir}" &/ Shell$($sName)
    Else If Project.Type = Project.TYPE_LIBRARY Then
      Print #hFile, "install -d %{buildroot}/%{_libdir}/gambas"; System.Version; "/"; LCase($sVendor)
      Print #hFile, "install -p "; Shell$($sName); ".gambas %{buildroot}/%{_libdir}/gambas"; System.Version; "/"; LCase($sVendor); "/"; Shell$($sLibraryName & ".gambas")
    Endif
    
    Print #hFile, GetExtraFilesDir(sSys, "install -d %{buildroot}/&1")
    Print #hFile, GetExtraFiles(sSys, "install -p .hidden/&1 %{buildroot}/&2/&1", "cp -R --preserve=timestamp .hidden/&1 %{buildroot}/&2/&1")
    
    If Project.Type = Project.TYPE_NORMAL
    
      sIcon = $sName & ".png"
    
      ' NOTE: There are explicit references to /usr/share.
      '       I must find a way to use the rpm macros instead.
    
      Select Case sSys
    
        Case "mageia"
    
          Print #hFile, "install -Dp .icon/16.png %{buildroot}/%{_datadir}/icons/hicolor/16x16/apps" &/ Shell$(sIcon)
          Print #hFile, "install -Dp .icon/32.png %{buildroot}/%{_datadir}/icons/hicolor/32x32/apps" &/ Shell$(sIcon)
          Print #hFile, "install -Dp .icon/48.png %{buildroot}/%{_datadir}/icons/hicolor/48x48/apps" &/ Shell$(sIcon)
          Print #hFile, "install -Dp .icon/64.png %{buildroot}/%{_datadir}/icons/hicolor/64x64/apps" &/ Shell$(sIcon)
          
          If $sName <> sPackageName Then Print #hFile, "ln -s " & Shell$($sName) & " \"%{buildroot}/%{_bindir}\"/" & Shell$(sPackageName)
  
          Print #hFile, "install -d %{buildroot}/%{_datadir}/applications"
          Print #hFile, "cat << EOF > %{buildroot}/%{_datadir}/applications/%{name}.desktop"
        
          PrintDesktopFile(hFile, $sName, $sName) ', "X-MandrivaLinux-" & Replace(Replace(Project.Menus[sSys], " ", ""), "/", "-") & ";")
        
          Print #hFile, "EOF"
    
        Case "fedora"
    
          Print #hFile, "install -d %{buildroot}/%{_datadir}/pixmaps"
          Print #hFile, "install -d %{buildroot}/%{_datadir}/applications"
          Print #hFile, "install -p .icon/48.png %{buildroot}/%{_datadir}/pixmaps" &/ Shell$(sIcon)
          
          If $sName <> sPackageName Then Print #hFile, "ln -s " & Shell$($sName) & " \"%{buildroot}/%{_bindir}\"/" & Shell$(sPackageName)
    
          Print #hFile, "cat << EOF > %{buildroot}%{_datadir}/applications/%{name}.desktop"
          
          PrintDesktopFile(hFile, $sName, sIcon, "X-RedHat-Extra;")
          
          Print #hFile, "EOF"
    
        Case "suse"
    
          Print #hFile, "install -d %{buildroot}/%{_datadir}/pixmaps"
          Print #hFile, "install -d %{buildroot}/%{_datadir}/applications"
          Print #hFile, "install -p .icon/48.png %{buildroot}/%{_datadir}/pixmaps" &/ Shell$(sIcon)
          
          If $sName <> sPackageName Then Print #hFile, "ln -s " & Shell$($sName) & " \"%{buildroot}/%{_bindir}\"/" & Shell$(sPackageName)
    
          Print #hFile, "cat << EOF > %{buildroot}%{_datadir}/applications/%{name}.desktop"
          
          PrintDesktopFile(hFile, $sName, sIcon)
          
          Print #hFile, "EOF"
          
      End Select
      
    Endif
    
  Endif

  If sSys <> "mageia" Then

    Print #hFile, "\n%clean"
    Print #hFile, "rm -rf %{buildroot}"
    
  Endif
  
    Print #hFile, "\n%files"
    
  If sSys <> "mageia" Then
    
    Print #hFile, "%defattr(-,root,root)"

  Endif
  
  If Project.Type = Project.TYPE_COMPONENT Then
  
    Print #hFile, FormatFileInList("%{_libdir}/gambas" & System.Version & "/" & $sComponentName & ".gambas")
    Print #hfile, FormatFileInList("%{_libdir}/gambas" & System.Version & "/" & $sComponentName & ".component")
    
    Print #hFile, FormatFileInList("%{_datadir}/gambas" & System.Version & "/info/" & $sComponentName & ".info")
    Print #hFile, FormatFileInList("%{_datadir}/gambas" & System.Version & "/info/" & $sComponentName & ".list")

    If Exist(Project.Dir &/ ".hidden/control") Then 
      Print #hFile, FormatFileInList("%{_datadir}/gambas" & System.Version & "/control/" & $sComponentName & "/*.png")
    Endif
    Print #hFile, GetExtraFilesList(sSys, "", "&2");

  Else
  
    If Project.Type = Project.TYPE_NORMAL Then
      Print #hFile, FormatFileInList("%{_bindir}" &/ $sName)
      Print #hFile, FormatFileInList("%{_bindir}" &/ sPackageName)
    Else If Project.Type = Project.TYPE_LIBRARY Then
      Print #hFile, FormatFileInList("%{_libdir}/gambas" & System.Version &/ LCase($sVendor) &/ $sLibraryName & ".gambas")
    Endif
    
    Print #hFile, GetExtraFilesList(sSys, "", "&2");
  
    If Project.Type = Project.TYPE_NORMAL Then
      
      Select Case sSys
    
        Case "mageia"
    
          Print #hFile, FormatFileInList("%{_datadir}/icons/hicolor/*/apps" &/ sIcon)
                'PRINT #hFile, "/%{_menudir}" &/ sName
          Print #hFile, "%{_datadir}/applications/%{name}.desktop"
        
        Case "fedora"
    
          Print #hFile, FormatFileInList("%{_datadir}/pixmaps" &/ sIcon)
          Print #hFile, "%{_datadir}/applications/%{name}.desktop"
    
        Case "suse"
    
          Print #hFile, FormatFileInList("%{_datadir}/pixmaps" &/ sIcon)
          Print #hFile, "%{_datadir}/applications/%{name}.desktop"
    
      End Select
      
    Endif
    
  Endif

  Print #hFile, "\n%changelog\n"
  Print #hFile, ChangeLog

  Close #hFile

    ' and step 4.... build that sucker.

  AddLog(("Creating package...") & "\n")

  sLang = System.Language
  System.Language = "C"

  sCmd = RPMBUILD_PROG & " -ba " & Shell$($sHome &/ "RPM/SPECS" &/ sPackageName & ".spec")
  AddLog(sCmd)

    'sTemp = Temp$
  $sOutput = ""
  $bEnd = False
  Shell sCmd & " 2>&1" Wait For Read As "Process" 'TO sOutput
    ' REPEAT
    '   SLEEP 0.01
    '   WAIT
    ' UNTIL $bEnd
  
  AddLog("")
  
  sOutput = Replace($sOutput, "\r", "")
  Try sOutput = DConv(sOutput)

  Try Kill $sHome &/ ".rpmmacros"

  System.Language = sLang

  Dec Application.Busy

  If Process.LastValue Then Error.Raise(("'rpmbuild' has returned the following error code:") & " " & Process.LastValue)

  For Each sCmd In Split(sOutput, "\n")
    If Left(sCmd, 6) = "Wrote:" And Right$(sCmd, 10) = "noarch.rpm" Then

      sPackage = Subst("&1-&2-&3.noarch.rpm", sPackageName, $sVersion, sRelease)
            ' IF sSys = "debian" THEN
            '   SHELL "cd " & Shell$(sHome &/ "RPM/RPMS/noarch") & "; fakeroot " & ALIEN_PROG & " --to-deb -k " & Shell$(sPackage) WAIT
            '   sPackage = sPrefix & Subst("&1_&2.&3-&4_all.deb", LCase(Project.Name), Project.MajorVersion, Project.MinorVersion, sRelease)
            '   IF NOT Exist(sHome &/ "RPM/RPMS/noarch" &/ sPackage) THEN Error.Raise("alien command has failed")
            ' ENDIF
      MoveFile($sHome &/ "RPM/RPMS/noarch" &/ sPackage, Path &/ sPackage)
      AddLog(sPackage)

      sPackage = Subst("&1-&2-&3.src.rpm", sPackageName, $sVersion, sRelease)
            ' IF sSys = "debian" THEN
            '   SHELL "cd " & Shell$(sHome &/ "RPM/SRPMS") & "; fakeroot alien --to-deb -k " & Shell$(sPackage) WAIT
            '   sPackage = sPrefix & Subst("&1_&2.&3-&4_all.deb", LCase(Project.Name), Project.MajorVersion, Project.MinorVersion, sRelease)
            '   IF NOT Exist(sHome &/ "RPM/SRPMS" &/ sPackage) THEN Error.Raise("alien command has failed")
            '   MoveFile(sHome &/ "RPM/SRPMS" &/ sPackage, Path &/ File.BaseName(sPackage) & ".src.deb")
            ' ELSE
      MoveFile($sHome &/ "RPM/SRPMS" &/ sPackage, Path &/ sPackage)
      AddLog(sPackage)
            ' ENDIF

    End If
  Next

End

Private Function MakeRpmSupportPackage(sSys As String, sSupport As String, aComp As String[])

  Dim sCmd As String
  Dim hFile As File
  Dim sOutput As String
  Dim sLang As String
  Dim sPackage As String
  Dim sDistribution As String
  Dim sVendor As String
  Dim sRelease As String
  Dim sPackageName As String
  Dim sDependency As String

  Inc Application.Busy

  $sFormatSingle = ",&1"
  $sFormatMin = ",&1 >= &2"
  $sFormatMax = ",&1 < &3"
  $sFormatMinMax = $sFormatMin & $sFormatMax
  $sFormatEqual = $sFormatMinMax
  
  sPackageName = FormatRpmPackageName($sUnsafePackageName) & "-" & sSupport
  
  sRelease = CStr(Project.PackageVersion)

  Select Case sSys

    Case "mageia"
      sDistribution = "Mageia"
      sVendor = "Mageia"
      sRelease &= ".mga"

    Case "fedora"
      sDistribution = "Fedora"
      sVendor = "Fedora"
      sRelease &= ".fdr"

    Case "suse"
      sDistribution = "OpenSUSE"
      sVendor = "Novell"
      sRelease &= ".suse"

  End Select
  
    ' step 1, set up RPM dirs and macros if they're not there already

  AddLog(("Initializing ~/RPM directory."))

  For Each sCmd In ["RPM/", "RPM/BUILD/", "RPM/RPMS/", "RPM/SOURCES/", "RPM/SPECS/", "RPM/SRPMS/", "RPM/tmp/"]
    Try Mkdir $sHome &/ sCmd
  Next

  hFile = Open $sHome &/ ".rpmmacros" For Create
  Print #hFile, "%_topdir " & $sHome &/ "RPM"
  Print #hFile, "%_tmppath " & $sHome &/ "RPM/tmp"
    'PRINT #hFile, "%_signature gpg"
    'PRINT #hFile, "%_gpg_name Mandrivalinux"
    'PRINT #hFile, "%_gpg_path ~/.gnupg"
  Print #hFile, "%packager "; Project.Maintainer & "<" & SConv(Project.Address) & ">"
  Print #hFile, "%buildhost "; System.Host
  Print #hFile, "%distribution "; sDistribution
  Print #hFile, "%vendor "; If($sVendor, $sVendor, sVendor)
  Close #hFile

    ' step 2, write source tarball (with bzip) to SOURCES dir

  AddLog(("Creating .spec file."))

  hFile = Open $sHome &/ "RPM/SPECS" &/ sPackageName & ".spec" For Create

  Print #hFile, "%define name " & sPackageName
  Print #hFile, "%define version " & $sVersion
  Print #hFile, "%define release "; sRelease
  
  Print #hFile
  Print #hFile, "Summary: "; sSupport; " support for "; IIf(Project.Title, Project.Title, Project.Name)
  Print #hFile, "Name: %{name}"
  Print #hFile, "Version: %{version}"
  Print #hFile, "Release: %{release}"
  'Print #hFile, "Source0: " '"%{name}-%{version}.tar.bz2"
  Print #hFile, "License: "; If(Project.License, Project.License, "GPL")
  Print #hFile, "Group: " & Project.Groups[sSys]
  'Print #hFile, "BuildRoot: %{_tmppath}/%{name}-%{version}-${release}-buildroot"
  If sSys <> "mageia" Then
    Print #hFile, "Prefix: %{_prefix}"
  Endif
    'PRINT #hFile, "BuildRequires: gambas2-ide"
  Print #hFile, "BuildArch: noarch"

  sDependency = $sVersion & "-" & sRelease

  Print #hFile, "Requires: "; FormatRpmPackageName($sUnsafePackageName);; ">=";; sDependency;; ","; FormatRpmPackageName($sUnsafePackageName);; "<=";; sDependency; GetDependencies(aComp)

  Print #hFile

  Print #hFile, "AutoReqProv: no"

  Print #hFile, "\n%description\n"; "This is the "; sSupport; " support for "; Project.Name

  Print #hFile, "\n%files"
  If sSys <> "mageia" Then
    Print #hFile, "%defattr(-,root,root)"
  Endif

  Print #hFile, "\n%changelog\n"
  Print #hFile, ChangeLog

  Close #hFile

    ' and step 4.... build that sucker.

  AddLog(("Creating package...") & "\n")

  sLang = System.Language
  System.Language = "C"

  sCmd = RPMBUILD_PROG & " -ba " & Shell$($sHome &/ "RPM/SPECS" &/ sPackageName & ".spec")
  AddLog(sCmd)

    'sTemp = Temp$
  $sOutput = ""
  $bEnd = False
  Shell sCmd & " 2>&1" Wait For Read As "Process" 'TO sOutput
    ' REPEAT
    '   SLEEP 0.01
    '   WAIT
    ' UNTIL $bEnd
  
  AddLog("")
  
  sOutput = Replace($sOutput, "\r", "")
  Try sOutput = DConv(sOutput)

  Try Kill $sHome &/ ".rpmmacros"

  System.Language = sLang

  Dec Application.Busy

  If Process.LastValue Then Error.Raise(("'rpmbuild' has returned the following error code:") & " " & Process.LastValue)

  For Each sCmd In Split(sOutput, "\n")
    If Left(sCmd, 6) = "Wrote:" And Right$(sCmd, 10) = "noarch.rpm" Then

      sPackage = Subst("&1-&2-&3.noarch.rpm", sPackageName, $sVersion, sRelease)
            ' IF sSys = "debian" THEN
            '   SHELL "cd " & Shell$(sHome &/ "RPM/RPMS/noarch") & "; fakeroot " & ALIEN_PROG & " --to-deb -k " & Shell$(sPackage) WAIT
            '   sPackage = sPrefix & Subst("&1_&2.&3-&4_all.deb", LCase(Project.Name), Project.MajorVersion, Project.MinorVersion, sRelease)
            '   IF NOT Exist(sHome &/ "RPM/RPMS/noarch" &/ sPackage) THEN Error.Raise("alien command has failed")
            ' ENDIF
      MoveFile($sHome &/ "RPM/RPMS/noarch" &/ sPackage, Path &/ sPackage)
      AddLog(sPackage)

      sPackage = Subst("&1-&2-&3.src.rpm", sPackageName, $sVersion, sRelease)
            ' IF sSys = "debian" THEN
            '   SHELL "cd " & Shell$(sHome &/ "RPM/SRPMS") & "; fakeroot alien --to-deb -k " & Shell$(sPackage) WAIT
            '   sPackage = sPrefix & Subst("&1_&2.&3-&4_all.deb", LCase(Project.Name), Project.MajorVersion, Project.MinorVersion, sRelease)
            '   IF NOT Exist(sHome &/ "RPM/SRPMS" &/ sPackage) THEN Error.Raise("alien command has failed")
            '   MoveFile(sHome &/ "RPM/SRPMS" &/ sPackage, Path &/ File.BaseName(sPackage) & ".src.deb")
            ' ELSE
      MoveFile($sHome &/ "RPM/SRPMS" &/ sPackage, Path &/ sPackage)
      AddLog(sPackage)
            ' ENDIF

    End If
  Next

End

Private Sub PrintSlackDescRuler(hFile As File, sPackageName As String)
  
  Dim sLine As String
  
  sLine = Space(Len(sPackageName)) & "|-----handy-ruler" & String$(76, "-")
  sLine = Left(sLine, 75) & "|"
  Print #hFile, sLine
  
End

Private Sub PrintSlackDesc(hFile As File, sLine As String, sPackageName As String)
  
  sLine = sPackageName & ": " & sLine
  sLine = Left(sLine, 76)
  Print #hFile, sLine
  
End


Private Sub PrintSlackDescContents(hFile As File, sData As String, nLine As Integer, sPackageName As String)

  Dim sLine As String
  Dim iPos As Integer
    
  While nLine
    If sData Then
      If Len(sData) > (76 - Len(sPackageName) - 2) Then
        sLine = Left(sData, 76 - Len(sPackageName) - 2)
        iPos = RInStr(sLine, " ")
        If iPos Then sLine = RTrim(Left(sLine, iPos - 1))
        sData = LTrim(Mid$(sData, Len(sLine) + 1))
      Else
        sLine = sData
        sData = ""
      Endif
    Else
      sLine = ""
    Endif
    PrintSlackDesc(hFile, sLine, sPackageName)
    Dec nLine
  Wend
  
End

Private Sub FormatTgzPackageName(sName As String) As String

 ' Use the same format as RPM at the moment
  Return FormatRpmPackageName(sName)

End

Private Sub MakeTgzPackage(sSys As String)

  Dim sDistribution As String
  Dim sVendor As String
  Dim sBuildDir As String
  Dim hFile As File
  'Dim sPrefix As String
  Dim sCmd As String
  Dim sIcon As String
  Dim sData As String
  Dim sTempPackage As String
  Dim sPackage As String
  Dim sFile As String 
  Dim sPackageName As String
      
  Inc Application.Busy

  $sFormatSingle = "&1\n"
  $sFormatMin = "&1 >= &2\n"
  $sFormatMax = "&1 < &3\n"
  $sFormatMinMax = $sFormatMin & $sFormatMax
  $sFormatEqual = $sFormatMinMax

  sPackageName = FormatTgzPackageName($sUnsafePackageName)
  sDistribution = "Slackware"
  sVendor = "Slackware"
  
  ' 1. Create the temporary build directory

  AddLog(("Making build directory."))

  sBuildDir = File.SetExt(Temp$("slackware"), "")
  Shell "rm -rf " & Shell(sBuildDir) Wait
  Mkdir sBuildDir

  Select Case Project.Type

    Case Project.TYPE_COMPONENT 
  
      For Each sCmd In ["/usr", "/usr/lib", "/usr/lib/gambas" & System.Version, "/usr/share", "/usr/share/gambas" & System.Version, "/usr/share/gambas" & System.Version & "/info", "/install"]
        Try Mkdir sBuildDir &/ sCmd
      Next
      
    Case Project.TYPE_LIBRARY
      
      For Each sCmd In ["/usr", "/usr/lib", "/usr/lib/gambas" & System.Version, "/usr/lib/gambas" & System.Version &/ LCase($sVendor), "/install"]
        Try Mkdir sBuildDir &/ sCmd
      Next
      
    Case Project.TYPE_NORMAL

      For Each sCmd In ["/usr", "/usr/bin", "/usr/share", "/usr/share/applications", "/usr/share/pixmaps", "/install"]
        Try Mkdir sBuildDir &/ sCmd
      Next
  
  End Select
  
  CopyExtraFiles(sSys, sBuildDir)
  
  If Project.Type = Project.TYPE_COMPONENT Then 
  
    ' 2. Copy the library, the information files and the controls
  
    Copy Project.GetExecPath() To sBuildDir &/ "/usr/lib/gambas" & System.Version &/ $sComponentName & ".gambas"
    Copy Project.Dir &/ ".component" To sBuildDir &/ "/usr/lib/gambas" & System.Version &/ $sComponentName & ".component"
    Copy Project.Dir &/ ".info" To sBuildDir &/ "/usr/share/gambas" & System.Version &/ "info/" & $sComponentName & ".info"
    Copy Project.Dir &/ ".list" To sBuildDir &/ "/usr/share/gambas" & System.Version &/ "info/" & $sComponentName & ".list"
  
    If Exist(Project.Dir &/ ".hidden/control") Then 
      Try Mkdir sBuildDir &/ "/usr/share/gambas" & System.Version & "/control"
      Try Mkdir sBuildDir &/ "/usr/share/gambas" & System.Version & "/control/" & $sComponentName
      For Each sFile In Dir(Project.Dir &/ ".hidden/control", "*.png") 
        Copy Project.Dir &/ ".hidden/control" &/ sFile To sBuildDir &/ "/usr/share/gambas" & System.Version & "/control/" & $sComponentName &/ sFile
      Next
    Endif
  
  Else

    ' 2. Copy the executable
  
    'COPY Project.Dir &/ Project.Name & ".gambas" TO sBuildDir &/ "/usr/bin" &/ Project.Name
    
    If Project.Type = Project.TYPE_NORMAL Then
      Copy Project.GetExecPath() To sBuildDir &/ "/usr/bin" &/ $sName
      Chmod sBuildDir &/ "/usr/bin" &/ $sName To "..x..x..x"
      If $sName <> sPackageName Then Link $sName To sBuildDir &/ "/usr/bin" &/ sPackageName
    Else If Project.Type = Project.TYPE_LIBRARY Then
      Copy Project.GetExecPath() To sBuildDir &/ "/usr/lib/gambas" & System.Version &/ LCase($sVendor) &/ $sLibraryName & ".gambas"
      Chmod sBuildDir &/ "/usr/lib/gambas" & System.Version &/ LCase($sVendor) &/ $sLibraryName & ".gambas" To "..x..x..x"
    Endif
      
    If Project.Type = Project.TYPE_NORMAL Then
      
      ' 3. Copy the icon
    
      ' IF Project.Icon THEN
      '   hIcon = Image.Load(Project.Dir &/ Project.Icon)
      ' ELSE
      '   hIcon = Image.Load("img/gambas.png")
      ' ENDIF
    
      Project.StretchIcon(Project.Dir &/ Project.Icon, 48).Save(sBuildDir &/ "/usr/share/pixmaps" &/ Project.Name & ".png")
    
      ' 4. Make the desktop file
      
      sIcon = Shell$($sName) & ".png"
    
      hFile = Open sBuildDir &/ "/usr/share/applications/" & sPackageName & ".desktop" For Create
    
      PrintDesktopFile(hFile, $sName, "/usr/share/pixmaps/" & $sName & ".png")
    
      Close #hFile
      ' if we are creating a project with desktop icon/menu we need to run a doinst.sh in the package
      ' to make sure the menu item appears without needing a restart of xwindows.
      hFile = Open sBuildDir &/ "install" &/ "doinst.sh" For Create
      Print #hFile, "if [ -x /usr/bin/update-desktop-database ]; then"
      Print #hfile, "  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1"
      Print #hFile, "fi"
      Close #hFile
       
            
    Endif
  
  Endif
  
  ' 5. Description file

  hFile = Open sBuildDir &/ "/install/slack-desc" For Create
  
  Print #hFile, File.Load("install/slack-desc-header");    
  PrintSlackDescRuler(hFile, sPackageName)
  PrintSlackDesc(hFile, Subst("&1 &2: &3", sPackageName, $sVersion, If(Project.Title, Project.Title, Project.Name)), sPackageName)
  PrintSlackDesc(hFile, "", sPackageName)
  sData = Replace($sDescription, "\n", " ")
  PrintSlackDescContents(hFile, sData, 7, sPackageName)
  PrintSlackDesc(hFile, "Package created by: " & Project.Maintainer, sPackageName)
  PrintSlackDesc(hFile, "EMail address: " & Project.Address, sPackageName)

  Close #hFile
  
  ' 6. Dependencies. only used by slapt-get. other tools, like installpkg don't use it.
  ' gambas is packaged a a single package - gambas3 including everything.
  
  hFile = Open sBuildDir &/ "/install/slack-required" For Create
  
  Print #hFile, "gambas"; System.Version; " >= "; MIN_VERSION
  Print #hFile, "gambas"; System.Version; " < "; MAX_VERSION

  Print #hFile, GetDependencies();
  Print #hFile, GetExtraDependencies(sSys);
  
  Close #hFile
  
  ' 7. Create package
  
  AddLog(("Creating package...") & "\n")

  sTempPackage = File.SetExt(Temp$("slackware"), "tgz")
  sCmd = TAR_1_13_PROG & " --exclude=slackware.tgz --owner=root --group=root -c -z -f " & Shell$(sTempPackage) & " ."
  RunCommand(sCmd, sBuildDir)
  ' AddLog(sCmd)
  ' $sOutput = ""
  ' $bEnd = FALSE
  ' SHELL "(cd " & SConv(Shell$(sBuildDir)) & ";" & sCmd & ") 2>&1" FOR READ 'TO sOutput
  ' REPEAT
  '   SLEEP 0.01 
  '   WAIT
  ' UNTIL $bEnd
  
  Dec Application.Busy

  If Process.LastValue Then Error.Raise(("'tar' has returned the following error code:") & " " & Process.LastValue)

  AddLog("")
  sPackage = Subst("&1-&2-noarch-&3.tgz", sPackageName, $sVersion, CStr(Project.PackageVersion))
  MoveFile(sTempPackage, Path &/ sPackage)
  AddLog(sPackage)
  
End

' Private Sub MakeTgzSupportPackage((sSys) As String, sSupport As String, aComp As String[])
' 
'   Dim sDistribution As String
'   Dim sVendor As String
'   Dim sBuildDir As String
'   Dim hFile As File
'   'Dim sPrefix As String
'   Dim sCmd As String
'   Dim sData As String
'   Dim sTempPackage As String
'   Dim sPackage As String
'   Dim sPackageName As String
'   
'   Inc Application.Busy
' 
'   $sFormatSingle = "&1\n"
'   $sFormatMin = "&1 >= &2\n"
'   $sFormatMax = "&1 < &3\n"
'   $sFormatMinMax = $sFormatMin & $sFormatMax
'   $sFormatEqual = $sFormatMinMax
' 
'   sPackageName = FormatTgzPackageName($sUnsafePackageName) & "-" & sSupport
'   sDistribution = "Slackware"
'   sVendor = "Slackware"
'   
'   ' 1. Create the temporary build directory
' 
'   AddLog(("Making build directory."))
' 
'   sBuildDir = File.SetExt(Temp$("slackware"), "")
'   Shell "rm -rf " & Shell(sBuildDir) Wait
'   Mkdir sBuildDir
'   Try Mkdir sBuildDir &/ "install"
'   
'   ' 5. Description file
' 
'   hFile = Open sBuildDir &/ "/install/slack-desc" For Create
'   
'   Print #hFile, File.Load("install/slack-desc-header");    
'   PrintSlackDescRuler(hFile, sPackageName)
'   PrintSlackDesc(hFile, Subst("&1 &2: &3", sPackageName, $sVersion, sSupport & " support for " & IIf(Project.Title, Project.Title, Project.Name)), sPackageName)
'   PrintSlackDesc(hFile, "", sPackageName)
'   sData = sSupport & " support for " & IIf(Project.Title, Project.Title, Project.Name)
'   PrintSlackDescContents(hFile, sData, 7, sPackageName)
'   PrintSlackDesc(hFile, "Package created by: " & Project.Maintainer, sPackageName)
'   PrintSlackDesc(hFile, "EMail address: " & Project.Address, sPackageName)
' 
'   Close #hFile
'   
'   ' 6. Dependencies
'   
'   hFile = Open sBuildDir &/ "/install/slack-required" For Create
'   
'   Print #hFile, FormatTgzPackageName($sUnsafePackageName); " >= "; $sVersion
'   Print #hFile, FormatTgzPackageName($sUnsafePackageName); " <= "; $sVersion
'   Print #hFile, GetDependencies(aComp);
'   
'   Close #hFile
'   
'   ' 7. Create package
'   
'   AddLog(("Creating package...") & "\n")
' 
'   sTempPackage = File.SetExt(Temp$("slackware"), "tgz")
'   sCmd = TAR_1_13_PROG & " --exclude=slackware.tgz --owner=root --group=root -c -z -f " & Shell$(sTempPackage) & " ."
'   RunCommand(sCmd, sBuildDir)
'   ' AddLog(sCmd)
'   ' $sOutput = ""
'   ' $bEnd = FALSE
'   ' SHELL "(cd " & SConv(Shell$(sBuildDir)) & ";" & sCmd & ") 2>&1" FOR READ 'TO sOutput
'   ' REPEAT
'   '   SLEEP 0.01 
'   '   WAIT
'   ' UNTIL $bEnd
'   
'   Dec Application.Busy
' 
'   If Process.LastValue Then Error.Raise(("'tar' has returned the following error code:") & " " & Process.LastValue)
' 
'   AddLog("")
'   sPackage = Subst("&1-&2-noarch-&3.tgz", sPackageName, $sVersion, CStr(Project.PackageVersion))
'   MoveFile(sTempPackage, Path &/ sPackage)
'   AddLog(sPackage)
'   
' End
' 
Private Sub MakeAutotoolsPackage((sSys) As String)

  Dim sBuildDir As String
  Dim sFile As String
  Dim hFile As File
  Dim sCmd As String
  Dim sPackage As String
  
  sPackage = GetPackageName($sName)
  
  ' 1. Create the temporary build directory

  AddLog(("Making build directory."))
  Inc Application.Busy

  sBuildDir = File.SetExt(Temp$("autotools"), "")
  Shell "rm -rf " & Shell(sBuildDir) Wait
  Mkdir sBuildDir
  
  ' 2. Copy needed files 

  Try sCmd = Scan(Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False), "*/bin/gbc" & System.Version & " *")[1]

  'Mkdir sBuildDir &/ Project.Name
  Shell "cp -rL " & Shell$(Project.Dir) & " " & Shell$(sBuildDir &/ $sName) Wait 
  sFile = Replace(File.Load("install/acinclude.m4"), "$(VERSION)", CStr(System.Version))
  sFile = Replace(sFile, "$(PACKAGE_VERSION)", $sVersion)
  sFile = Replace(sFile, "$(EXTRA_TEST)", Project.ExtraAutoconfTest)
  File.Save(sBuildDir &/ "acinclude.m4", sFile)
  sFile = Replace(File.Load("install/Makefile.am"), "$(VERSION)", CStr(System.Version))  
  sFile = Replace(sFile, "$(COMPILE_FLAGS)", sCmd)
  File.Save(sBuildDir &/ "Makefile.am", sFile)
  
  ' 3. Create needed files (AUTHORS, README, COPYING, NEWS, ChangeLog, configure.ac)
  
  If Exist(sBuildDir &/ $sName &/ "CHANGELOG") Then 
    Move sBuildDir &/ $sName &/ "CHANGELOG" To sBuildDir &/ "ChangeLog"
  Endif  

  File.Save(sBuildDir &/ "ChangeLog", ChangeLog)
  File.Save(sBuildDir &/ "AUTHORS", Project.Maintainer & " <" & Project.Address & ">")
  File.Save(sBuildDir &/ "README", Project.Description)
  File.Save(sBuilddir &/ "COPYING", "This program is under : " & Project.License)
  File.Save(sBuilddir &/ "NEWS", "")
  
  hFile = Open sBuildDir &/ "configure.ac" For Create
    Print #hFile, "dnl -- package generated with gambas3 ide --"
    Print #hFile, ""
    Print #hFile, "AC_INIT([" & $sName & "],[" & $sVersion & "],[],[" & $sName & "])"
    Print #hFile, "AC_CONFIG_SRCDIR([configure.ac])"
    If Project.Type = Project.TYPE_COMPONENT Then
      Print #hFile, "GB_INIT_PROJECT([" & $sName & "],[" & sPackage & "],[" & $sComponentName & "])"
    Else
      Print #hFile, "GB_INIT_PROJECT([" & $sName & "],[" & sPackage & "])"
    Endif
    Print #hFile, ""
    Print #hFile, "dnl -- create makefile --"
    Print #hFile, ""
    Print #hFile, "AC_OUTPUT(Makefile)"
  Close #hFile

  ' 4. create the package
  
  AddLog(("Creating package...") & "\n")

  For Each sCmd In ["aclocal", AUTOCONF_PROG, AUTOMAKE_PROG & " -a", "./configure", "make dist"]
    RunCommand(sCmd, sBuildDir)
  Next 
  
  Dec Application.Busy
  AddLog("")

  sPackage = Dir(sBuildDir, $sName & "*" & $sVersion & "*")[0]
  MoveFile(sBuildDir &/ sPackage, Path &/ GetPackagePrefix(Project.Type, $sVendor, $sVendorPrefix, Project.Prefix) & sPackage)
  AddLog(sPackage)
  
  ' Remove build dir
  Shell "rm -rf " & Shell(sBuildDir) Wait
  
End

Private Sub FormatArchPackageName(sName As String) As String
  
  Dim I As Integer
  Dim sCar As String
  Dim sRes As String

  For I = 1 To Len(sName)
    sCar = Mid$(sName, I, 1)
    If Not IsLetter(sCar) And If Not IsDigit(sCar) Then sCar = "-"
    sRes &= sCar
  Next
  
  Return sRes
  
End


Private Function MakeArchPackage(sSys As String)

  Dim sBuildDir As String
  Dim sPkgTgz As String
  Dim hFile As File
  Dim sCmd As String
  Dim sDependencies As String
  Dim sPkgDesc As String
  Dim sPackageName As String
  
  Inc Application.Busy

  sPackageName = FormatArchPackageName($sUnsafePackageName)

  sPkgDesc = Replace($sDescription, "\n", " ")
  
  If sPkgDesc = "" Then sPkgDesc = "a software done with Gambas3"
  
  $sFormatMinMax = "'&1>=&2' '&1<=&3' "
  $sFormatMin = "'&1>=&2' "
  $sFormatMax = "'&1<=&3' "
  $sFormatSingle = "'&1' "
  $sFormatEqual = "'&1=&2' "
  sDependencies = GetDependencies() & GetExtraDependencies(sSys)
  
  ' 1. Create the temporary build directory

  AddLog(("Making build directory."))

  sBuildDir = File.SetExt(Temp$("archlinux"), "")
  Shell "rm -rf " & Shell$(sBuildDir) Wait
  Mkdir sBuildDir

  ' 2. Copy needed files 
  
  sPkgTgz = $sName & "-" & $sVersion & ".tar.bz2"
  Project.MakeSourcePackageTo(sBuildDir &/ sPkgTgz)

  File.Save(sBuilddir &/ "license.txt", "This program is under : " & Project.License)
  
  If Project.Type = Project.TYPE_NORMAL Then

    Project.StretchIcon(Project.Dir &/ Project.Icon, 48).Save(sBuildDir &/ Project.Name & ".png")

    hFile = Open sBuildDir &/ sPackageName & ".desktop" For Create
    PrintDesktopFile(hFile, $sName, "/usr/share/pixmaps/" & $sName & ".png")
    Close #hFile
    
  Endif 
   
  hFile = Open sBuildDir &/ "PKGBUILD" For Create
    Print #hFile, "# generated by the gambas3 ide"
    Print #hFile, "# Maintainer : " & Project.Maintainer & " <" & Project.Address & ">"
    Print #hFile, ""
    Print #hFile, "pkgname="; sPackageName
    Print #hFile, "_realname="; $sName
    Print #hFile, "pkgdesc=\"" & sPkgDesc & "\""
    Print #hFile, "pkgver=" & $sVersion
    Print #hFile, "pkgrel="; Project.PackageVersion
    Print #hFile, "arch=('any')"
    Print #hFile, "url=\"" & Project.Url & "\""
    If Me.ChangeLog Then 
      File.Save(sBuildDir &/ "ChangeLog", Me.ChangeLog)
      Print #hFile, "changelog=ChangeLog"
    Endif  
    Print #hFile, "license=('custom')"
    If Len(sDependencies) > 0 Then
      Print #hFile, "depends=(" & Left$(sDependencies, -1) & ")"
    Endif
    Print #hFile, "makedepend=('gambas3-devel')"
    Print #hFile, "source=(${_realname}-$pkgver.tar.bz2 license.txt";
    If Project.Type = Project.TYPE_NORMAL Then
      Print #hFile, "\\\n        '" & sPackageName & ".desktop'";
      Print #hFile, " '" & Project.Name & ".png'";
    Endif 
    Print #hFile, ")"
  Close #hFile

  RunCommand("makepkg -m -g >> PKGBUILD", sBuildDir, True)
  If Process.LastValue > 0 Then Error.Raise(("'makepkg' has failed."))

  hFile = Open sBuildDir &/ "PKGBUILD" For Append
  
    Print #hFile, ""
    Print #hFile, "build() {"
    Print #hFile, "  cd ${srcdir}/${_realname}"
    Print #hFile, ""
    Print #hFile, "  "; Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False); " && gba"; System.Version
    Print #hFile, "}"
    Print #hFile, ""
    Print #hFile, "package() {"
    Print #hFile, "  cd ${srcdir}/${_realname}"
    Print #hFile, ""
    
    Select Case Project.Type
    
      Case Project.TYPE_COMPONENT
        
        Print #hFile, "  install -d ${pkgdir}/usr/lib/gambas" & System.Version
        Print #hFile, "  install -d ${pkgdir}/usr/share/gambas" & System.Version &/ "info"
        Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/lib/gambas" & System.Version &/ $sComponentName & ".gambas"
        Print #hFile, "  install .component ${pkgdir}/usr/lib/gambas" & System.Version &/ $sComponentName & ".component"
        Print #hFile, "  chmod a-x ${pkgdir}/usr/lib/gambas" & System.Version &/ $sComponentName & ".component" 
        Print #hFile, "  install .info ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sComponentName & ".info"
        Print #hFile, "  chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sComponentName & ".info" 
        Print #hFile, "  install .list ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sComponentName & ".list"
        Print #hFile, "  chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sComponentName & ".list" 
        Print #hFile, "  if test -d control; then"
        Print #hFile, "    install -d ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sComponentName
        Print #hFile, "    install .hidden/control/*.png ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sComponentName
        Print #hFile, "    chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sComponentName &/ "*.png"
        Print #hFile, "  fi"
        
      Case Project.TYPE_LIBRARY
        
        Print #hFile, "  install -d ${pkgdir}/usr/lib/gambas" & System.Version &/ LCase($sVendor)
        Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/lib/gambas" & System.Version &/ LCase($sVendor) &/ $sLibraryName & ".gambas"

      Case Project.TYPE_NORMAL
  
        Print #hFile, "  install -d ${pkgdir}/usr/bin"
        Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/bin" &/ $sName
        If $sName <> sPackageName Then Print #hFile, "  ln -s " & Shell$($sName) & " \"${pkgdir}\"/usr/bin" &/ Shell$(sPackageName)
        Print #hFile, "  install -D .." &/ Project.Name & ".png \\"
        Print #hFile, "    ${pkgdir}/usr/share/pixmaps" &/ Project.Name & ".png"
        Print #hFile, "  install -D .." &/ sPackageName & ".desktop \\"
        Print #hFile, "    ${pkgdir}/usr/share/applications" &/ sPackageName & ".desktop"

    End Select

    Print #hFile, GetExtraFilesDir(sSys, "  install -d ${pkgdir}&1");
    Print #hFile, GetExtraFiles(sSys, "  install -p ${srcdir}/${_realname}/.hidden/&1 ${pkgdir}&2/&1", "  cp -R --preserve=timestamp ${srcdir}/${_realname}/.hidden/&1 ${pkgdir}&2/&1");
    Print #hFile, "  install -Dm644 ${srcdir}/license.txt ${pkgdir}/usr/share/licenses" &/ sPackageName &/ "license.txt"
    Print #hFile, "}"
    
  Close #hFile
  
  ' 4. create the packages
  
  AddLog(("Creating package...") & "\n")
  
  For Each sCmd In ["makepkg -m --allsource", "makepkg -m -d"]
    RunCommand(sCmd, sBuildDir)
    If Process.LastValue > 0
      Dec Application.Busy
      Error.Raise(("'makepkg' has failed."))
    Endif
  Next 

  RunCommand("cp " & sPackageName & "-" & $sVersion & "-" & CStr(Project.PackageVersion) & "*.tar.* " & Shell$(Path) &/ "", sBuildDir)

  ' Remove build dir
  Shell "rm -rf " & Shell(sBuildDir) Wait

  Dec Application.Busy

End 

Private Function MakeArchSupportPackage((sSys) As String, sSupport As String, aComp As String[])

  Dim sBuildDir As String
  Dim hFile As File
  Dim sCmd As String
  Dim sDependencies As String
  Dim sPkgDesc As String
  Dim sPackageName As String
  
  Inc Application.Busy

  sPackageName = FormatArchPackageName($sUnsafePackageName & "-" & sSupport)

  sPkgDesc = "This is the " & sSupport & " support for " & If(Project.Title, Project.Title, Project.Name)
  
  $sFormatMinMax = "'&1>=&2' '&1<=&3' "
  $sFormatMin = "'&1>=&2' "
  $sFormatMax = "'&1<=&3' "
  $sFormatSingle = "'&1' "
  $sFormatEqual = "'&1=&2' "
  
  sDependencies = GetDependencies(aComp)
  
  ' 1. Create the temporary build directory

  AddLog(("Making build directory."))

  sBuildDir = File.SetExt(Temp$("archlinux"), "")
  Shell "rm -rf " & Shell$(sBuildDir) Wait
  Mkdir sBuildDir

  ' 2. Copy needed files 
  
  File.Save(sBuilddir &/ "license.txt", "This program is under : " & Project.License)
  
  hFile = Open sBuildDir &/ "PKGBUILD" For Create
    Print #hFile, "# generated by the gambas3 ide"
    Print #hFile, "# Maintainer : " & Project.Maintainer & " <" & Project.Address & ">"
    Print #hFile, ""
    Print #hFile, "pkgname=" & sPackageName
    Print #hFile, "_realname="; sPackageName
    Print #hFile, "pkgdesc=\"" & sPkgDesc & "\""
    Print #hFile, "pkgver=" & $sVersion
    Print #hFile, "pkgrel="; Project.PackageVersion
    Print #hFile, "arch=('any')"
    Print #hFile, "url=\"" & Project.Url & "\""
    If Me.ChangeLog Then 
      File.Save(sBuildDir &/ "ChangeLog", Me.ChangeLog)
      Print #hFile, "changelog=ChangeLog"
    Endif  
    Print #hFile, "license=('custom')"
    If Len(sDependencies) > 0 Then
      Print #hFile, "depends=(" & Subst($sFormatEqual, FormatArchPackageName($sUnsafePackageName), $sVersion) & Left$(sDependencies, -1) & ")"
    Endif
    'Print #hFile, "makedepend=('gambas3-devel')"
    'Print #hFile, "source=(${_realname}-$pkgver.tar.bz2 license.txt";
    'If Project.Type = Project.TYPE_NORMAL Then
    '  Print #hFile, "\\\n        '" & $sPackageName & ".desktop'";
    '  Print #hFile, " '" & Project.Name & ".png'";
    'Endif 
    'Print #hFile, ")"
  Close #hFile
  
  RunCommand("makepkg -m -g >> PKGBUILD", sBuildDir, True)
  If Process.LastValue > 0 Then Error.Raise(("'makepkg' has failed."))
  
  ' hFile = Open sBuildDir &/ "PKGBUILD" For Append
  ' 
  '   Print #hFile, ""
  '   Print #hFile, "build() {"
  '   Print #hFile, "  cd ${srcdir}/${_realname}"
  '   Print #hFile, ""
  '   Print #hFile, "  "; Project.GetCompileCommand(True, Not Project.KeepDebugInfo, True, False); " && gba"; System.Version
  '   Print #hFile, "}"
  '   Print #hFile, ""
  '   Print #hFile, "package() {"
  '   Print #hFile, "  cd ${srcdir}/${_realname}"
  '   Print #hFile, ""
  '   
  '   Select Case Project.Type
  '   
  '     Case Project.TYPE_COMPONENT
  '       
  '       Print #hFile, "  install -d ${pkgdir}/usr/lib/gambas" & System.Version
  '       Print #hFile, "  install -d ${pkgdir}/usr/share/gambas" & System.Version &/ "info"
  '       Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/lib/gambas" & System.Version
  '       Print #hFile, "  install .component ${pkgdir}/usr/lib/gambas" & System.Version &/ $sName & ".component"
  '       Print #hFile, "  chmod a-x ${pkgdir}/usr/lib/gambas" & System.Version &/ $sName & ".component" 
  '       Print #hFile, "  install .info ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sName & ".info"
  '       Print #hFile, "  chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sName & ".info" 
  '       Print #hFile, "  install .list ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sName & ".list"
  '       Print #hFile, "  chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "info" &/ $sName & ".list" 
  '       Print #hFile, "  if test -d control; then"
  '       Print #hFile, "    install -d ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sName
  '       Print #hFile, "    install .hidden/control/*.png ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sName
  '       Print #hFile, "    chmod a-x ${pkgdir}/usr/share/gambas" & System.Version &/ "control" &/ $sName &/ "*.png"
  '       Print #hFile, "  fi"
  '       
  '     Case Project.TYPE_LIBRARY
  '       
  '       Print #hFile, "  install -d ${pkgdir}/usr/bin"
  '       Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/bin" &/ $sName & ".gambas"
  ' 
  '     Case Project.TYPE_NORMAL
  ' 
  '       Print #hFile, "  install -d ${pkgdir}/usr/bin"
  '       Print #hFile, "  install -m755 " & $sName & ".gambas ${pkgdir}/usr/bin" &/ $sName
  '       Print #hFile, "  install -D .." &/ Project.Name & ".png \\"
  '       Print #hFile, "    ${pkgdir}/usr/share/pixmaps" &/ Project.Name & ".png"
  '       Print #hFile, "  install -D .." &/ $sPackageName & ".desktop \\"
  '       Print #hFile, "    ${pkgdir}/usr/share/applications" &/ $sPackageName & ".desktop"
  ' 
  '   End Select
  ' 
  '   Print #hFile, GetExtraFilesDir(sSys, "  install -d ${pkgdir}&1\n");
  '   Print #hFile, GetExtraFiles(sSys, "  install -p ${srcdir}/${_realname}/.hidden/&1 ${pkgdir}&2/&1", "  cp -R --preserve=timestamp ${srcdir}/${_realname}/.hidden/&1 ${pkgdir}&2/&1");
  '   Print #hFile, "  install -Dm644 ${srcdir}/license.txt ${pkgdir}/usr/share/licenses" &/ $sPackageName &/ "license.txt"
  '   Print #hFile, "}"
  '   
  ' Close #hFile
  
  ' 4. create the packages
  
  AddLog(("Creating package...") & "\n")
  
  For Each sCmd In ["makepkg -m --allsource", "makepkg -m -d"]
    RunCommand(sCmd, sBuildDir)
    If Process.LastValue > 0
      Dec Application.Busy
      Error.Raise(("'makepkg' has failed."))
    Endif
  Next 

  RunCommand("cp " & sPackageName & "-" & $sVersion & "-" & CStr(Project.PackageVersion) & "*.tar.* " & Shell$(Path) &/ "", sBuildDir)

  ' Remove build dir
  Shell "rm -rf " & Shell(sBuildDir) Wait

  Dec Application.Busy

End 

Private Sub MoveFile(sSrc As String, sDst As String)

  Try Kill sDst
  Try Move sSrc To sDst
  If Error Then
    Copy sSrc To sDst
    Try Kill sSrc
  Endif
  
End

Public Sub Process_Kill()
  
  $bEnd = True
  
End

Public Sub Process_Read()
  
  Dim sData As String
  
  sData = Read #Last, -256
  $sOutput &= sData
  FMakeInstall.AddLog(sData, True)
  
End

Public Sub Process_Error(sData As String)

  FMakeInstall.AddLog(sData, True)

End

Public Sub GetPackageName(sName As String) As String

  sName = LCase(String.RemoveDiacritics(sName))
  sName = Replace(sName, ".", "-")
  Return sName
  
End

Public Sub GetPackagePrefix(iProjectType As Integer, sVendor As String, sVendorPrefix As String, bPrefix As Boolean) As String

  Dim sText As String
  Dim sPackage As String
  Dim sPrefix As String
  
  If iProjectType = Project.TYPE_COMPONENT Then

    sPrefix = String.LCase(Trim(sVendorPrefix))
    If Not sPrefix Then sPrefix = "gb"
    
    sPackage = GetPackageName(Project.Name)
    If sPackage Begins sPrefix & "-" Or If sPackage = sPrefix Then 
      sPrefix = ""
    Else 
      sPrefix &= "-"
    Endif
    sText = "gambas" & System.Version & "-" & sPrefix
  
  Else

    If bPrefix Then
      
      sPrefix = sVendorPrefix
      If Not sPrefix Then sPrefix = sVendor
      sPrefix = String.LCase(Trim(sPrefix))
      sPrefix = Replace(sPrefix, "_", "-")
  
      If Not sPrefix Then sPrefix = "gambas"
      
      sText = sPrefix & "-"
      
    Endif
    
  Endif
  
  Return sText
  
End

Public Sub GetPackageFullName(sVendor As String, sVendorPrefix As String, bPrefix As Boolean, bWithMajor As Boolean, bWithVersion As Boolean) As String
  
  Dim sText As String
  
  sText = GetPackagePrefix(Project.Type, sVendor, sVendorPrefix, bPrefix) & GetPackageName(Project.Name)
  If bWithMajor Then sText &= CStr(Project.MajorVersion)
  If bWithVersion Then sText &= "-" & Project.FormatVersion()
  Return sText
  
End
