#!/usr/clearos/sandbox/usr/bin/php
<?php

/**
 * Accounts home directory creation.
 *
 * Most services on ClearOS use PAM to auto-create home directories on 
 * demand.  However, there are some exceptions to this rule.  This 
 * script is run (via clearsync) when a user is added to the system.
 *
 * @category   apps
 * @package    accounts
 * @subpackage scripts
 * @author     ClearFoundation <developer@clearfoundation.com>
 * @copyright  2011 ClearFoundation
 * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License version 3 or later
 * @link       http://www.clearfoundation.com/docs/developer/apps/accounts/
 */

///////////////////////////////////////////////////////////////////////////////
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// B O O T S T R A P
///////////////////////////////////////////////////////////////////////////////

$bootstrap = getenv('CLEAROS_BOOTSTRAP') ? getenv('CLEAROS_BOOTSTRAP') : '/usr/clearos/framework/shared';
require_once $bootstrap . '/bootstrap.php';

///////////////////////////////////////////////////////////////////////////////
// D E P E N D E N C I E S
///////////////////////////////////////////////////////////////////////////////

// Factories
//----------

use \clearos\apps\users\User_Manager_Factory as User_Manager;

clearos_load_library('users/User_Manager_Factory');

// Classes
//--------

use \clearos\apps\base\File as File;
use \clearos\apps\base\Folder as Folder;

clearos_load_library('base/File');
clearos_load_library('base/Folder');

// Exceptions
//-----------

use \Exception as Exception;

///////////////////////////////////////////////////////////////////////////////
// M A I N
///////////////////////////////////////////////////////////////////////////////

$home_basename = '/home';
$home_perms = '0700';

try {
    $home_group = 'allusers';

    // Set home group do "domain users" in AD mode
    //--------------------------------------------

    if (clearos_library_installed('samba_common/Samba')) {
        clearos_load_library('samba_common/Samba');
        $samba = new \clearos\apps\samba_common\Samba();
        $mode = $samba->get_mode();

        if ($mode === \clearos\apps\samba_common\Samba::MODE_AD_CONNECTOR)
            $home_group = 'domain users';
    }

    // Grab /etc/skel file info
    //-------------------------

    try {
        $skel = new Folder('/etc/skel');
        $all_skel_files = $skel->get_listing(TRUE);
        $skel_files = array();

        foreach ($all_skel_files as $skel_file) {
            if (!$skel_file['is_dir'])
                $skel_files[] = $skel_file['name'];
        }
    } catch (Exception $e) {
        // Not fatal
    }

    // Create/manage user home dirs
    //-----------------------------

    $user_manager = User_Manager::create();
    $users = $user_manager->get_list();

    $folder = new Folder($home_basename);
    $homes = $folder->get_listing(FALSE, FALSE);

    foreach ($users as $user) {
        if (!in_array($user, $homes)) {
            $home = new Folder($home_basename . '/' . $user);
            $home->create($user, $home_group, $home_perms);
            clearos_log('accounts', 'created home directory: ' . $user);
        }

        try {
            foreach ($skel_files as $skel_filename) {
                $target = $home_basename . '/' . $user . '/' . $skel_filename;
                $source = '/etc/skel/' . $skel_filename;
                $target_file = new File($target);

                if (!$target_file->exists()) {
                    $source_file = new File($source);
                    $source_file->copy_to($target);
                    $source_file->chown($user, $home_group);
                    clearos_log('accounts', 'copied skel file: ' . $target);
                }
            }
        } catch (Exception $e) {
            // Not fatal
        }
    }
} catch (Exception $e) {
    exit(1);
}

// vim: syntax=php ts=4
