#!/usr/bin/python
#
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import errno, os, sys, string

copyright = '''\
# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

try:
    fp = open(sys.argv[1])
except IOError, e:
    if e.errno != errno.ENOENT:
        raise
    lines = []
else:
    lines = fp.readlines()
    fp.close()

preamble = None
if lines and lines[0][:2] == '#!':
    preamble = lines.pop(0)

# try to guess whether the file already has a copyright notice:
criteria_lines = [
'# Twisted, the Framework of Your Internet',
'# Copyright (C) 2001 Matthew W. Lefkowitz',
'# Lesser General Public License for more details.']

for line in criteria_lines:
    if not line in map(string.strip, lines):
        break
else:
    print "file seems to have a copyright notice, not adding!"
    sys.exit(1)
fp = open(sys.argv[1]+'.tmp', 'w')
if preamble is not None:
    fp.write(preamble)
fp.write(copyright)
fp.writelines(lines)
fp.close()
os.rename(sys.argv[1]+'.tmp', sys.argv[1])
