#!/usr/bin/tclsh
#
# Generate a basic template Portfile given a few bits of information
#
# Todo:
# Add remaining portgroup bits
# Add more fields with comments perhaps, so the template is more fully
#    ready for various uses
#

set MY_VERSION 0.2

set supportedGroups [list perl5 php python ruby github]

proc printUsage {{portgroup ""} {channel stderr}} {
   global supportedGroups
   puts $channel "Usage: $::argv0 \[-h\] \[-g portgroup\] <portname> <portversion>"
   puts $channel "   -g   Specify a port group to use"
   puts $channel "   -h   This help"
   puts $channel ""
   puts $channel "portname      name to use for the port; see also port groups usage"
   puts $channel "portversion   version to use for the port"
   puts $channel ""
   if {$portgroup eq ""} {
      puts $channel "To see usage for a specific port group: $::argv0 -h -g portgroup"
      puts $channel "Supported port groups: ${supportedGroups}"
   } else {
      puts $channel "Usage for port group $portgroup:"
      if {$portgroup eq "perl5"} {
         puts $channel "Do not use the prefix 'p5-' in the portname as it will be added when needed."
      } elseif {$portgroup eq "php"} {
         puts $channel "Do not use the prefix 'php-' in the portname as it will be added when needed."
      } elseif {$portgroup eq "python"} {
         puts $channel "Do not use the prefix 'py-' in the portname as it will be added when needed."
      } elseif {$portgroup eq "ruby"} {
         puts $channel "Do not use the prefix 'rb-' in the portname as it will be added when needed."
      } elseif {$portgroup eq "github"} {
         puts $channel "Usage: $::argv0 -g github username/repository tag"
         puts $channel ""
         puts $channel "GitHub username and repository have to be specifed as one argument,"
         puts $channel "but will be split automatically. The tag is the version number,"
         puts $channel "often in the format of 'vX.Y.Z'."
      } else {
         puts $channel "No special parameters for '$portgroup'."
      }
   }
}


set groupCode ""
set usage false
while {[string index [lindex $::argv 0] 0] eq "-"} {
   switch [string range [lindex $::argv 0] 1 end] {
      g {
         if {[llength $::argv] < 2} {
            puts stderr "-g needs a port group"
            printUsage
            exit 1
         }
         set groupCode [lindex $::argv 1]
         if {[lsearch ${supportedGroups} ${groupCode}] == -1} {
            puts "Sorry, port group ${groupCode} is currently not supported"
            puts "Supported: [join ${supportedGroups}]"
            exit 1
         }
         set ::argv [lrange $::argv 1 end]
      }
      h {
        set usage true
      }
      default {
         puts stderr "Unknown option [lindex $::argv 0]"
         printUsage
         exit 1
      }
   }
   set ::argv [lrange $::argv 1 end]
}

if {$usage} {
    printUsage $groupCode stdout
    exit 0
}

if {[llength $::argv] != 2} {
   puts stderr "Error: missing portname or portversion"
   printUsage
   exit 1
}

set portname [lindex $::argv 0]
set portversion [lindex $::argv 1]

if {$groupCode eq "github"} {
    set verlist [split $portname /]
    set github.user [lindex ${verlist} 0]
    set portname    [lindex ${verlist} 1]
    if {[regexp {^v([0-9]+.[0-9.-]+)} $portversion -> version]} {
        set portversion $version
        set github.tag_prefix "v"
    }
}

puts "# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4"
puts ""
puts "PortSystem          1.0"
switch ${groupCode} {
   perl5 {
      puts "PortGroup           perl5 1.0"
      puts ""
      puts "perl5.branches      5.8 5.10 5.12 5.14 5.16"
      puts "perl5.setup         ${portname} ${portversion}"
      puts "categories-append   replaceme"
   }
   php {
      puts "PortGroup           php 1.1"
      puts ""
      puts "name                php-${portname}"
      puts "version             ${portversion}"
      puts "categories-append   replaceme"
   }
   python {
      puts "PortGroup           python 1.0"
      puts ""
      puts "name                py-${portname}"
      puts "version             ${portversion}"
      puts "categories-append   replaceme"
   }
   ruby {
      puts "PortGroup           ruby 1.0"
      puts ""
      puts "ruby.setup          ${portname} ${portversion}"
      puts "categories-append   replaceme"
   }
   github {
      puts "PortGroup           github 1.0"
      puts ""
      if {[info exists github.tag_prefix]} {
          set tag_prefix " ${github.tag_prefix}"
      } else {
          set tag_prefix ""
      }
      puts "github.setup        ${github.user} ${portname} ${portversion}${tag_prefix}"
      puts "categories          replaceme"
   }
   default {
      puts ""
      puts "name                ${portname}"
      puts "version             ${portversion}"
      puts "categories          replaceme"
   }
}
puts "platforms           darwin"
puts "maintainers         replaceme"
puts "license             replaceme"
switch ${groupCode} {
    php {
        puts ""
        puts {php.branches        5.3 5.4 5.5}
        puts {php.pecl            yes}
    }
}
puts ""
puts "description         replaceme"
puts ""
puts "long_description    replaceme"
switch ${groupCode} {
   perl5 {
   }
   php {
   }
   python {
      puts ""
      puts "homepage            https://pypi.python.org/pypi/${portname}/"
      puts "master_sites        pypi:[string index ${portname} 0]/${portname}"
      puts "distname            \${python.rootname}-\${version}"
   }
   ruby {
   }
   github {
   }
   default {
      puts ""
      puts "homepage            replaceme"
      puts "master_sites        replaceme"
   }
}
puts ""
puts "checksums           rmd160 12345 \\"
puts "                    sha256 6789a"
switch ${groupCode} {
   php {
      puts ""
      puts "if {\$subport ne \$name} {"
      puts "    depends_lib-append      replaceme"
      puts ""
      puts "    configure.args-append   replaceme"
      puts "}"
   }
   python {
      puts ""
      puts "python.versions     27 34 35"
      puts ""
      puts "if {\$subport ne \$name} {"
      puts "    post-destroot {"
      puts {        set docdir ${prefix}/share/doc/${subport}}
      puts {        xinstall -m 755 -d ${destroot}${docdir}}
      puts "        xinstall -m 644 -W $\{worksrcpath\} replaceme \\"
      puts {           ${destroot}${docdir}}
      puts "    }"
      puts "}"
   }
}
