bump version.
[clive.git] / lib / clive / Compat.pm
blob48341e05059f56316a4e1ccf777d9a3fdc6b35cb
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009 Toni Gundogdu.
7 # This file is part of clive.
9 # clive is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation, either version 3 of the License, or (at your option)
12 # any later version.
14 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 ###########################################################################
22 package clive::Compat;
24 use warnings;
25 use strict;
27 use clive::Error qw(CLIVE_OK CLIVE_READ);
28 use clive::Util;
30 # Upgrades clive 2.0/2.1 config to 2.2+ format.
31 sub upgradeConfig {
32 require File::Spec;
33 my $path
34 = File::Spec->catfile( $ENV{HOME}, ".config", "clive", "config" );
36 require Config::Tiny;
37 print("Verify $path.\n");
38 my $c = Config::Tiny->read($path);
40 if ( !$c ) {
41 log->errn( "$path: " . Config::Tiny->errstr );
42 exit(CLIVE_READ);
45 my %opts = (
46 connect_timeout => $c->{http}->{connect_timeout},
47 connect_timeout_socks => $c->{http}->{connect_timeout_socks},
48 agent => $c->{http}->{agent},
49 proxy => $c->{http}->{proxy},
50 limit_rate => $c->{http}->{limit_rate},
51 format => $c->{output}->{format},
52 save_dir => $c->{output}->{savedir},
53 cclass => $c->{output}->{cclass},
54 filename_format => $c->{output}->{filename_format},
55 format => $c->{output}->{format},
56 cache_dump_format => $c->{output}->{show},
57 exec => $c->{commands}->{exec},
58 stream_exec => $c->{commands}->{stream},
60 # Obsolete in 2.2:
61 #progress => $c->{_}->{progress},
62 #ytuser => $c->{youtube}->{user},
63 #ytpass => $c->{youtube}->{pass },
64 #clivepass => $c->{commands}->{clivepass},
67 my $data;
68 while ( ( my $i ) = each(%opts) ) {
69 $data .= qq/--$i="$opts{$i}"\n/
70 if ( $opts{$i} );
73 if ( !$data ) {
74 my $a = clive::Util::prompt(
75 "error: Nothing to upgrade -- View file? (Y/n):");
76 system("less $path")
77 if ( $a ne "n" );
78 exit(CLIVE_OK);
81 open my $fh, ">", "$path.new"
82 or print( STDERR "error: $path.new: $!" )
83 and exit(CLIVE_READ);
85 print $fh $data;
86 close $fh;
88 my $a = clive::Util::prompt("View differences? (Y/n):");
89 system("diff -u $path $path.new | less")
90 if ( $a ne "n" );
92 $a = clive::Util::prompt("Overwrite with new config? (y/N):");
93 exit(CLIVE_OK)
94 unless ( $a eq "y" );
96 print("Backup -> $path.old\n");
97 $c->write("$path.old");
99 print("Upgrade.\n");
100 require File::Copy;
101 File::Copy::move( "$path.new", "$path" )
102 or print( STDERR "error: move: $!" )
103 and exit(CLIVE_READ);
104 print("Done.\n");
106 exit(CLIVE_OK);
111 # A wildcat did growl.