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