#!/usr/bin/env ruby

# NVrec configuration file generator

# $Id: makeConf.rb,v 1.6 2002/05/21 02:54:58 makeinu Exp $

# 2002/4/30  Kenji Suzuki <kenji@po.ganseki.ne.jp>
# Copyright (C) 2002 Kenji Suzuki <kenji@po.ganseki.ne.jp>
# This is free software with ABSOLUTELY NO WARRANTY.

# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

binDir  = File::dirname(File::expand_path( __FILE__ ))
baseDir = File::dirname(binDir)
confDir = baseDir + "/conf"

# generate conf/NormalQ.conf,LowW.conf etc
open("#{confDir}/quality.conf.in") do |qualityFile|
	qualityFile.each do |line|
		next if (line =~ /^#/)
		next unless (line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/ )
		ary = line.chomp.split(/\s+/)
		quality = ary[0]
		w = ary[1]
		h = ary[2]
		vb = ary[3]
		ab = ary[4]
		r = ary[5]
		codec = ary[6]
		if (codec == 'xvid') then
			codec = '-xvid_divx'
		elsif (codec == 'divx') then
			codec = ''
		end

		confFile = confDir + "/" + quality + ".conf"
		file = open(confFile, "w")
		#file.print("-frequency #{frequency}\t# #{channel} ch\n\n")

		file.print("-v /dev/video0\n")
		file.print("-norm NTSC\n")
		#file.print("-input Television\n\n")

		file.print("#{codec}\t# use xvid codec?\n")
		file.print("-w #{w}\t# width #{w} pixcel\n")
		file.print("-h #{h}\t# height #{h} pixcel\n")
		file.print("-vb #{vb}\t# video bitrate #{vb}kbit/s\n")
		file.print("-vq 95\t# video quality factor\n\n")

		file.print("-r #{r}\t# audio sample rate #{r} Hz\n")
		file.print("-ab #{ab}\t\t# audio bitrate #{ab}kbit/s\n")
		file.print("-aq 100\t\t# audio quality factor\n\n")
		file.print("-s stereo\t# stereo\n")
		file.close

	end
end


# generate conf/channel.conf
open("#{confDir}/channel_table.conf") do |file|
	channelConf = open("#{confDir}/channel.conf", "w")
  file.each do |line|
    next if (line =~ /^#/)
    next unless (line =~ /(\S+)\s+(\S+)/ )
    ary = line.chomp.split(/\s+/)
    channel = ary[0]
		freq = ary[1]

		mixvol = ""
		#mixvol = "-mixvol /dev/mixer0:cd:80"

		channelConf.print("#{channel} -input Television -frequency #{freq} #{mixvol}\n")

	end
	channelConf.close
end

