#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2013-2017 Rudá Moura (Rudix)
# Author: Rudá Moura <ruda.moura@gmail.com>

"""Create a new port for Rudix."""

Makefile = """include ../../Library/{formula}.mk

Title=		{title}
Name=		{name}
Version=	{version}
Site=		{site}
Source=		{source}
License=        {license}
"""

def create_makefile(params, path):
    try:
        with open(path, 'w') as stream:
            output = Makefile.format(**params)
            stream.write(output)
    except:
        return 1
    else:
        return 0

def process(args):
    if args.title is None:
        title = args.name.title()
    else:
        title = args.title
    params = {'formula': args.formula,
              'name': args.name,
              'title': title,
              'version': args.version,
              'site': args.site,
              'source': args.source,
              'license': args.license,
    }
    if args.create:
        import os
        os.mkdir(args.name)
        path = os.path.join(args.name, 'Makefile')
    else:
        path = '/dev/stdout'
    return create_makefile(params, path)


if __name__ == '__main__':
    import sys
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('name',
                        help='the name of the port. Use all lowercase.')
    parser.add_argument('--version',
                        default='1.2.3',
                        help='set version.')
    parser.add_argument('--title',
                        default='My Package',
                        help='set title. Default: equals to name.')
    parser.add_argument('--site',
                        default='http://example.org/',
                        help='set home page.')
    parser.add_argument('--source',
                        default='http://example.org/$(Name)-$(Version).tar.gz',
                        help='set source name and version format.')
    parser.add_argument('--license',
                        default='GPL',
                        help='set license. Default: GPL.')
    parser.add_argument('--formula',
                        default='GNU',
                        help='set build formula to use. Default: GNU.')
    parser.add_argument('--create',
                        action='store_true',
                        help='create directory and Makefile.')
    args = parser.parse_args()
    sys.exit(process(args))

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

@@ -67,10 +67,17 @@
 -201
-7 
+9 Rudix (
 Rud%C3%A1
@@ -86,15 +86,8 @@
 oura
- (Rudix
 )%0A# 
@@ -127,16 +127,18 @@
 ail.com%3E
+%0A#
 %0A%0A%22%22%22Cre
@@ -141,18 +141,16 @@
 %22Create 
-a 
 new port
@@ -154,17 +154,17 @@
 ort 
-for Rudix
+prototype
 .%22%22%22
@@ -321,16 +321,85 @@
 license%7D
+%0A%0Adefine test_hook%0A$(BinDir)/%7Bname%7D --version %7C grep $(Version)%0Aendef
 %0A%22%22%22%0A%0Ade
@@ -636,16 +636,50 @@
 (args):%0A
+    args.name = args.name.lower()%0A
     if a
@@ -826,23 +826,22 @@
    '
-name': args.nam
+title':   titl
 e,%0A 
@@ -858,20 +858,26 @@
    '
-title': titl
+name':    args.nam
 e,%0A 
@@ -935,16 +935,19 @@
  'site':
+   
  args.si
@@ -973,16 +973,17 @@
 source':
+ 
  args.so
@@ -1025,22 +1025,16 @@
 .license
-,%0A    
 %7D%0A    if
@@ -1229,50 +1229,30 @@
 h)%0A%0A
-%0Aif __name__ == '__main__':%0A    import sys
+def parse_arguments():
 %0A   
@@ -1332,16 +1332,60 @@
 gument('
+--name',%0A                        default='no
 name',%0A 
@@ -1532,11 +1532,9 @@
 ='1.
-2.3
+0
 ',%0A 
@@ -1574,16 +1574,29 @@
 version.
+ Default: 1.0
 ')%0A    p
@@ -1629,54 +1629,8 @@
 e',%0A
-                        default='My Package',%0A
     
@@ -1679,22 +1679,24 @@
 lt: 
-equals to name
+name capitalized
 .')%0A
@@ -1762,34 +1762,33 @@
 efault='http
+s
 ://
-example
+rudix
 .org/',%0A    
@@ -1907,22 +1907,30 @@
 http
+s
 ://
-example.org
+rudix.org/download
 /$(N
@@ -1994,16 +1994,21 @@
 t source
+ URL,
  name an
@@ -2421,17 +2421,17 @@
   help='
-c
+C
 reate di
@@ -2438,19 +2438,44 @@
 rectory 
-and
+(with port name) and put the
  Makefil
@@ -2475,16 +2475,22 @@
 Makefile
+ there
 .')%0A    
@@ -2516,16 +2516,76 @@
 _args()%0A
+    return args%0A%0A%0Aif __name__ == '__main__':%0A    import sys%0A
     sys.
@@ -2601,66 +2601,24 @@
 ess(
+p
 ar
-gs))%0A%0A# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
+se_arguments()))
 %0A

update script