#!/usr/local/bin/python

from os import listdir, system, path
from subprocess import Popen, PIPE, STDOUT, call

fbsduf = '/var/db/freebsd-update-check/'
fbtag = '%stag' % fbsduf
fblist = '%stag' % fbsduf
fbvcmd = "freebsd-version"
installfbupdate = "freebsd-update fetch install"
fblist = '%stag' % fbsduf
fbsduf = '/var/db/freebsd-update-check/'
pulcmd = 'pkg upgrade -n' 

def listOfInstal():
    ls = listdir(fbsduf)
    for line in ls:
        if 'install.' in line:
            uptag = open(fbsduf + line + '/INDEX-NEW', 'r')
            info = uptag.readlines()
            return info


def checkFbsdUpdate():
    if path.exists(fbtag):
        uptag = open(fbtag, 'r')
        tag = uptag.readlines()[0].rstrip().split('|')
        upversion = tag[2] + "-p" + tag[3]
        fbv = Popen(fbvcmd, shell=True, stdin=PIPE, stdout=PIPE,
        stderr=STDOUT, close_fds=True)
        fbsdversion = fbv.stdout.readlines()[0].rstrip()
        if fbsdversion == upversion:
            return False
        else:
            return True


def lookFbsdUpdate():
    if path.exists(fbtag):
        uptag = open(fbtag, 'r')
        tag = uptag.readlines()[0].rstrip().split('|')
        return "FreeBSD " + tag[2] + "-p" + tag[3] + " base system updates"
    else:
        return None


def updateText():
    udatetitle = lookFbsdUpdate()
    text = "Update Details:\n"
    text += "%s\n\n" % udatetitle
    text += "The following files will be update:\n"
    for line in listOfInstal():
        txtlist = line.split('|')
        text += "%s" % txtlist[0] + "\n"
    return text

def pkg_update():
    fbv = Popen('%s | grep -v upgrade | grep -v package' % pulcmd, shell=True, stdin=PIPE, stdout=PIPE,
    stderr=STDOUT, close_fds=True)
    print fbv.stdout.read()

pkg_update() 
    

@@ -1192,16 +1192,23 @@
 FreeBSD 
+Update:
 %22 + tag%5B
@@ -1229,33 +1229,8 @@
 g%5B3%5D
- + %22 base system updates%22
 %0A   

Change sentence for FreeBSD update