#!/usr/bin/env python
import rospy
import os

"""
This is a simple wrapper around rosbag to record rosout_agg for later analysis
"""

rospy.init_node('rosout_agg_recorder')

output_dir = rospy.get_param('~output_directory')
max_bag_size = rospy.get_param('~max_bag_size_mb', '')
compress = rospy.get_param('~compress', False)
buffer_size_mb = rospy.get_param('~buffer_size_mb', 1024)

if not os.path.exists(output_dir):
    rospy.loginfo('Creating output directory at {0}'.format(output_dir))
    os.makedirs(output_dir)

output_prefix = os.path.join(output_dir, 'rosout_agg')

args = ['rosbag', 'record']

args += ['-o', output_prefix]
args += ['-b', str(buffer_size_mb)]

if max_bag_size:
    args += ['--split', '--size={0}'.format(int(max_bag_size))]

if compress:
    args += ['-j']

# Add topics to be recorded
args += ['/rosout_agg']


os.execvp('rosbag', args )
