RIGO.INFO TechBlog

Eventually I get into producing something I think is useful for others. You'll find random technical knowledge in this Blog so please be patient and use the search function. Some of these software are rather lame and old, the list is ordered by the approximate age (newest first). Unfortunately my older stuff are lost or scattered around my old CDs, not readable anymore 8(.

DistrOggize

A small shell script which allows recursive parallel distributed recoding of directories containing mp3 files. It uses ssh, oggenc and mp3info to do the work.

#!/bin/bash
 
set -e
 
echo "distributed oggifying directory $1 ... please be patient"
 
oggdir="$1.oggized"
 
mkdir -p "$oggdir"
 
tmpdir="/tmp/distoggize_"`date +%s%N`$RANDOM
echo $tmpdir
 
trap cleanup 0
 
cleanup() {
    echo "cleaning up..."
    for host in $peers; do 
        ssh $host "rm -rf $tmpdir"
    done
}
 
makeogg() {
    peerhost="$1"
    echo "** $peerhost"
 
    dir=`dirname "$mp3"`
    file=`basename "$mp3"`
    ogg=`basename "$mp3" .mp3`.ogg
 
    tracknum=`mp3info -p "%n" "$mp3"`
    artist=`mp3info -p "%a" "$mp3"`
    album=`mp3info -p "%l" "$mp3"`
    title=`mp3info -p "%t" "$mp3"`
 
    pushd "$oggdir" > /dev/null
    mkdir -p "$dir"
 
    tmpoggfile="$tmpdir/$dir/$ogg"
    tmpoggdir=`dirname "$oggfile"`
 
    mpg123 -q -w- ../"$mp3" | ssh "$peerhost" "mkdir -p \"$tmpoggdir\" ; oggenc --tracknum \"$tracknum\" --artist \"$artist\" --album \"$album\" --title \"$title\" -b 64 -o \"$tmpoggfile\" - ; cat \"$tmpoggfile\" ; rm -f \"$tmpoggfile\"" > "$dir/$ogg" ; 
 
    popd > /dev/null
 
}
 
DIST_INIT() {
    export C_RESET="\033[0m"
 
    export C_GRAY="\033[1;30m"
    export C_GREY="\033[1;30m"
    export C_RED="\033[1;31m"
    export C_GREEN="\033[1;32m"
    export C_YELLOW="\033[1;33m"
    export C_BLUE="\033[1;34m"
    export C_PURPLE="\033[1;35m"
    export C_MAGENTA="\033[1;35m"
    export C_CYAN="\033[1;36m"
    export C_WHITE="\033[1;37m"
 
 
    DIST_flagdir=`mktemp -d`
    #mkdir -p $DIST_flagdir
 
    DIST_peers="$*"
 
    for DIST_peer in $DIST_peers; do
	touch "$DIST_flagdir/$DIST_peer"
    done
 
    echo -e "\n${C_RESET}${C_GREEN}Initializing distributed running environment...${C_RESET}"
 
}
 
DIST_RUN() {
    # run task in background when a worker host is available
    while true; do
	DIST_peerhost=`ls -1 "$DIST_flagdir" | head -n1`
        if [ "$DIST_peerhost" ]; then
	    echo -e "\n${C_RESET}${C_GREEN}Starting task ${C_MAGENTA}$1${C_GREEN} on ${C_YELLOW}$DIST_peerhost${C_GREEN}...${C_RESET}"
	    (
		rm -fv "$DIST_flagdir/$DIST_peerhost" ; 
		"$1" "$DIST_peerhost"
		touch "$DIST_flagdir/$DIST_peerhost" ;
	    )&
	    break;
	fi
        sleep 1
    done
}
 
DIST_WAIT_FINISH() {
    # wait for the tasks to finish
    for DIST_peerhost in $DIST_peers; do
	echo -e "\n${C_RESET}${C_GREEN}Waiting for task on ${C_YELLOW}$DIST_peerhost${C_GREEN} to finish...${C_RESET}"
	while true; do
    	    if [ -f "$DIST_flagdir/$DIST_peerhost" ]; then
		break
	    fi
    	    sleep 1
	done    
	echo -e "\n${C_RESET}${C_GREEN}Task on ${C_YELLOW}$DIST_peerhost${C_GREEN} finished.${C_RESET}"
    done
    rm -rf "$DIST_flagdir"
    rm -rf "$DIST_logdir"
}
 
