2008-04-28

My routine network rsync backup script

#!/bin/bash
BACKUP_TARGET=rags.ath.cx:~/backups
#BACKUP_TARGET=csf:~/backups

cd $HOME

PATH_FILES=""
PATH_FILES="$PATH_FILES .local/bin/"
PATH_FILES="$PATH_FILES .kde/share/apps/knotes/"
#for f in $PATH_FILES ; do
# echo ============
# echo rsync -avz "$f" "$BACKUP_TARGET/$f"
# rsync -avz "$f" "$BACKUP_TARGET/$f"
#done

DATA_FILES=""
DATA_FILES="$DATA_FILES Desktop Thesis Projects Papers"
DATA_FILES="$DATA_FILES /usr/local/log"
DATA_FILES="$DATA_FILES sysnotes"

BKMK_FILE=""
BKMK_FILES="$BKMK_FILES .kde/share/apps/konqueror/bookmarks.xml"
BKMK_FILES="$BKMK_FILES .mozilla/firefox/*/bookmarks.html"

INIT_FILES=""
INIT_FILES="$INIT_FILES .bash_profile .bashrc .gdbinit .gtkrc* .kilescp*"
INIT_FILES="$INIT_FILES .log* .octave* .screenrc .twinrc"
INIT_FILES="$INIT_FILES .vim .vimrc .mc .xmms"
INIT_FILES="$INIT_FILES .xmodmap.conf .Xresources .xinitrc .fonts.conf "
INIT_FILES="$INIT_FILES .icewm"
echo ============
echo rsync -avz $DATA_FILES $PATH_FILES $BKMK_FILES $INIT_FILES $BACKUP_TARGET
rsync -avz $DATA_FILES $PATH_FILES $BKMK_FILES $INIT_FILES $BACKUP_TARGET


#INIT_FILES="$INIT_FILES .icewm/{pref*,tool*,menu,keys,win*,theme*}"
#rsync -rv ~/dist/gwc-lib-0.05 $BACKUP_TARGET
#rsync -rv ~/vtkCISG-MIF/objects/library/CISGRegistration $BACKUP_TARGET
#rsync -rv ~/comparop $BACKUP_TARGET
#rsync -rv ~/nag $BACKUP_TARGET
#rsync -rv ~/TMI $BACKUP_TARGET

echo Backup complete, press enter to continue...
read

Orient JPEGs so if in portrait orientation, rotate 90 degrees to make a landscape image

Useful to get maximum resolution available on e.g. regular or widescreen monitors to view images.

$ cat wide-orientation
#!/bin/sh

for f in "$@" ; do

  eval `identify "$f" | cut -f3 -d' ' | sed  's/x/; export H=/ ; s/^/export W=/' `
  if [ "$H" -gt 0  -a  "$W" -gt 0  -a  $H -gt $W  ] ; then
    echo Changing $f from ${W}x${H} to ${H}x${W}...
    #convert -verbose -rotate "90<" $f $f
    jpegtran -rotate 90 -trim "$f" > "$f.1"
    mv -f "$f.1"  "$f"
  else
    echo Not altering "$f"...
  fi

   # this alone will do it, but it's SLOW
   # convert -verbose -rotate "90<" $f $f

done


# vim:ft=sh

Using VIM's tags interface for OCaml programs via otags and ctags

Two scripts, octags calls the correct tags program (ctags or otags) and calls fixocamltags.pl to do fixups so VIM can read the output of otags(1).

$ cat octags
#!/bin/bash

# expected calling sequnce:
# ctags -f - --format=2 --excmd=pattern --fields=nks   \
#       --sort=no --language-force=c --c-types=dgsutvf \
#       "/home/maali/ubasic.c"
echo ctags "$@" > /tmp/run
FILENAME=${BASH_ARGV[0]}

# fix-ocaml-tags.pl script
FIXTAGS=$HOME/.local/bin/fixocamltags.pl


if [[ $FILENAME =~ \.ml[i]?\$ ]] ; then
  # otags has ugly output
  #/usr/bin/otags -o - $FILENAME | $FIXTAGS

  # ocamltags clobbers TAGS in cwd
  ocamltags $FILENAME > /tmp/run 2>&1 ; $FIXTAGS TAGS
else
  /usr/bin/ctags "$@"
fi

$ cat fixocamltags.pl
#!/usr/bin/perl
use Switch;
use Cwd;

# take an emacs-style tags file
# from 'otags' and put it in
# for for taglist vim module
# Note: 'ocamltags' might provide nicer output

$sep = "\t";
$newsection = 1;

while (<>) {
  # skip blank lines or ^L mean new section
  #if ( m/^\s+$/ ) { print "#"; next; }
  $line = $_ ;

  # section begins with ^L
  if ( $line =~ m/^\x0C$/ ) { # \x0c = ^L
    $newsection = 1;
    #print STDERR "==> New section\n";
    next;
  }
 
  # first line of each section has a filename
  if ( $newsection && ($line =~ m/^([^,]+),[0-9]+$/ ) ) {
    $fname = getcwd().'/'.$1 ;
    #print STDERR "==> New file $fname\n";
    $newsection = 0;
    next;
  }

  $newsection = 0;
  # original:
  # (stmt pattern)^?(symbol)^A(linenum),(symbol idnum)
  if ( $line =~ /^(.*)(.*)(.*)\,(.*)$/ ) {
    ($pat, $sym, $lnum, $idnum) = ( $1, $2, $3, $4 );
    #print STDERR "==> New symbol $sym\n";

    # added to taglist.vim :
    #  " ocaml language
    #  let s:tlist_def_ocaml_settings = 'ocaml;m:module;t:type;d:definition'
    #
    # get type from 1st match in pattern, if type "and"
    # use the type from last entry
    $pat =~ m/\b(.+)\b\s+\b$sym\b/;
    $rawtype = $1;
   
    switch (  $rawtype ) {
      case "and" { $type = $lasttype ; }
      case "let" { $type = "d" ; }
      case "type" { $type = "t" ; }
      case "module" { $type = "m" ; }
    }
   
    # target:
    # (symbol)\t/(pattern)/\t(file)\t(type)\tline:(line)
    print $sym.$sep.$fname.$sep.'/^'.$pat.'/;"'.$sep.$type.$sep.'line:'.$lnum."\n" if $pat;

    $lasttype = $type ;
  }
}


simple backup script

This script provides quick-n-dirty timestamped backups while preserving file extensions.

It echos to the console it's operations.

May be used on files or directories.

EXAMPLE
$ bk maxentropy.c
`maxentropy.c' -> `.backup/maxentropy~20080428d22h51.c'

It automatically creates the '.backup' directory and timestamps the filename and copies it there (minute granularity).

Here is the script.

$ cat `which bk`
#!/bin/bash

TARGETDIR=".backup"
SEP="~"
STAMP=`date +%Y%m%dd%Hh%M`

mkdir -p "${TARGETDIR}"
for f in "$@" ; do
  #cp -av "$f" backup/"$f"-`date +%Y%m%dd%Hh%M`

  # using 
  #     DIR="${FULLNAME%/*}"
  #     FILE="${FULLNAME##*/}"
  #     MAXBASE="${FILE%.*}"
  #     MINBASE="${FILE%%.*}"
  #     MAXSUF="${FILE#*.}"
  #     MINSUF="${FILE##*.}"
  #     echo $DIR $FILE $MAXBASE $MINBASE $MAXSUF $MINSUF
  #     /dir base.suf1.suf2 base.suf1 base suf1.suf2 suf2

  f="${f%/}"           #; echo f:$f;
  DIR="${f%/*}"        #; echo DI:$DIR;
  FILE="${f##*/}"      #; echo FI:$FILE;
  MAXBASE="${FILE%.*}" #; echo MB:$MAXBASE;

  # if the file has no extension
  if [ "$FILE" = "${FILE/*./}" ] ;
    then MINSUF="" ;  # filename in MAXBASE already
    else MINSUF="${FILE/*./.}";
  fi ;
  MINSUF="${FILE/*./.}";
  #echo SUF: $MINSUF;
  
  # file name starts with a ".", has no other "." in it
  if [ "$FILE" = "$MINSUF" ] ;
    then
      MAXBASE="${MINSUF}"
      MINSUF=""
  fi
  
  TARGET="${TARGETDIR}/${MAXBASE}${SEP}${STAMP}${MINSUF}"
  #echo $TARGET
  
  # maybe should add "-n" option to show what'll happen
  cp -av "$f" "$TARGET"
done


script to fix modifier keys on ubuntu debian gnu linux 2.6.24-16 when using vmware workstation 6.0.3 build-80004

$ cat `which fix-mod-keys`
#!/bin/sh
/usr/bin/xmodmap - << fixme
clear shift
add shift = Shift_L Shift_R
clear lock
add lock = Caps_Lock
clear control
add control = Control_L Control_R
clear mod1
add mod1 = Alt_L Alt_R
clear mod2
add mod2 = Num_Lock
clear mod3
clear mod4
add mod4 = Super_L Super_R
clear mod5
add mod5 = Scroll_Lock
fixme
xset r on
xset m 3.5 4
xset b off
xset s off

2008-04-20

HTML source code publishing with syntax highlighting using vim

Load your file.

Turn syntax highlighting on.

(Optionally, try :let html_use_css=1)

:TOhtml

:wq

LCD screen brightness control program for Linux

This program cycles through the screen brightness levels and shows the current value using an OSD (on screen display) field.

I map this to Meta-B since the native one only goes to "brightest" and
there is no other way to adjust it.

This has been used on linux kernels 2.6.22 and 2.6.24.




#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include <unistd.h>

#include <time.h>

//

// can use debian package for libxosd-dev

//  

// compile with:

//   gcc -Wall -DXOSD `xosd-config --cflags --libs` brightness_lcd.c -o brightness_lcd

//

// set sticky:

//   sudo chown root:root brightness_lcd

//   sudo chmod u+s brightness_lcd

//

//  execute:

//   ./brightness_lcd

//



#ifdef XOSD

#include <xosd.h>

#endif //XOSD









#define LEVELS "levels:"

#define CURRENT "current:"





static int lcmp(const void *a, const void *b)

{

  long x = *((long *)a), y = *((long *)b);

  return (x > y) - (x < y);

}



void help(int argc, char *argv[])

{

  // ======= args =========

  if (argc>1 && !strcmp(argv[1], "-h"))  {

    fprintf(stderr,

      "By default, move to next brightness level\n"

      "Options are device and/or value, e.g.:\n"

      "\t%s 37 /proc/acpi/video/VGA/LCD/brightness\n" 

      "\t%s 37\n",

      argv[0], argv[0] );

    exit(-1);

  }

}



int main(int argc, char *argv[])

