#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import subprocess
import sys
import logging
logger = logging.getLogger(__name__)

# Directory to execute
dir_exec = os.path.join(
    os.path.dirname(os.path.realpath(__file__)),
    "%s.d" % os.path.basename(__file__),
)

if not os.path.isdir(dir_exec):
    logger.error('%s does not exist' % dir_exec)
    exit()

# Find scripts
files = [(d, dir_exec) for d in os.listdir(dir_exec)]

# Run scripts
for executable, folder in sorted(files):
    command = os.path.join(folder, executable)
    if not os.path.getsize(command):
        continue
    if os.access(command, os.X_OK):
        logger.info("Executing %s", command)
        subprocess.check_call(command)