DIST_INIT "localhost mcree.myftp.org tricon.hu bofors.ikk.sztaki.hu dobrogi.ikk.sztaki.hu nat.sztaki.hu leeloo.kiskapu.hu leona.kiskapu.hu"
 
find "$1" -type f -iregex ".*.mp3$" | while read mp3; do 
 
    echo "... processing $mp3"
 
    mp3gain -r -k ../"$mp3"
 
    DIST_RUN makeogg
 
done
 
DIST_WAIT_FINISH
 
cleanup

Display countdown timer until given date using JavaScript from PHP

This function is used in my custom computer exam software to display a countdown timer that stops at the end of the exam. The timestamp is determined and stored in a file when the student clicks „begin exam”1) then it is resilient to page reloads since the destination timestamp never changes. Of course the timer is not exact since network latency is not taken into account, but this code is only used on a LAN 8-)

function jscd($ts) {
  $curts=time();
  $res="";
  $res.="<span id=cd></span>";
  $res.="<SCRIPT type='text/javascript' language='javascript'>
    var dst=(new Date().getTime()/1000) + $ts-$curts;
    function pitty() {
      var t=new Date().getTime()/1000;
      var diff=Math.floor(dst - t);
      var min=Math.floor(diff/60);
      var sec=diff%60;
      if(diff<0) {
        document.getElementById('cd').innerHTML = 'Lejárt az idő';
      } else {
        document.getElementById('cd').innerHTML = 'Hátralevő idő: ' + min + ' perc ' + sec + ' másodperc';
      }
      setTimeout('pitty()',100);
    }
    setTimeout('pitty()',100);";
  $res.="</SCRIPT>";
  return $res;
}

CVS home dir

A small bash script to be run from your .bashrc. This is practical for multi-homed users - like myself - to keep ssh authorized keys, bookmarks, bash settings, aliases, etc. in sync amongst different accounts.

#!/bin/bash
#
# CVSHOME - small BASH script to be run from your .bashrc
# this is practical for multi-homed users - like myself -
# to keep ssh authorized keys, bookmarks, bash settings, 
# aliases, etc. in sync amongst different accounts.
#
# GPLv2 by Erno Rigo <mcree AT tricon DOT hu>
#
 
# you should place these exports in an external 
# file if you are planning to keep cvshome itself 
# in its own CVS repository!
 
export CVS_RSH=ssh # enter your favorite RSH here
export CVSROOT=:ext:tricon.hu/var/lib/cvs # enter your CVSROOT here... I'm combinig ssh-keychain with this so it's quite convenient - for me 8)
 
export CVSHOME_ROOT=~mcree/.cvshome_root # local scratchpad
export CVSHOME_FILES="/home/mcree/.bashrc /home/mcree/.crontab" # files to keep in cvs
export CVSHOME_SAFEFILES="/home/mcree/.ssh/authorized_keys2" # files to keep in cvs, keeps user asked before upgrades
export CVSHOME_KEY="trallalala" # cvs repository "key" - you should share this amongst your homes
export CVSHOME_EDITOR="$EDITOR" # your favourite editor (used for conflict management)
#export CVSHOME_DEBUG="yes" # comment out to disable DEBUG messages (recommended)
export CVSHOME_NOTICE="yes" # comment out to disable NOTICE messages (not recommended)
 
######################################################################################
# BEWARE - DO NOT CROSS THIS LINE
#
 
cvshome_debug() {
    if [ $CVSHOME_DEBUG ]; then 
	echo `date`" cvshome: DEBUG: "$@
    fi
}
 
cvshome_fatal() {
    echo `date`" cvshome: FATAL: "$@
}
 
cvshome_notice() {
    if [ $CVSHOME_NOTICE ]; then 
	echo `date`" cvshome: NOTICE: "$@
    fi
}
 
cvshome_update_base() {
    #copy files if they differ
    cvshome_debug "searching for repo changes..."
    for file in $CVSHOME_FILES; do 
	if ! cmp "$file" "$CVSHOME_ROOT"/repo/"$file" > /dev/null; then
    	    cvshome_debug "changed in repo: $file"
	    cp "$CVSHOME_ROOT"/repo/"$file" $file
	    touch "$CVSHOME_ROOT"/need_commit
	fi
    done	
 
    #take account of safe files
    for file in $CVSHOME_SAFEFILES; do 
	if ! cmp "$file" "$CVSHOME_ROOT"/repo/"$file" > /dev/null; then
    	    cvshome_debug "changed in repo (SAFE): $file"
	    mkdir -p "$CVSHOME_ROOT"/safe
	    cp --parents "$file" "$CVSHOME_ROOT"/safe
	    touch "$CVSHOME_ROOT"/need_commit
	fi
    done	
}
 
