#!/usr/bin/env python
#
# gen-inetloc/gen-inetloc ---
#

# put them in ~/Library/Scripts 

import argparse
import math
import os
import os.path
import pdb
import pprint
import re
import subprocess
import sys

#####

DEBUG=None
VERBOSE=None
VERSION='$Revision: 1.14 $'

#####

def write_inetloc(path,url):
  print "### write_inetloc: '%s' -> '%s'"%(url,path)
  data="""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>URL</key>
    <string>""" + url + """</string>
</dict>
</plist>
"""
  path_tmp=path+".tmp"
  fh=open(path_tmp,"w")
  fh.write(data)
  fh.close()
  os.rename(path_tmp,path)

#####

def main(raw_args):
  global DEBUG,VERBOSE,VERSION
  #
  parser=argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
  parser.description="""
DESCRIPTION:

"""
  parser.epilog="""
EXAMPLES:

"""
  #
  g = parser.add_argument_group('GENERAL')
  g.add_argument("--debug","-d",
                 action="store_true",
                 help="Turn on the debugging flag.")
  g.add_argument("--pdb",
                 action="store_true",
                 help="Enter the debugger after reading args.")
  g.add_argument("--verbose","-v",
                 action="store_true",
                 help="Be more verbose.")
  #
  g.add_argument("--user","-u",
                 help="Username to use for ssh.")
  #
  g.add_argument("--short-hostname","-s",
                 action="store_true",
                 help="Use the short hostname for the output filename.")
  #
  g.add_argument("--out-dir","-o",
                 help="Output directory for files.")
  #
  g.add_argument("args",nargs="*",
                 help="The remaining args.")
  #
  args=parser.parse_args(raw_args)
  #
  if args.pdb:
    pdb.set_trace()
  if args.debug:
    DEBUG=args.debug
  if args.verbose:
    VERBOSE=args.verbose

  ### put code here

  for arg in args.args:
    path=arg
    if args.short_hostname:
      path=path.split(".")[0]
    path=path+".inetloc"
    if args.out_dir:
      path=os.path.join(args.out_dir,path)
    #
    url=arg
    if url.startswith("http:"):
      pass
    else: # default to ssh
      if args.user and url.find("@")==-1:
        url=args.user+"@"+arg
      url="ssh://"+url
    #
    write_inetloc(path,url)

#
if __name__ == "__main__":
  sys.exit(main(sys.argv[1:]))

# Local Variables:
# mode: python
# End:
