#!/usr/bin/python

# Copyright (c) 2008 Motorola, Inc. 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 version 2
# as published by the Free Software Foundation.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

import time
import os
import stat
import sys

try:
    import tarfile
except ImportError:
    print('Python 2.3 or later is needed to be able to add device files to the rootdisk tarball.')
    sys.exit(1)

if len(sys.argv) != 4:
    print('usage: %s <rootdisk.tar> <gen_init_cpio_list> <prefix>') % sys.argv[0] 
    sys.exit(1)

tar = tarfile.open(sys.argv[1], 'a')
list = open(sys.argv[2], 'r')
info = tarfile.TarInfo()
info.uname = 'root'
info.gname = 'root'
info.mtime = time.time()
hasPrefix = True
if info.__dict__.has_key('prefix'):
    info.prefix = sys.argv[3]
else:
    hasPrefix = False

for line in list.readlines():
    if line[0] == '#' or len(line) < 10:
        continue
    l = line.split(' ');
    info.name = l[1].strip('/')
    if not hasPrefix:
        info.name = sys.argv[3] + '/' + info.name
    info.size = 0
    if l[0] == 'file':
        # file <name> <location> <mode> <uid> <gid>
        info.mode = int(l[3], 8)
        info.uid = int(l[4])
        info.gid = int(l[5])
        info.type = tarfile.REGTYPE
        info.size = os.stat(l[2])[stat.ST_SIZE]
        tar.addfile(info, open(l[2]))
    elif l[0] == "dir":
        # dir <name> <mode> <uid> <gid>
        info.mode = int(l[2], 8)
        info.uid = int(l[3])
        info.gid = int(l[4])
        info.type = tarfile.DIRTYPE
        tar.addfile(info)
    elif l[0] == "nod":  
        # nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
        info.mode = int(l[2], 8)
        info.uid = int(l[3])
        info.gid = int(l[4])
        if l[5] == 'c':
            info.type = tarfile.CHRTYPE
        else:
            info.type = tarfile.BLKTYPE
        info.devmajor = int(l[6])
        info.devminor = int(l[7])
        tar.addfile(info)
    elif l[0] == "slink":  
        # slink <name> <target> <mode> <uid> <gid>
        info.linkname = l[2]
        info.mode = int(l[3], 8)
        info.uid = int(l[4])
        info.gid = int(l[5])
        info.type = tarfile.SYMTYPE
        tar.addfile(info)
    elif l[0] == "pipe":  
        # pipe <name> <mode> <uid> <gid>
        print "pipe is not supported\n"
        sys.exit(1)
    elif l[0] == "sock":  
        # sock <name> <mode> <uid> <gid>
        print "sock is not supported\n"
        sys.exit(1)
    else:
        print "bad gen_init_cpio list file\n"
        sys.exit(1)
list.close()
tar.close()
