#!/usr/bin/perl # Filename: rox.panelput.pl # Version: 0.2 # Livense: GPLv3 # # Description: # simple perl script to assist in adding/removing device icons for ROX pinboard # read more on: http://www.void.gr/kargig/blog/2009/06/23/using-halevt-to-automount-media-and-make-them-appear-on-rox-desktop/ # Created by George Kargiotakis kargig[at]void[dot]gr # # Changelog # v0.2 Path Corrections by Antonio SĪ±nchez # v0.1 Original version use warnings; use strict; use Env qw(HOME USER); #VARIABLES my $DEVICEFILE="$HOME/.halevt/mounted/devices"; my $DEVICEFILETEMP="$HOME/.halevt/mounted/devices_TEMP"; my $ROXFILE="$HOME/.config/rox.sourceforge.net/ROX-Filer/pb_pinboard"; my $ROXTEMP="$HOME/.config/rox.sourceforge.net/ROX-Filer/ROX-Filer/pb_pinboard_TEMP"; my $LOCKFILE="/tmp/rox-halevt-lock-$USER.tmp"; my $fail=0; my $ARG1=$ARGV[0]; my $ARG2=$ARGV[1]; my $ARG3=$ARGV[2]; my $LABEL; my $DEVPATH; my $devicecount=0; my $spot=0; my $newspot=0; my $counter=0; my $psi=0; sub roxrpc { my ($ARG1,$DEVPATH,$LABEL,$psi) = @_; my $tmpfile="/tmp/rox.tmp.$$"; open (FILEWRITE,">$tmpfile") || die "could not open $DEVICEFILE: $!\n"; flock FILEWRITE,2; print FILEWRITE " $DEVPATH 50 $psi "; flock FILEWRITE,8; close FILEWRITE; system("rox -R <$tmpfile"); unlink($tmpfile); } if ($ARG1 eq "Add") { open (FILE, $DEVICEFILE) || die "could not open $DEVICEFILE: $!\n"; my @file=; my $lines=@file; close FILE; for (@file) { if ($_ =~ /^$ARG2:/) { $fail=1; last } } if ($fail == 1) { #if device already exists, exit print "PHAIL Add!\n"; exit; } else { if ($ARG3) { $DEVPATH=$ARG2; $LABEL=$ARG3; } else { $DEVPATH=$ARG2; $LABEL=$ARG2; } for (@file) { #find count of empty lines if ($_ !~ /^$/) { $devicecount++; } } if ($devicecount < $lines) { #if there's an empty line in devicefile $counter=0; for (@file) { #find spot of first empty line $counter++; if ($_ =~ /^$/) { $spot=$counter; last; } } $newspot=$spot-1; $psi=$newspot * 100 + 50; open (FILEREAD,"<$DEVICEFILE") || die "could not open $DEVICEFILE: $!\n"; open (FILEWRITE,">$DEVICEFILETEMP") || die "could not open $DEVICEFILETEMP: $!\n"; flock FILEWRITE,2; while () { if ($. == $spot) { $_ =~ s//$DEVPATH:$LABEL/; } print FILEWRITE; } close FILEREAD; flock FILEWRITE,8; close FILEWRITE; rename($DEVICEFILETEMP,$DEVICEFILE); } else { $psi = $devicecount * 100 + 50; open (FILEWRITE,">>$DEVICEFILE") || die "could not open $DEVICEFILE: $!\n"; flock FILEWRITE,2; print FILEWRITE "$DEVPATH:$LABEL\n"; flock FILEWRITE,8; close FILEWRITE; } roxrpc($ARG1,$DEVPATH,$LABEL,$psi); } } elsif ($ARG1 eq "Remove") { open (FILE, $DEVICEFILE) || die "could not open $DEVICEFILE: $!\n"; while () { if ($_ =~ /^$ARG2:/) { $spot = $.; ($DEVPATH,$LABEL) = split(/:/); chomp($DEVPATH); chomp($LABEL); $fail=1; last } } close FILE; if ($fail == 0) { #if device does not exists, exit print "PHAIL Remove!\n"; exit; } else { open (FILEREAD,"<$DEVICEFILE") || die "could not open $DEVICEFILE: $!\n"; open (FILEWRITE,">$DEVICEFILETEMP") || die "could not open $DEVICEFILETEMP: $!\n"; flock FILEWRITE,2; while () { if ($. == $spot) { $_ =~ s/.*//; } print FILEWRITE; } close FILEREAD; flock FILEWRITE,8; close FILEWRITE; rename($DEVICEFILETEMP,$DEVICEFILE); $psi=$spot * 100 + 50; roxrpc($ARG1,$DEVPATH,$LABEL,$psi); } } elsif ($ARG1 eq "Restore") { open (FILE, $DEVICEFILE) || die "could not open $DEVICEFILE: $!\n"; while () { ($DEVPATH,$LABEL) = split(/:/); chomp($DEVPATH); chomp($LABEL); print "halevt file line=$_ DEVPATH=$DEVPATH LABEL=$LABEL\n"; open (FILEREAD,"<$ROXFILE") || die "could not open $ROXFILE: $!\n"; open (FILEWRITE,">$ROXTEMP") || die "could not open $ROXTEMP: $!\n"; flock FILEWRITE,2; while () { if ($_ !~ /$LABEL/) { print FILEWRITE; } } } close FILEREAD; flock FILEWRITE,8; close FILEWRITE; rename($ROXTEMP,$ROXFILE); close FILE; open (FILE, ">$DEVICEFILE") || die "could not open $DEVICEFILE: $!\n"; flock FILE,2; print FILE ""; flock FILE,8; close FILE; }