cvshome_update_repo() {
    #copy files if they differ
    cvshome_debug "searching for local changes..."
    for file in $CVSHOME_FILES $CVSHOME_SAFEFILES; do 
	if ! cmp "$file" "$CVSHOME_ROOT"/repo/"$file" > /dev/null; then
    	    cvshome_debug "changed locally: $file"
	    cp --parents $file "$CVSHOME_ROOT"/repo	
	    touch "$CVSHOME_ROOT"/need_commit
	fi
    done	
 
}
 
cvshome_update() {
    if [ -f "$CVSHOME_ROOT"/update_failed ]; then
	cvshome_fatal "last update failed, need manual cleanup!"
	return 0
    fi
    cvshome_update_repo
    cvshome_debug "uptading from repo..."
    pushd "$CVSHOME_ROOT"/repo > /dev/null
    updateres=`cvs -Q update -A 2>&1`
    if [ ! -z "$updateres"  ]; then
	cvshome_fatal "cannot update: $updateres"
	touch "$CVSHOME_ROOT"/update_failed
	return 0
    fi
    popd > /dev/null
    cvshome_update_base
    cvshome_commit
}
 
cvshome_commit() {
    if [ -f "$CVSHOME_ROOT"/need_commit ]; then
	cvshome_debug "commiting..."
	pushd "$CVSHOME_ROOT"/repo > /dev/null
	cvs -Q commit -m `date +%s`
	popd > /dev/null
	rm -f "$CVSHOME_ROOT"/need_commit
    else
	cvshome_debug "no need to commit."
    fi
}
 
 
cvshome_bg() {
    cvshome_debug "background init..."
    mkdir -p "$CVSHOME_ROOT"
 
    export CVSHOME_REPO="cvshome_"$CVSHOME_KEY
 
    if [ ! -d "$CVSHOME_ROOT"/repo ]; then
	cvshome_debug "no local copy present trying to checkout..."
	pushd "$CVSHOME_ROOT" > /dev/null
	cvs -Q checkout -d repo -D now "$CVSHOME_REPO" 2>/dev/null 1>/dev/null
	popd > /dev/null
    fi
 
    if [ ! -d "$CVSHOME_ROOT"/repo ]; then
 
	cvshome_debug "no existing repository... creating one for you"
	mkdir -p "$CVSHOME_ROOT"/"$CVSHOME_REPO"
 
	for file in $CVSHOME_FILES $CVSHOME_SAFEFILES; do 
	    cvshome_debug "initial copy: $file"
	    cp --parents $file "$CVSHOME_ROOT"/"$CVSHOME_REPO"
	done	
 
	cvshome_debug "importing..."
	pushd "$CVSHOME_ROOT"/"$CVSHOME_REPO" > /dev/null
	cvs -Q import -m "initial" "$CVSHOME_REPO" "$CVSHOME_REPO" "initial" 
	popd > /dev/null
 
	cvshome_debug "checking out..."
	pushd "$CVSHOME_ROOT" > /dev/null
	cvs -Q checkout -d repo -D now "$CVSHOME_REPO"
	popd > /dev/null
 
	if [ ! -d "$CVSHOME_ROOT"/repo ]; then
	    cvshome_fatal "cannot checkout: cvs checkout -d repo -D now $CVSHOME_REPO"
	    return 0
	fi
 
	rm -rf "$CVSHOME_ROOT"/"$CVSHOME_REPO"
 
	cvshome_notice "new repository was created as $CVSHOME_REPO"
    fi
 
    cvshome_update
}
 
cvshome_prompt() {
    while read -p "$1 ["`echo "$2" | tr ' ' '/' `"] " ans; do
	for pos in $2; do
	    if [ "$pos" == "$ans" ]; then
		echo "$ans"
		return;
	    fi
	done
    done
}
 
