#!/usr/bin/env python
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 by philemon <mmgen-py@yandex.com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
mmgen-addrimport: Import addresses generated by mmgen-addrgen into an
                  online bitcoind watching wallet.
"""

import sys
from mmgen.Opts   import *
from mmgen.config import *
from mmgen.license import *
from mmgen.utils import check_infile,parse_addrs_file,confirm_or_exit

help_data = {
	'prog_name': sys.argv[0].split("/")[-1],
	'desc': """Import addresses generated by mmgen-addrgen into an
                     online bitcoind watching wallet""",
	'usage':"[opts] [infile]",
	'options': """
-h, --help               Print this help message
-q, --quiet              Suppress warnings
-v, --verbose            Produce more verbose output
"""
}

short_opts = "hqv"
long_opts  = "help", "quiet", "verbose"

opts,cmd_args = process_opts(sys.argv,help_data,"".join(short_opts),long_opts)

if len(cmd_args) != 1: usage(help_data)

check_infile(cmd_args[0])

seed_id,addr_data = parse_addrs_file(cmd_args[0])

from mmgen.tx import connect_to_bitcoind
c = connect_to_bitcoind(mmgen=True)

message = """
Importing addresses can take a long time, up to 30 min. per address on a
low-powered computer such as a netbook.
"""
confirm_or_exit(message, "continue", expect="YES")

for n,i in enumerate(addr_data):
	comment = " " + " ".join(i[2:]) if len(i) > 2 else ""
	label = "%s:%s%s" % (seed_id,str(i[0]),comment)
	msg("Importing %-6s %-34s (%s)" % (
				("%s/%s:" % (n+1,len(addr_data))),
				i[1], label)
			)
 	c.importaddress(i[1],label)