#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""verify quota
original code: eole-fichier/common/pretemplate/00-fix-home-fstab
"""

import os
import sys
from pyeole.process import system_out


def enable_quota():
    """
    Enable quota on call
    """
    os.system('quotacheck -aug')
    #le service ne doit pas être en erreur
    os.system('systemctl reset-failed quotaon.service 2>/dev/null')
    os.system('quotaon -aug')


def check_quota():
    """
    Check quota if they are disabled during "instance" time
    """
    cmd = ['quotaon', '-paug']
    eni = {"LC_ALL": "C", "LANG": 'C', "PATH": '/sbin'}
    _, out, _ = system_out(cmd, env=eni)
    status = out.strip().split('\n')
    for elm in status:
        if  not elm.endswith('on'):
            return False
    return True


def main():
    """
    Main program, call quota enable.
    """
    #seulement à l'instance
    if sys.argv[-1] != 'instance':
        sys.exit(0)

    if not check_quota():
        enable_quota()


if __name__ == "__main__":
    main()