cvshome_init() {
 
    # check for CVS conflicts
    if [ -f "$CVSHOME_ROOT"/update_failed ]; then
	cvshome_debug "previous update failed, searching for conflicts..."
	corrfile=`mktemp`
        echo "no" > $corrfile;
	exec 3<&0
	find "$CVSHOME_ROOT"/repo -regex ".*/Entries" | while read file; do 
	    grep -h "/[^/]*/[^/]*/[^+/]*+[^/]*/" "$file" | while read entry; do
		realdir=`dirname "$file"`
		realdir=`dirname "$realdir"`
		realfile="$realdir/"`echo "$entry" | cut -f2 -d '/'`
		ans=`cvshome_prompt "CVS conflict detected in: $realfile. Correct it?" "yes no" <&3`
		if [ "$ans" == "yes" ]; then
		    $CVSHOME_EDITOR "$realfile" <&3
		else
		    echo "yes" > $corrfile;
		fi
	    done
	done
	exec 3>&-
	if [ `cat $corrfile` == "no" ]; then
	    rm -f "$CVSHOME_ROOT"/update_failed
	fi
	rm -f $corrfile
    fi
 
    # check for changed SAFE files
    if [ -d "$CVSHOME_ROOT"/safe ]; then
	cvshome_debug "searching for changed SAFE files..."
	pushd "$CVSHOME_ROOT"/safe >/dev/null
	exec 3<&0
	find . -type f | while read file; do
	    diff -ruN "/$file" "$CVSHOME_ROOT"/repo/"$file"
	    ans=`cvshome_prompt "SAFE file changed: $file. Update locally?" "yes no abort" <&3`
	    if [ "$ans" == "abort" ]; then
		touch "$CVSHOME_ROOT"/update_failed
	    elif [ "$ans" == "no" ]; then
		cvshome_notice "forcing local file version to CVS"
		cp --parents "/$file" "$CVSHOME_ROOT"/repo/"$file"
		touch "$CVSHOME_ROOT"/need_commit
	    else
		cvshome_notice "upgrading file from CVS"
		rm -f "$CVSHOME_ROOT"/safe/"$file"
	    fi
	done
	exec 3>&-
	popd >/dev/null
    fi
}
 
# actions requiring user interaction
cvshome_init
# start background processing... for faster prompts 8)
cvshome_bg &
2010-05-02 20:06 · 0 Linkbacks

XStreamWrapper

xstreamwrapper.zip - I've written a java wrapper class for using XStream in my way. Also here you can find a very simple example which can be used to save and restore application configuration from an .xml conffile.

2010-05-02 20:06 · 0 Linkbacks

Wainting for samba mounts upon login to KDE

Since my employer introduced the policy of switching off office PCs during night hours I'm keeping my documents and other important stuff (most notably my KDE Desktop directory) on a remote samba share. When I arrive to the office in the morning it's useful to have KDE wait for the Desktop directory to be mounted, that in turn only happens after my PC obtains an address via DHCP. Often I was too quick to supply my login credentials so KDE panicked and scattered the contents of my home dir on my desktop instead the contents of the Desktop dir.

I've placed the following small script to an executable file named .xsessionrc in my home directory. The script uses kdialog to display a progress meter that counts for 100 seconds or until my Desktop directory can be found. Then it disposes the progress bar dialog via the KDE DCOP service.

#!/bin/bash
dcopserver

DCOPREF=`kdialog --progressbar "Waiting for samba mounts"  --title "Waiting..."`
DCOPCLIENT=`dcopclient "$DCOPREF"`
DCOPOBJECT=`dcopobject "$DCOPREF"`

dcop "$DCOPCLIENT" "$DCOPOBJECT" showCancelButton true
dcop "$DCOPCLIENT" "$DCOPOBJECT" ignoreCancel true

for i in `seq 1 100`; do
    dcop "$DCOPCLIENT" "$DCOPOBJECT" setProgress $i
    cancel=`dcop "$DCOPCLIENT" "$DCOPOBJECT" wasCancelled`
    if [ "$cancel" = "true" -o -d "/mnt/cifs/mcree/Desktop" ]; then
        dcop "$DCOPCLIENT" "$DCOPOBJECT" close
        exit
    fi
    sleep 1
done
2010-05-02 20:06 · 0 Linkbacks

Linkbacks

Use the following URL for manually sending trackbacks: https://rigo.info/lib/plugins/linkback/exe/trackback.php/en:blog
en/blog.txt · Utolsó módosítás: 2009-05-15 00:00 (külső szerkesztés)
CC Attribution-Noncommercial-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0