# -*- coding: utf-8 -*-
import os
import re
import commands

def create_pkglist(repodir):
    group_list={}
    os.chdir(str(repodir))
    dir_list = os.listdir(".")
    for dir in dir_list:
        if os.path.isdir(dir):
            #print "found a dir:%s" %dir

            dir_walk = dir
            for root, dirs, files in os.walk(dir_walk):
                #print "walking %s,%s" %(root,dirs)
                for file in [f for f in files]:
                    if file == "PKGBUILD" :
                        topdir="%s/%s" %(repodir, root)
                        if "linhes_pkgbuild" in topdir:
                            if  ("core" in topdir) or ("extra" in topdir):
                                pass
                            else:
                                print "%s not LINHES" %topdir
                                print "skipping"
                                continue

                        currentfile = topdir + "/PKGBUILD"
                        if os.path.isfile(currentfile):
                            pkgname="unknown"
                            pkgversion="unknown"
                            pkgdescription="unknown"
                            pkggroup=[]
                            try:
                                f= open(currentfile,"r")
                                file_contents= f.readlines()
                                f.close()
                                for line in file_contents:
                                    if line.strip().startswith("pkgname"):
                                        pkgname=line.partition("=")[2].strip()

                                    elif line.strip().startswith("pkgver"):
                                        pkgversion=line.partition("=")[2]

                                    elif line.strip().startswith("pkgdesc"):
                                        pkgdescription=line.partition("=")[2]

                                    elif line.strip().startswith("groups"):
                                        l=line.partition("=")[2].strip()
                                        for c in ['(',')',"'"]:
                                            l = l.replace( c, '' )
                                        pkggroup=l.split()
                            except:
                                pass

                            #temptuple=(pkgname,topdir)
                            #group_list.append(temptuple)
                            group_list[pkgname] = (topdir,pkgversion)
                        #print repodir
                        #print root
                        #print dirs
                        #print file
    os.chdir("..")
    return group_list

def updatepkg(abslocation,linheslocation):
    print "\n\n"
    print "%s  - > %s "  %(abslocation, linheslocation)
    print "---------------------------------------------------------------"
    cmd = '''rsync -avchp -delete-after  --stats --exclude="src" %s/  %s'''   %(abslocation, linheslocation)
    totalfiles=0
    global    updated
    global failed
    global no_update
    rc,output = commands.getstatusoutput(cmd)
   # print output
    for line in output.split("\n"):
    #print line
        sline=line.strip()
        if line.startswith("Number of files transferred"):
            totalfiles=line.split(":")[1].strip()
            if rc == 0:
                #print totalfiles
                totalfiles = int(totalfiles.strip())
                if totalfiles >= 1 :
                   # print "total files transferred is more then 0"
                    cmd = "touch %s/.updated" %linheslocation
                    os.system(cmd)
                    updated = updated + 1
                else:
                    cmd = "touch %s/.no_update" %linheslocation
                    os.system(cmd)
                    no_update = no_update +1
            else:
                cmd = "touch %s/.updated_failed" %linheslocation
                os.system(cmd)
                failed = failed + 1

    print "  updated: %s " %updated
    print "no_update: %s " %no_update
    print "   failed: %s " %failed




skip_changelist="/tmp/update_list/changelist"
updatedlist="/tmp/update_list/updatedlist"
not_in_abs="/tmp/update_list/not_in_abs"
matchlist="/tmp/update_list/matched"

skipfile = open(skip_changelist,"w")
updatefile = open(updatedlist,"w")
matchfile = open(matchlist,"w")
not_absfile = open(not_in_abs,"w")


abslist = create_pkglist("/var/abs")
linheslist = create_pkglist("/data/linhes_pkgbuild/abs")
global updated
global failed
global no_update
updated=0
failed=0
no_update=0

for pkgname, pkglocation_t in linheslist.iteritems():

    pkglocation=pkglocation_t[0]
    pkgversion=pkglocation_t[1].strip().rstrip('\n')
    #print pkgname, pkglocation
    if "mv-core" in pkglocation :
        continue
    if "mythtv" in pkglocation :
        continue
    if ("core" in pkglocation) or ("extra" in pkglocation):
        pass
    else:
        continue


    abslocation = ''
    try:
      abslocation = abslist[pkgname][0]
      absversion = abslist[pkgname][1].strip().rstrip('\n')
    except:
      #  print "pkg is not found in abs %s" %pkgname
        outline = "%s :  %s  \n" %(pkgname, pkglocation)
        not_absfile.write(outline)
        continue


    #print "ABSLOCATION  %s" %abslocation
    #print "LINHES LOCATION %s" %pkglocation
    changelog = pkglocation + "/__changelog"
    if os.path.isfile(changelog):
       # print "%s: pkg has linhes changelog, skipping" %pkgname
        outline = "%s :  %s \n" %(pkgname, pkglocation)
        skipfile.write(outline)
    else:
	if absversion == pkgversion:
		outline = "%s: matched %s \n" %(pkgname,pkgversion)
		matchfile.write(outline)
	else:
            #print "%s: updating from ABS" %pkgname
            outline = '%s:\n %s,%s | %s,%s \n' %(pkgname, pkglocation, pkgversion,  abslocation, absversion)
            print outline
            updatefile.write(outline)
#            updatepkg(abslocation,pkglocation)



skipfile.close()
updatefile.close()
not_absfile.close()