summaryrefslogtreecommitdiff
path: root/go
blob: b7da20b80dd9ed9ec7522747a091dee5b2c10a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl

use strict;
use warnings;
use Sys::Hostname;
#use Gtk2 '-init';
use Data::Dumper;
use File::Which;
use Text::Iconv;

my $iconv = Text::Iconv->new("UTF-8", "ISO8859-1");

sub read_sites
{
  my @sites;

  open SITES, $ENV{'HOME'}."/.go/sites" or return;
  my @file_contents = <SITES>;
  foreach ( @file_contents ) {
    my %site;
    next if /^#|^[[:blank:]]*$/;

    ( $site{'browser'}, $site{'profile'}, $site{'match'} ) = split;
    return unless($site{'profile'} =~ /^[\w\d.\-]+$/);
    push @sites, \%site;
  }

  return @sites;
}

my $host = hostname();
my $prefered_browser;
my $browser_command;
my $browser_profile_dir;
my @browser_arguments;
my @sites;

if($ARGV[0]) {
  if($ARGV[0] eq "clip") {
#    my $clip = Gtk2::Clipboard->get(Gtk2::Gdk::Atom -> intern("CLIPBOARD", 0));
#    print Dumper($clip);

    my @clipboard = `xclip -out`;
    my $clipboard;

    if (scalar( @clipboard ) > 1) {
      for my $i ( 0 .. scalar( @clipboard ) - 1 ) {
         print "hepp $i\n";
         $clipboard[$i] =~ s/^\+//;
         chomp($clipboard[$i]);
      }
      @browser_arguments = ( join("", @clipboard) );
    } else {
      @browser_arguments = @clipboard;
    }

  } elsif($ARGV[0] eq "incognito") {
    $ENV{'XDG_CONFIG_HOME'} = `/bin/mktemp -d`;
  } elsif($ARGV[0] eq "concat") {
    shift @ARGV;
    if($ARGV[1] eq "clip") {
      @browser_arguments = $ARGV[0].`xclip -out`;
    } else {
      $ARGV[1] = $ARGV[0].$ARGV[1];
      shift @ARGV;
      while($ARGV[1]) {
        $ARGV[1] = $ARGV[0].'+'.$ARGV[1];
        shift @ARGV;
      }
      @browser_arguments = @ARGV;
    }
  } else {
    @browser_arguments = @ARGV;
  }
}

if (which('dwb')) {
  $prefered_browser = "dwb";
} else {
  $prefered_browser = "chrome";
}

@sites = read_sites;
unless(@sites) {
  print "Failed reading ~/.go/sites.\n";
  exit(1);
}

for ( @sites ) {
  my %site = %$_;
  if(grep(/$site{'match'}/, @browser_arguments)) {
    $browser_profile_dir = $site{'profile'} unless $site{'profile'} eq "default";
    if($site{'browser'} eq "default") {
      ;
    } elsif ($site{'browser'} eq "chrome") {
      $prefered_browser = "chrome";
    } elsif ($site{'browser'} eq "chromium") {
      $prefered_browser = "chromium";
    } elsif ($site{'browser'} eq "uzbl") {
      $prefered_browser = "uzbl";
    } elsif ($site{'browser'} eq "iceweasel") {
      $prefered_browser = "iceweasel";
    }
  }
}

if($prefered_browser eq "dwb") {
  $browser_command = "dwb";
  unshift @browser_arguments, "--new-instance";
  if($browser_profile_dir) {
    unshift @browser_arguments, "--profile=".$browser_profile_dir;
  }
} elsif($prefered_browser eq "chrome") {
  $browser_command = "chrome";
  unshift @browser_arguments, "--new-window";
} elsif($prefered_browser eq "chromium") {
  $browser_command = "chromium";
  unshift @browser_arguments, "--new-window";
  if($browser_profile_dir) {
    unshift @browser_arguments, "--user-data-dir=$ENV{'HOME'}/.config/chromium/".$browser_profile_dir;
  }
} elsif($prefered_browser eq "iceweasel") {
  $browser_command = "iceweasel";
  unshift @browser_arguments, "-new-window";
} elsif($prefered_browser eq "uzbl") {
  $browser_command = "uzbl";
}

system($iconv->convert($browser_command), @browser_arguments);