############################################################################
#
#	File:     newicon.icn
#
#	Subject:  Program to produce new Icon program file
#
#	Author:   Ralph E. Griswold
#
#	Date:     March 26, 2002
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This program creates a new file with a standard Icon program
#  header and a skeleton mail procedure.
#
#  The first command-line argument is taken as the base
#  name of the file; default "foo".  The second command-line argument is
#  taken as the author; the default is "Ralph E. Griswold" -- with minor
#  apologies, I use this program a lot; personalize it for your own
#  use.  The same comment applies to the skeleton file mentioned below.
#
#  The new file is brought up in the vi editor.
#
#  The supported options are:
#
#	-f	overwrite and existing file
#	-p	produce a procedure file instead of a program
#	-o	provide program skeleton with options()
#
#  The files skeleton.icn, skelproc.icn, and skelopt.icn must be accessible
#  via dopen().
#
############################################################################
#
#  Requires:  system(), vi(1)
#
############################################################################
#
#  Links:  basename, datetime, io, options
#
############################################################################

link basename
link datetime
link io
link options

procedure main(args)
   local opts, overwrite, name, author, input, output, file

   opts := options(args, "fpo")
   if \opts["f"] then overwrite := 1

   name := (args[1] | "foo")
   if (*name < 4) | (name[-4:0] ~== ".icn") then name ||:= ".icn"

   author := args[2] | "Ralph E. Griswold"

   if /overwrite then {				# check to see if file exists
       if input := open(name) then {
          close(input)
          system("vi " || name)
          exit()
          }
      }

   output := open(name, "w") |
      stop("*** cannot open ", name, " for writing")

   input := dopen(
      if \opts["o"] then file := "skelopt.icn"
      else if \opts["p"] then "skelproc.icn"
      else "skeleton.icn"
      ) | stop("*** cannot open skeleton file")

   every 1 to 2 do write(output, read(input)) |
      stop("*** short skeleton file")
   write(output, read(input), name) |
      stop("*** short skeleton file")
   every 1 to 3 do write(output, read(input)) |
      stop("*** short skeleton file")
   write(output, read(input), author) |
      stop("*** short skeleton file")
   write(output, read(input)) |
      stop("*** short skeleton file")
   write(output, read(input), date()) |
      stop("*** short skeleton file")
   write(output, read(input)) |
      stop("*** short skeleton file")
   while write(output, read(input))

   if \opts["p"] then {
      write(output, "procedure ", basename(name, ".icn"), "()")
      write(output)
      write(output, "end")
      }

   close(output)

   system("vi " || name)

end