{

  FILE *fp=NULL;

  char *devname = "/proc/acpi/video/VGA/LCD/brightness";

  int NS=1024;

  char s[NS];

  int NL = 256;

  long levels[NL], nlevels = 0;

  long current = 0;

  int i, j, c=0;

  struct timespec ts = {0, 1000000*250};



#ifdef XOSD

  xosd *osd;

#endif //XOSD





  help(argc, argv);

  

  // ======= read =========

  if (argc>2)

    devname = argv[2];



  if ( !(fp = fopen(devname, "r")) ) {

    fprintf(stderr,

      "Could not open device '%s' for reading.\n",

      devname);

    return -1;

  }



  while (fgets(s, NS, fp)) {

    if ( !strncmp(LEVELS, s, strlen(LEVELS)) ) {

      char *sp, *sp1 = s + strlen(LEVELS);

      errno = 0;

      do {

        sp = sp1;

        levels[nlevels++] = strtol(sp, &sp1, 10);

      } while (sp!=sp1) ;

      nlevels--;

      // printf("levels: ... %ld %ld %ld x%ld\n",

      //         levels[8], levels[9], levels[10], nlevels);

    }

    else if ( !strncmp(CURRENT, s, strlen(CURRENT)) ) {

      char *sp = s + strlen(CURRENT);

      current = strtol(sp, &sp, 10);

      // printf("current: %ld\n", current);

    }

  }



  if (fp)

    fclose(fp);



  // ======= sort and remove duplicates =========

  qsort(levels, nlevels, sizeof(long), lcmp);

  for (i=1; i<nlevels; i++) {

    if (levels[i]==levels[i-1]) {

      for (j=i+1; j<nlevels; j++) {

        levels[j-1] = levels[j];

      }

      nlevels--;

    }

  }

  

  // for (i=0; i<nlevels; i++) {

  //   printf("  level: %ld\n", levels[i]);

  // }



  // ======= find current, go to next =========

  // printf("current: %ld\n", current);

  for (i=0; i<nlevels; i++) {

    if (current<=levels[i]) {

      c = i;

      break;

    }

  }



  c++;

  c%=nlevels;

  // printf("c: %d, level: %ld, x%ld\n", c, levels[c], nlevels);



  if (argc>1)

    sprintf(s, "%s", argv[1]);

  else {

    sprintf(s, "%ld", levels[c]);

  }



  // ======= write =========

  printf("Writing '%s' to '%s'\n", s, devname);

  

  if ( !(fp = fopen(devname, "w")) ) {

    fprintf(stderr, "Could not open device '%s' for writing.\n", devname);

    return -1;

  }



  fwrite(s, 1, strlen(s), fp);



  if (fp)

    fclose(fp);



#ifdef XOSD

  osd = xosd_create (1);

  if (osd == NULL) {

    perror ("Could not create \"osd\"");

    exit (1);

  }



  xosd_set_font (osd, "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-iso8859-1");

  xosd_set_pos (osd, XOSD_bottom);

  xosd_set_align (osd, XOSD_left);

  xosd_display (osd, 0, XOSD_string, s);

  xosd_set_shadow_offset (osd, 2);

  //sleep (1);

  nanosleep (&ts, NULL);

  xosd_destroy (osd);

#endif //XOSD



  return 0;

}





Who is guilty?


Claims LOGIC is incompatible with having a LIFE.


Claims LIFE is LOGICAL.

Who is guilty of committing fallacy?

NetBeans 6.01 Visual Mobility Designer plugin fails to create new VMD files

Under investigation... using Debian.

2008-04-19

Netbeans errors with toplinklib

Platform: Debian

Problem:

Netbeans 6 has integration with Sun Wireless Toolkit via the SWT plugin. Installing this plugin requires specifying a directory with a java(1) executable, but specifying /usr/lib/jvm/java-6-openjdk/jre/bin/ causes the install to fail since a jar(1) executable isn't in that directory.

However, /usr/lib/jvm/java-6-sun/bin/ works since this directory has symlinks to all important java6 related binaries. The /usr/bin directory works as well if one runs "sudo update-java-alternatives --list" then "sudo update-java-alternatives --set java-6-sun" (or use java-6-openjdk).

Anyway, half the plugins didn't work, and wiping out the ~/.netbeans* directories didn't fix anything.

Netbeans keeps complaining about needing org-netbeans-modules-j2ee-toplinklib although it is present and seems fine.

Solution:

Go to the plugins in Netbean, select Installed Plugins, select "Java Persistence" and uninstall. Then re-install this upon reloading Netbeans.

Now all the broken plugins start working again.