#!/bin/bash # # qamscan.sh # # public release 0.6 # # script to scan DVB device for QAM channels # for importing into MythTV # # P Watkins, 2006 # user input # # $tables -- space-delimited list of table types if [ "$tables" = "" ]; then tables="Standard HRC IRC" fi # # $device -- DVB device number if [ "$device" = "" ]; then device="0" fi # # $timeout -- how long to try each channel if [ "$timeout" = "" ]; then timeout=6 fi if [ "$DISPLAY" == "" ]; then echo "No DISPLAY variable found; please run this on a system with X and mplayer" >/dev/stderr exit 2 fi echo -n "making working directory... " tmpdir="" while [ "$tmpdir" = "" ]; do d="/tmp/qamscan.$RANDOM" mkdir -m 0750 $d && tmpdir="$d" done echo "work directory is $tmpdir" get_scan_channels() { table="$1" atscdumpfile=${tmpdir}/atscscan_results-${table}.txt cat $atscdumpfile > ${tmpdir}/scan_channels.txt num=`wc -l < ${tmpdir}/scan_channels.txt` if [ $num -lt 1 ]; then return fi if [ $num -gt 0 ]; then echo "found $num channels, making $HOME/.azap/channels.conf" mkdir $HOME/.azap 2>/dev/null if [ \( -f $HOME/.azap/channels.conf \) -a \( \! -f ${tmpdir}/channels.conf.$$ \) ]; then echo "copying $HOME/.azap/channels.conf to ${tmpdir}/channels.conf.$$" cp -p $HOME/.azap/channels.conf ${tmpdir}/channels.conf.$$ fi cat ${tmpdir}/scan_channels.txt > $HOME/.azap/channels.conf else echo "No channels found" fi } test_lock() { name="$1" start=`date +%s` stop=`expr $start + $timeout` echo "trying to tune $name" ${HERE}/dvb-apps/util/szap/azap -a $device -r "$name" & sleep 2 mplayer_test "$name" } mplayer_test() { name="$1" echo "Testing $table channel $name in mplayer" mplayer -v -nosound /dev/dvb/adapter${device}/dvr0 >${tmpdir}/mplayer.out & sleep $timeout killall azap killall -9 azap killall mplayer killall -9 mplayer sleep 1 # give killall time # alternate? mplayer -nosound "dvb://$name" ans="N" mplayer_count=`grep "Found video stream" ${tmpdir}/mplayer.out |wc -l` if [ $mplayer_count -gt 0 ]; then echo echo "This (channel $name) looks like a channel we can tune!" echo ans="Y" fi #ask "Should we use this channel ($name)?" if [ "$ans" = "Y" ]; then echo "$name" >> ${tmpdir}/good_channels.txt cat $HOME/.azap/channels.conf >> $tmpdir/channels.conf.new fi } ask() { q="$1" ans="" while [ "$ans" = "" ]; do printf "%s (Y or N) " "$q" read ans ans=`echo $ans | tr '[:lower:]' '[:upper:]'` if [ \( "$ans" != "Y" \) -a \( "$ans" != "N" \) ]; then ans="" fi done } show_vm() { v=`grep VmallocUsed /proc/meminfo` echo "`date`: vmalloc = $v" | tee -a ${tmpdir}/vminfo.txt } HERE=`pwd` cd dvb-apps/util/scan if [ $? -ne 0 ]; then echo "please cd to the directory that contains 'dvb-apps/'" >/dev/stderr exit 1 fi for table in $tables; do if [ ! -f "${HOME}/channels.conf.new" ]; then # look for channels if [ -f "${HOME}/atscscan_results-$table.txt" ]; then echo "not scanning; using using ${HOME}/atscscan_results-$table.txt" echo "If you want a fresh scan, remove or rename ${HOME}/atscscan_results-$table.txt" echo "and run this script again" cat "${HOME}/atscscan_results-$table.txt" > ${tmpdir}/atscscan_results-$table.txt else echo "About to scan $table frequencies with atscscan;" printf "%s" "This could take some time. Press [Enter] to continue." read foo show_vm ./atscscan -v -A 3 atsc/us-Cable-${table}-center-frequencies-QAM256 > ${tmpdir}/atscscan_results-$table.txt show_vm if [ -f "${HOME}/atscscan_results-$table.txt.add" ]; then echo "merging scan results with ${HOME}/atscscan_results-$table.txt.add" cat ${tmpdir}/atscscan_results-$table.txt "${HOME}/atscscan_results-$table.txt.add" | sort | uniq > $tmpdir/scan_sorted mv $tmpdir/scan_sorted ${tmpdir}/atscscan_results-$table.txt fi fi echo "About to parse atscscan output..." get_scan_channels $table if [ -s ${tmpdir}/scan_channels.txt ]; then # found channels! echo < ${tmpdir}/good_channels.txt numch=`wc -l < ${tmpdir}/scan_channels.txt` i=1 echo "About to test $numch channels" while [ $i -le $numch ]; do head -$i ${tmpdir}/scan_channels.txt | tail -1 > $HOME/.azap/channels.conf name=`head -$i ${tmpdir}/scan_channels.txt | tail -1 | awk -F\: '{print $1}'` echo "`date` testing ${i}th channel of ${numch}, $name" >> ${tmpdir}/status.txt vt=`dmesg | grep vmalloc | grep -v '^Kernel command line'` if [ "$vt" != "" ]; then echo "WARNING: vmalloc errors; you need to boot with more vmalloc" | tee -a ${tmpdir}/status.txt fi show_vm test_lock $name i=`expr $i + 1` done fi else cp ${HOME}/channels.conf.new $tmpdir/ cp ${HOME}/channels.conf.new $tmpdir/scan_channels.txt cut -d: -f1 < ${HOME}/channels.conf.new > $tmpdir/good_channels.txt fi if [ -s ${tmpdir}/scan_channels.txt ]; then numchannels=`wc -l < ${tmpdir}/good_channels.txt` if [ $numchannels -gt 0 ]; then channels="" for c in `cat ${tmpdir}/good_channels.txt`; do channels="${channels}, $c" done echo "We found the following $numchannels channels:" echo $channels | sed 's:^, ::' ask "Do you want to rename these channels?" if [ $ans = "Y" ]; then cat /dev/null > $tmpdir/renamed names="" # count how many channels to try count=`wc -l < $tmpdir/channels.conf.new` i=1 while [ $i -le $count ]; do # tune one by one; rebuild channels.conf in case there are name conflicts head -$i $tmpdir/channels.conf.new | tail -1 > $HOME/.azap/channels.conf name=`awk -F\: '{print $1}' < $HOME/.azap/channels.conf` printf 'About to tune "%s"; exit mplayer when ready to rename\n' "$name" printf 'Press Enter to continue' read x # tune the channel ${HERE}/dvb-apps/util/szap/azap -a $device -r "$name" >/dev/null 2>/dev/null & # tell mpayer to display it mplayer -nosound /dev/dvb/adapter${device}/dvr0 >/dev/null 2>/dev/null # user has terminated mplayer killall azap >/dev/null 2>/dev/null killall -9 azap >/dev/null 2>/dev/null # now ask what name to use ans="N" while [ $ans = "N" ]; do printf 'What should we name that channel? ' read name # only safe characters name=`echo $name | sed 's:[^a-zA-Z0-9\ \/\-\_]::g'` # make sure that name isn't already in use used=`grep "^${name}:" $tmpdir/renamed | wc -l` if [ $used -gt 0 ]; then echo "Name \"$name\" is already in use; pick another" else # looks like a good name; prompt for confirmation to be sure no typos ask "Confirm: should we use \"$name\" for this channel?" fi done # we have a good name for the channel; add it to the "renamed" work file sed "s,^[^\\:]*:,$name:," < $HOME/.azap/channels.conf >> $tmpdir/renamed # and add it to our list names="${names}, $name" # and incr 1 i=`expr $i + 1` done # re-create the channels.conf file with the newly renamed channels cat $tmpdir/renamed > $HOME/.azap/channels.conf # echo what we have names=`echo $names | sed 's:, $::'` echo "New channel names in $HOME/.azap/channels.conf: $names" fi ask "Should we rebuild channels.conf with these channels?" if [ "$ans" = "Y" ]; then cat $tmpdir/channels.conf.new > $HOME/.azap/channels.conf echo "$HOME/.azap/channels.conf has been rebuilt" dupes=`awk -F\: '{print $1}' < $HOME/.azap/channels.conf|sort|uniq -c|awk '$1 > 1 {print}'|wc -l` if [ $dupes -gt 0 ]; then echo "WARNING: $HOME/.azap/channels.conf has some lines with duplicate channel names!" fi echo "You should now try to import this using mythtvsetup" exit 0 fi fi exit fi done # if we got here, we didn't find channels or didn't agree to update channels.conf if [ -f "${tmpdir}/channels.conf.$$" ]; then echo "restoring $HOME/.azap/channels.conf" cp -p "${tmpdir}/channels.conf.$$" $HOME/.azap/channels.conf fi