#!/usr/bin/env python

# Copyright (C) 2006-2011 Oracle. All rights reserved.
#
# 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, version 2.  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, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 021110-1307, USA.

import sys
import subprocess
import string
import re



def main():

 
    partition = '0'

    try:

        power_dev = sys.argv[1]
  
        partition = sys.argv[2]

        if partition != '0':
            power_dev = power_dev.replace(partition, "")
            
        power_p = subprocess.Popen(["/sbin/powermt",
                                 "display",
                                 "dev=%s" % power_dev],
                                 stdout = subprocess.PIPE,
                                 stderr = subprocess.PIPE)
        (p_out, p_err) = power_p.communicate()  

        if power_p.returncode != 0:
            return

        power_lines_LIST = map( string.strip, p_out.splitlines() )

        entries = len(power_lines_LIST)

        # found second line index of "========================"
        mc = 0
        for index in range(0, entries):
            match=re.search(r'====================', power_lines_LIST[index])
            if match:
                if mc == 1:
                    break
                else:
                    mc = mc + 1 

        if index >= entries:
            return

        for index in range(index, entries):
            sd = re.search(r' sd[a-z]*', power_lines_LIST[index])
            if sd:
                scsi_dev = sd.group().strip()
                st = re.search(r' alive', power_lines_LIST[index])
                if st:
                    scsi_id_p = subprocess.Popen(["/lib/udev/scsi_id",
                                 "--whitelisted",
                                 "--replace-whitespace",
                                 "--device=/dev/%s" % scsi_dev],
                                 stdout = subprocess.PIPE,
                                 stderr = subprocess.PIPE)
                    (p_out, p_err) = scsi_id_p.communicate()

                    if scsi_id_p.returncode != 0:
                        return

                    if partition != '0':
                        p_out = p_out.strip() + 'p' + ('%s' % partition)
                    sys.stdout.write(p_out)
                    break




    except:
        return

if __name__ == "__main__":
    main()
