summaryrefslogtreecommitdiff
path: root/setup.pl
blob: 1c15e2f992c61b24493eeeee90197e09c085988b (plain)
  1. #!/usr/bin/perl
  2. #
  3. ######################################################################
  4. # LedgerSMB Small Medium Business Accounting Software Installer
  5. # Copyright (c) 2002, Dieter Simader
  6. #
  7. # Web: http://sourceforge.net/projects/ledger-smb/
  8. #
  9. #######################################################################
  10. $| = 1;
  11. if ($ENV{HTTP_USER_AGENT}) {
  12. print "
  13. This does not work yet!
  14. use $0 from the command line";
  15. exit;
  16. }
  17. $lynx = `lynx -version`; # if LWP is not installed use lynx
  18. $gzip = `gzip -V 2>&1`; # gz decompression utility
  19. $tar = `tar --version 2>&1`; # tar archiver
  20. $latex = `latex -version`;
  21. %checkversion = ( www => 3, abacus => 4, pluto => 5, neptune => 8 );
  22. %source = (
  23. 1 => { url => "http://voxel.dl.sourceforge.net/sourceforge/sql-ledger", site => "New York, U.S.A", locale => us },
  24. 2 => { url => "http://easynews.dl.sourceforge.net/sourceforge/sql-ledger", site => "Arizona, U.S.A", locale => us },
  25. 3 => { url => "http://www.sql-ledger.com/source", site => "California, U.S.A", locale => us },
  26. 4 => { url => "http://abacus.sql-ledger.com/source", site => "Toronto, Canada", locale => ca },
  27. 5 => { url => "http://pluto.sql-ledger.com/source", site => "Edmonton, Canada", locale => ca },
  28. 6 => { url => "http://ufpr.dl.sourceforge.net/sourceforge/sql-ledger", site =>"Brazil", locale => br },
  29. 7 => { url => "http://surfnet.dl.sourceforge.net/sourceforge/sql-ledger", site => "The Netherlands", locale => nl },
  30. 8 => { url => "http://neptune.sql-ledger.com/source", site => "Ireland", locale => ie },
  31. 9 => { url => "http://kent.dl.sourceforge.net/sourceforge/sql-ledger", site => "U.K", locale => uk },
  32. 10 => { url => "http://ovh.dl.sourceforge.net/sourceforge/sql-ledger", site => "France", locale => fr },
  33. 11 => { url => "http://mesh.dl.sourceforge.net/sourceforge/sql-ledger", site => "Germany", locale => de },
  34. 12 => { url => "http://citkit.dl.sourceforge.net/sourceforge/sql-ledger", site => "Russia", locale => ru },
  35. 13 => { url => "http://optusnet.dl.sourceforge.net/sourceforge/sql-ledger", site => "Sydney, Australia", locale => au },
  36. 14 => { url => "http://nchc.dl.sourceforge.net/sourceforge/sql-ledger", site => "Taiwan", locale => tw },
  37. 15 => { url => "http://jaist.dl.sourceforge.net/sourceforge/sql-ledger", site => "Japan", locale => jp }
  38. );
  39. $userspath = "users"; # default for new installation
  40. eval { require "ledger-smb.conf"; };
  41. $filename = shift;
  42. chomp $filename;
  43. $newinstall = 1;
  44. # is LWP installed
  45. eval { require LWP::Simple; };
  46. $lwp = !($@);
  47. unless ($lwp || $lynx || $filename) {
  48. die "You must have either lynx or LWP installed or specify a filename.
  49. perl $0 <filename>\n";
  50. }
  51. if ($filename) {
  52. # extract version
  53. die "Not a LedgerSMB archive\n" if ($filename !~ /^sql-ledger/);
  54. $version = $filename;
  55. $version =~ s/sql-ledger-(\d+\.\d+\.\d+).*$/$1/;
  56. }
  57. if (-f "VERSION") {
  58. # get installed version from VERSION file
  59. open(FH, "VERSION");
  60. @a = <FH>;
  61. close(FH);
  62. $version = $a[0];
  63. chomp $version;
  64. $newinstall = !$version;
  65. if (! -f "ledger-smb.conf") {
  66. $newinstall = 1;
  67. }
  68. }
  69. $webowner = "nobody";
  70. $webgroup = "nogroup";
  71. if ($httpd = `find /etc /usr/local/etc -type f -name 'httpd.conf'`) {
  72. chomp $httpd;
  73. $webowner = `grep "^User " $httpd`;
  74. $webgroup = `grep "^Group " $httpd`;
  75. chomp $webowner;
  76. chomp $webgroup;
  77. ($null, $webowner) = split / /, $webowner;
  78. ($null, $webgroup) = split / /, $webgroup;
  79. }
  80. if ($confd = `find /etc /usr/local/etc -type d -name 'apache*/conf.d'`) {
  81. chomp $confd;
  82. }
  83. system("tput clear");
  84. if ($filename) {
  85. $install = "\ninstall $version from (f)ile\n";
  86. }
  87. # check for latest version
  88. &get_latest_version;
  89. chomp $latest_version;
  90. if (!$newinstall) {
  91. $install .= "\n(r)einstall $version\n";
  92. }
  93. if ($version && $latest_version) {
  94. if ($version lt $latest_version) {
  95. $install .= "\n(u)pgrade to $latest_version\n";
  96. }
  97. }
  98. $install .= "\n(i)nstall $latest_version (from Internet)\n" if $latest_version;
  99. $install .= "\n(d)ownload $latest_version (no installation)" unless $filename;
  100. print qq|
  101. LedgerSMB Accounting and ERP Installation
  102. $install
  103. Enter: |;
  104. $a = <STDIN>;
  105. chomp $a;
  106. exit unless $a;
  107. $a = lc $a;
  108. if ($a !~ /d/) {
  109. print qq|\nEnter httpd owner [$webowner] : |;
  110. $web = <STDIN>;
  111. chomp $web;
  112. $webowner = $web if $web;
  113. print qq|\nEnter httpd group [$webgroup] : |;
  114. $web = <STDIN>;
  115. chomp $web;
  116. $webgroup = $web if $web;
  117. }
  118. if ($a ne 'f') {
  119. system("tput clear");
  120. # choose site
  121. foreach $item (sort { $a <=> $b } keys %source) {
  122. $i++;
  123. print qq|$i. $source{$item}{site}\n|;
  124. }
  125. $site = "1";
  126. print qq|\nChoose Location [$site] : |;
  127. $b = <STDIN>;
  128. chomp $b;
  129. $site = $b if $b;
  130. }
  131. if ($a eq 'd') {
  132. &download;
  133. }
  134. if ($a =~ /(i|u)/) {
  135. &install;
  136. }
  137. if ($a eq 'r') {
  138. $latest_version = $version;
  139. &install;
  140. }
  141. if ($a eq 'f') {
  142. &install;
  143. }
  144. exit;
  145. # end main
  146. sub download {
  147. &get_source_code;
  148. }
  149. sub get_latest_version {
  150. print "Checking for latest version number .... ";
  151. if ($filename) {
  152. print "skipping, filename supplied\n";
  153. return;
  154. }
  155. if ($lwp) {
  156. foreach $source (qw(pluto www abacus neptune)) {
  157. $url = $source{$checkversion{$source}}{url};
  158. print "\n$source{$checkversion{$source}}{site} ... ";
  159. $latest_version = LWP::Simple::get("$url/latest_version");
  160. if ($latest_version) {
  161. last;
  162. } else {
  163. print "not found";
  164. }
  165. }
  166. } else {
  167. if (!$lynx) {
  168. print "\nYou must have either lynx or LWP installed";
  169. exit 1;
  170. }
  171. foreach $source (qw(pluto www abacus neptune)) {
  172. $url = $source{$checkversion{$source}}{url};
  173. print "\n$source{$checkversion{$source}}{site} ... ";
  174. $ok = `lynx -dump -head $url/latest_version`;
  175. if ($ok = ($ok =~ s/HTTP.*?200 //)) {
  176. $latest_version = `lynx -dump $url/latest_version`;
  177. last;
  178. } else {
  179. print "not found";
  180. }
  181. }
  182. die unless $ok;
  183. }
  184. if ($latest_version) {
  185. print "ok\n";
  186. 1;
  187. }
  188. }
  189. sub get_source_code {
  190. $err = 0;
  191. @order = ();
  192. push @order, $site;
  193. for (sort { $a <=> $b } keys %source) {
  194. push @order, $_;
  195. }
  196. if ($latest_version) {
  197. # download it
  198. chomp $latest_version;
  199. $latest_version = "sql-ledger-${latest_version}.tar.gz";
  200. print "\nStatus\n";
  201. print "Downloading $latest_version .... ";
  202. foreach $key (@order) {
  203. print "\n$source{$key}{site} .... ";
  204. if ($lwp) {
  205. $err = LWP::Simple::getstore("$source{$key}{url}/$latest_version", "$latest_version");
  206. $err -= 200;
  207. } else {
  208. $ok = `lynx -dump -head $source{$key}{url}/$latest_version`;
  209. $err = !($ok =~ s/HTTP.*?200 //);
  210. if (!$err) {
  211. $err = system("lynx -dump $source{$key}{url}/$latest_version > $latest_version");
  212. }
  213. }
  214. if ($err) {
  215. print "failed!";
  216. } else {
  217. last;
  218. }
  219. }
  220. } else {
  221. $err = -1;
  222. }
  223. if ($err) {
  224. die "Cannot get $latest_version";
  225. } else {
  226. print "ok!\n";
  227. }
  228. $latest_version;
  229. }
  230. sub install {
  231. if ($filename) {
  232. $latest_version = $filename;
  233. } else {
  234. $latest_version = &get_source_code;
  235. }
  236. &decompress;
  237. if ($newinstall) {
  238. open(FH, "ledger-smb.conf.default");
  239. @f = <FH>;
  240. close(FH);
  241. unless ($latex) {
  242. grep { s/^\$latex.*/\$latex = 0;/ } @f;
  243. }
  244. open(FH, ">ledger-smb.conf");
  245. print FH @f;
  246. close(FH);
  247. $alias = $absolutealias = $ENV{'PWD'};
  248. $alias =~ s/.*\///g;
  249. $httpddir = `dirname $httpd`;
  250. if ($confd) {
  251. $httpddir = $confd;
  252. }
  253. chomp $httpddir;
  254. $filename = "sql-ledger-httpd.conf";
  255. # do we have write permission?
  256. if (!open(FH, ">>$httpddir/$filename")) {
  257. open(FH, ">$filename");
  258. $norw = 1;
  259. }
  260. $directives = qq|
  261. Alias /$alias $absolutealias/
  262. <Directory $absolutealias>
  263. AllowOverride All
  264. AddHandler cgi-script .pl
  265. Options ExecCGI Includes FollowSymlinks
  266. Order Allow,Deny
  267. Allow from All
  268. </Directory>
  269. <Directory $absolutealias/users>
  270. Order Deny,Allow
  271. Deny from All
  272. </Directory>
  273. |;
  274. print FH $directives;
  275. close(FH);
  276. print qq|
  277. This is a new installation.
  278. |;
  279. if ($norw) {
  280. print qq|
  281. Webserver directives were written to $filename
  282. Copy $filename to $httpddir
  283. |;
  284. if (!$confd) {
  285. print qq| and add
  286. # SQL-Ledger
  287. Include $httpddir/$filename
  288. to $httpd
  289. |;
  290. }
  291. print qq| and restart your webserver!\n|;
  292. if (!$permset) {
  293. print qq|
  294. WARNING: permissions for templates, users, css and spool directory
  295. could not be set. Login as root and set permissions
  296. # chown -hR :$webgroup users templates css spool
  297. # chmod 771 users templates css spool
  298. |;
  299. }
  300. } else {
  301. print qq|
  302. Webserver directives were written to
  303. $httpddir/$filename
  304. |;
  305. if (!$confd) {
  306. if (!(`grep "^# SQL-Ledger" $httpd`)) {
  307. open(FH, ">>$httpd");
  308. print FH qq|
  309. # SQL-Ledger
  310. Include $httpddir/$filename
  311. |;
  312. close(FH);
  313. }
  314. }
  315. if (!$>) {
  316. # send SIGHUP to httpd
  317. if ($f = `find /var -type f -name 'httpd.pid'`) {
  318. $pid = `cat $f`;
  319. chomp $pid;
  320. if ($pid) {
  321. system("kill -s HUP $pid");
  322. }
  323. }
  324. }
  325. }
  326. }
  327. # if this is not root, check if user is part of $webgroup
  328. if ($>) {
  329. if ($permset = ($) =~ getgrnam $webgroup)) {
  330. `chown -hR :$webgroup users templates css spool`;
  331. chmod 0771, 'users', 'templates', 'css', 'spool';
  332. `chown :$webgroup ledger-smb.conf`;
  333. }
  334. } else {
  335. # root
  336. `chown -hR 0:0 *`;
  337. `chown -hR $webowner:$webgroup users templates css spool`;
  338. chmod 0771, 'users', 'templates', 'css', 'spool';
  339. `chown $webowner:$webgroup ledger-smb.conf`;
  340. }
  341. chmod 0644, 'ledger-smb.conf';
  342. unlink "ledger-smb.conf.default";
  343. &cleanup;
  344. while ($a !~ /(Y|N)/) {
  345. print qq|\nDisplay README (Y/n) : |;
  346. $a = <STDIN>;
  347. chomp $a;
  348. $a = ($a) ? uc $a : 'Y';
  349. if ($a eq 'Y') {
  350. @args = ("more", "doc/README");
  351. system(@args);
  352. }
  353. }
  354. }
  355. sub decompress {
  356. die "Error: gzip not installed\n" unless ($gzip);
  357. die "Error: tar not installed\n" unless ($tar);
  358. &create_lockfile;
  359. # ungzip and extract source code
  360. print "Decompressing $latest_version ... ";
  361. if (system("gzip -df $latest_version")) {
  362. print "Error: Could not decompress $latest_version\n";
  363. &remove_lockfile;
  364. exit;
  365. } else {
  366. print "done\n";
  367. }
  368. # strip gz from latest_version
  369. $latest_version =~ s/\.gz//;
  370. # now untar it
  371. print "Unpacking $latest_version ... ";
  372. if (system("tar -xf $latest_version")) {
  373. print "Error: Could not unpack $latest_version\n";
  374. &remove_lockfile;
  375. exit;
  376. } else {
  377. # now we have a copy in sql-ledger
  378. if (system("tar -cf $latest_version -C sql-ledger .")) {
  379. print "Error: Could not create archive for $latest_version\n";
  380. &remove_lockfile;
  381. exit;
  382. } else {
  383. if (system("tar -xf $latest_version")) {
  384. print "Error: Could not unpack $latest_version\n";
  385. &remove_lockfile;
  386. exit;
  387. } else {
  388. print "done\n";
  389. print "cleaning up ... ";
  390. `rm -rf sql-ledger`;
  391. print "done\n";
  392. }
  393. }
  394. }
  395. }
  396. sub create_lockfile {
  397. if (-d "$userspath") {
  398. open(FH, ">$userspath/nologin");
  399. close(FH);
  400. }
  401. }
  402. sub cleanup {
  403. unlink "$latest_version";
  404. unlink "$userspath/members.default" if (-f "$userspath/members.default");
  405. &remove_lockfile;
  406. }
  407. sub remove_lockfile { unlink "$userspath/nologin" if (-f "$userspath/nologin") };