blob: e30336630abc9f1fc78b154402086cab62c743c6 (
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#!/usr/bin/perl
die("No HOME set, cannot install defaults.\n")
if !$ENV{'HOME'};
die("No KODI_HOME set, cannot install defaults.\n")
if !$ENV{'KODI_HOME'};
sub get_home {
return $ENV{'HOME'} if defined $ENV{'HOME'};
}
sub get_extras {
# KODI_HOME is assumed to be always setup
return $ENV{'KODI_HOME'}."/extras/" if get_os() eq "osx";
return;
}
sub get_xbmc_home {
my $os = get_os();
my $home = get_home();
return if !defined $home;
if ( $os eq "osx" ) {
return $home."/Library/Application Support/XBMC";
}
elsif ( $os eq "linux" ) {
return $home."/.xbmc";
}
return;
}
sub get_app_home {
my $os = get_os();
my $home = get_home();
return if !defined $home;
if ( $os eq "osx" ) {
return $home."/Library/Application Support/Kodi";
}
elsif ( $os eq "linux" ) {
return $home."/.kodi";
}
return;
}
sub get_os {
if ( defined $ENV{'OSTYPE'} && $ENV{'OSTYPE'} =~ /linux/ ) {
return "linux";
}
return "osx";
}
sub get_userdata_path {
my $apphome = get_app_home();
return if !defined $apphome;
return "$apphome/userdata";
}
sub setup_sources {
my $apphome = get_app_home();
my $userdata = get_userdata_path();
my $sources = $userdata."/sources.xml";
# check whether a sources.xml already exists, and if it does back it up
if ( -f $sources ) {
print STDERR "sources.xml found at \"$sources\", backing up\n";
my $backup_sources = $sources.".".time().".xml";
`cp -f \"$sources\" \"$backup_sources\"`;
}
# construct a sources.xml string
my $sources_xml = get_sources_xml( get_default_sources() );
if ( $sources_xml ) {
open SOURCES, ">$sources" || exit;
print SOURCES get_sources_xml( get_default_sources() );
close SOURCES;
}
}
sub get_sources_xml {
my $sources = shift;
my ($sourcetype, $source);
return if !defined $sources;
my $xml = "<sources>\n";
while ( ($sourcetype, $source) = each ( %$sources ) ) {
$xml .= (" " x 4)."<$sourcetype>\n";
$xml .= (" " x 8)."<default></default>\n";
my ($name, $path);
while ( ($name, $path) = each( %{ $source } ) ) {
$xml .= (" " x 8)."<source>\n";
$xml .= (" " x 12)."<name>$name</name>\n";
if ( $path =~ /(.*)\^\^(.*)/ ) {
$xml .= (" " x 12)."<path>".$1."</path>\n";
$xml .= (" " x 12)."<thumbnail>".$2."</thumbnail>\n";
}
else {
$xml .= (" " x 12)."<path>".$path."</path>\n";
}
if ($path ne "/" ) {
$xml .= (" " x 12)."<allowsharing>true</allowsharing>\n";
}
$xml .= (" " x 8)."</source>\n";
}
$xml .= (" " x 4)."</$sourcetype>\n";
}
$xml .= "</sources>\n";
return $xml;
}
sub get_default_sources {
my $sources = {};
my $home = get_home();
my $apphome = get_app_home();
return if !defined $apphome;
$sources->{'programs'} = {};
$sources->{'video'} = {};
$sources->{'music'} = {};
$sources->{'pictures'} = {};
$sources->{'files'} = {};
if ( get_os() eq "osx" ) {
# Default sources for OS X
$sources->{'music'}->{'Music'} = $home."/Music";
$sources->{'music'}->{'Music Playlists'} = "special://musicplaylists";
$sources->{'video'}->{'Movies'} = $home."/Movies";
$sources->{'video'}->{'Video Playlists'} = "special://videoplaylists";
$sources->{'pictures'}->{'Pictures'} = $home."/Pictures";
}
elsif ( get_os() eq "linux" ) {
# Default source for Linux
for my $key ( keys %$sources ) {
$sources->{$key}->{'Media'} = "/media";
$sources->{$key}->{'Home'} = "$home";
$sources->{$key}->{'Desktop'} = "$home/Desktop";
$sources->{$key}->{'Root'} = "/";
}
}
return $sources;
}
sub first_app_run() {
my $home = get_app_home();
if ( ! -f "$home/.setup_complete" ) {
return 1;
}
return;
}
sub first_app_version_run() {
my $home = get_app_home();
if ( ! -f "$home/.setup_complete" ) {
return 1;
}
return;
}
sub needs_kodi_migration() {
my $xbmchome = get_xbmc_home();
my $apphome = get_app_home();
#check if there is an old XBMC folder to migrate
if ( -d "$xbmchome" && ! -d "$apphome" ) {
return 1;
}
return;
}
sub setup_default_app() {
my $extras = get_extras();
my $apphome = get_app_home();
my $userdata = get_userdata_path();
die("No extras :(\n")
if !defined $extras;
die("No XBMC home folder :(\n")
if !defined $apphome;
die ("No userdata folder :(\n")
if !defined $userdata;
# create userdata directory if it doesn't exist
`mkdir -p "$userdata"`;
# copy extra stuff to xbmc's userdata directory
`cp -fRv "$extras/user/" "$userdata"`;
setup_sources();
`touch -f "$apphome/.setup_complete"`;
}
sub setup_version_app() {
#TODO
}
#migration from XBMC to Kodi
sub migrate_to_kodi() {
my $xbmchome = get_xbmc_home();
my $apphome = get_app_home();
print "mv $xbmchome $apphome";
`mv "$xbmchome" "$apphome"`;
`touch -f "$apphome/.kodi_data_was_migrated"`;
}
if (needs_kodi_migration()) {
migrate_to_kodi();
}
if (first_app_run()) {
setup_default_app();
}
if (first_app_version_run()) {
setup_version_app();
}
|