<?php  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#
namespace HipPlayConnect\FileSystem;


use HipPlayConnect\Configuration\AxFeatures;

class AxMountsHelper
{
    public static function getESATADeviceId()
    { 
        $eSATADeviceId = null;
        if ( false !== ( $fd = @fopen( '/proc/mounts', 'r' ) ) )
        {
            while ( !feof( $fd ) )
                if ( preg_match( '|^/dev/([^ ]+?)[0-9]* /mnt/eSata |', fgets( $fd ), $matches ) )
                {
                    $eSATADeviceId = $matches[ 1 ];
                    break;
                }
            fclose( $fd );
        }
        return $eSATADeviceId;
    }
    
    public static function isMainStorageMounted()
    {    
        // VMs always have a main storage mounted
        if ( AxFeatures::has('isVM') )
            return TRUE;
        
        // call the right function depending on what is the main storage
        return !AxFeatures::has('isOnlyUSBStorage') ? self::_isHomeMounted() : self::_isUSBStorageMounted();
    }

    private static function _isHomeMounted()
    { 
        $isHomeMounted = false;
        if ( false !== ( $fd = @fopen( '/proc/mounts', 'r' ) ) )
        {
            // Bug 16785: [Roxio] Use /home/0common as the main storage mount point in homebase (not used in practice)
            $regexp = '|^[^ ]+ ' . ( !AxFeatures::has('mainStorageMountPoint') ? '/home' : trim( AxFeatures::get('mainStorageMountPoint') ) ) . ' |';
            while ( !feof( $fd ) )
                if ( preg_match( $regexp, fgets( $fd ) ) )
                {
                    $isHomeMounted = true;
                    break;
                }
            fclose( $fd );
        }
        return $isHomeMounted;
    }
    
    private static function _isUSBStorageMounted()
    {
        return @filesize( '/var/axentra/usbmountpoints' ) != 0; // if the file does not exist, FALSE is returned, and FALSE == 0
    }    
}
