#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys, time
print "!!! You must run as root"
print "Are you sure? (If you sure, press enter. If not, press ctrl + C)"
raw_input()
print "Caution : If you've downloaded already 'karnado-0.2_r1.tar.bz2',"
print "          Please move it to path where 'setup.py' exists."
print "Press <Enter>"
raw_input()

try:
	a = sys.argv[1]
except:
	print "Usage: ./setup.py (device node)"
	print "Ex) ./setup.py /dev/sda"
	sys.exit()

f = open('url.txt').read().replace('\n', '')
if 'karnado-0.2_r1.tar.bz2' in os.listdir('.'):
	print 'Already downloaded. pass.'
else:
	os.system('wget %s' %f)
	if 'karnado-0.2_r1.tar.bz2' not in os.listdir('.'):
		print 'Download Failed. try again.'
		sys.exit()

print os.popen('md5sum karnado-0.2_r1.tar.bz2').read()
os.system('tar -xvjf karnado-0.2_r1.tar.bz2')
os.system('mkdir tmp; tar -xvf syslinux-conf.tar -C tmp/')
os.system('mkdir dv')
os.system('mount %s ./dv' %a)
os.system('cp tmp/* dv/')
print "Transferring now. Please wait!"
print "%s is mounted. DO NOT REMOVE!" %a
os.system('cp karnado.squashfs dv/')
print "Wait 2 minutes to finish the transferring."
time.sleep(120)
print "Syslinux updating"
os.system('syslinux %s' %a)
print "Copying kernel image"
os.system('tar -xvf kernel-pack.tar -C tmp/')
os.system('cp tmp/* dv/')
print "Wait 10 seconds..."
time.sleep(10)
print 'Unmounting...'
os.system('umount dv')
print 'Cleaning!'
os.system('rm -rf tmp')
print 'Everything is done!'
