This file is indexed.

/usr/bin/dvd-menu is in dvd-slideshow 0.8.6.1-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
#!/bin/bash
#    dvd-menu
#    Copyright 2003-2008 Scott Dylewski  <scott at dylewski.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

name='dvd-menu'
version='0.8.6-1'

echo "[dvd-menu]            dvd-menu $version"
echo "[dvd-menu]            Licensed under the GNU GPL"
echo "[dvd-menu]            Copyright 2003-2015 by Scott Dylewski"
echo "[dvd-menu]            "

## TODO:
# read in variables from ~/.dvd-menurc and local .dvd-menurc
# relative paths in xine output suggestion

## Known bugs:

## set up bash variables
LANG="POSIX"

changes () 
{
echo 'Changes:
0.8.6
	Need to fix sox errors for newer versions, "warning Option -s depreciated"
	Change ac3 encoding to ac3_fixed in ffmpeg to avoid core dump
0.8.4	
	Fix sox arguments to work with both old and new versions of sox.
	Added "unset CDPATH to full_path function.  (Thanks Bob!)
	Add explicit PAL or NTSC in .xml files for dvdauthor 0.7.0
	Add totem and VLC playback examples
0.8.2
	Change imagemagick calls with -type TrueColor to -type TrueColorMatte for latest version
	Sox v. > 13 changes -w to -2 and -e to -n (thanks Ludovic)
	Font path follows links.  Thanks Jim Potter!
	AC3 audio created with ffmpeg needs k added to bitrate
	Changed -depth 16 to -depth 8 in background gradient creation.
	Fix embedded image help text.
	Default layout will switch to two columns of buttons (no embedded image) if the button
	list is long.
	Change LANG=C to LANG=POSIX to try to fix radix problems with printf
	Add menu fix so "dvd menu" button works on dvd players (thanks will guaraldi)
0.8.0
	pre9:
	Added 6image layout option.  It can also read the title and icon from
	  the .xml file output from dvd-slideshow.  more testing is needed.
	Added many more variables to control the layout of the menu also.
	pre8:
	Added -vcd and -svcd modes.  Works!  tested -vcd a little.
	Audio automatically fades in/out for 2 seconds
	Reads variables correctly from ~/.dvd-slideshow/dvd-slideshowrc	
	Updated variables.  See dvd-slideshowrc example file
	Better control of fonts and variables.
	Deletes temp directory when finished.
	-nocleanup works
	Reads -theme files
	Added -fadein and -fadeout for menus.
	Remove seq calls for compatibility with FreeBSD (Thanks David Mulcahy)
	Remove wc --chars calls for compatibility with FreeBSD
0.2	Initial release'
}

help ()
{
echo "dvd-menu is part of dvd-slideshow $version"
echo "`basename $0`"
echo "http://freshmeat.net/dvdslideshow/"
echo 'Copyright 2003 Scott Dylewski <scott at dylewski.com>
 
Usage: 
  dvd-menu -t <titleset1> ... -t <titlesetN>
  -f <xmlfile1> ... -f <xmlfileN> [-o output_directory]
  [-n "menu title"] [-e <embed image>] [-L]
  [-b <background jpeg>] [ -a <menu audio file> ] [-p]
  [-c] [-D] [-W] [-mp2] [-iso] [-w] [-theme <themefile>]  [-nomenu] 

Description: 
	Creates a simple vmgm menu with buttons that
	link to the mpeg files in the specified 
	dvdauthor-compatible xml files. 
	
Options: 
 -t <titleset1> -t <titleset2> ... -t <titlesetN>
	Name of each title.  Each string will be used for 
	the button menu corresponding to each xmlfile.
	This sets the order of the menu buttons!

 -f <xmlfile1> <xmlfile2> ...
	Name of each titleset xml file.  You can use
	dvd_slideshow to generate one for each slideshow
	or you can create your own for a simple mpeg2
	file.

 [-o <output directory>]
	Directory where the output dvd file system resides

 [-n "dvd_title"]
        This string will be placed on the top of the
        menu page.

 [-b <background jpeg>]
	Image to put in the background of the menu.

 [-e <embedded image>]
	Embed a jpeg image in the middle of the screen.
	The image shows up as a thumnail to the left of the
	menu buttons.
	Only one image is allowed.

 [-a <audiofile>]
	Audio file to use in the background of the
	menu.  The full audio file will keep looping.
	Supports mp3, ogg, or wav.

 [-mp2]
        Use MP2 audio by default.  AC3 seems to be more stable
	in hardware players, but requires ffmpeg.

 [-p]
        Use PAL format instead of NTSC (untested initial support)

 [-c]
        Use continuous method.  Each video plays one after another.

 [-W]
        Do not generate 50% white background behind menu text.

 [-w]
        Use widescreen mode (16:9) instead of default 4:3 aspect ratio.

 [-L]
        Use low quality mode for compatibility with the same option
	in dvd-slideshow.  This renders half-resolution menus.

 [-D]
        Do not call dvdauthor at the end.  Useful when you want
	to edit your own xml file and pass it to dvdauthor by hand.

 [-theme themename]
	Use the given theme when setting variables/colors/etc.  Themes are
	installed in /opt/dvd-slideshow/themes or in a local directory
	~/.dvd-slideshow/themes
 
 [-vcd]|[-svcd]  [ALPHA!]
        Create vcd .cue and .bin files (requires vcdimager)

 [-iso]
        Create iso image with genisoimage when finished.

 [-nomenu]
        Do not create a menu.  Useful for just finishing the 
	dvdauthor xml file from one video that you want to just
	start playing when the user puts it into a dvd player.

 [-nocleanup]
        Save files in temp directory for debugging.

 -h or -help 
   Prints this help. 

 -v or -version 
   Prints this help. 

Requires:
	dvdauthor 0.6.11
	sox >= 14.0.0
	mjpegtools  (mp2enc, mpeg2enc, ppmtoy4m)
	ImageMagick (convert)
	ffmpeg

Variables:
	dvd-menu will read the following variables out of your ~/.dvd-slideshow/dvd-slideshowrc file.
	The default values are shown

	menu_bg_color='steelblue'  # imagemagick colors or a hex #rrggbb value.
	menu_title_font_size=40
	menu_title_white_height=125
	menu_title_location_x=80
	menu_title_location_y=50
	menu_panel_height=28  # height of each button.  Default is slightly bigger than font size
	menu_panel_width=400  # goes all the way to the right of the screen
	menu_panel_location_x=420
	menu_panel_location_y=175  # distance of the top of the first menu item to the top of the screen
	menu_button_font_size=21
	menu_title_justify=left
	mpeg_encodr='ffmpeg'   # or mpeg2enc
 '
}

if [ $# -lt 1 ]; then
	help
	exit 1
fi

# [-button <button image.jpg>]
#	Use image.jpg for a highlight image button.
#	The text from -t <titleset> will be placed underneath.
#	Pass one -b <image.jpg> for each titleset xml file.
#


##################################################################
## Functions:

myecho ()
{
	## use this version of echo to write to screen and to the logfile:
	echo "$*"
	echo "$*" >> "$logfile"
}

logecho ()
{
	## use this version of echo to write to the logfile:
	echo "$*" >> "$logfile"
}

myechon ()
{
	## use this version of echo to write to screen and to the logfile:
	echo -n "$*"
	echo -n "$*" >> "$logfile"
}


#check_rm () # check_rm checks to see if the file exists before it's deleted:
#{
#	if [ -f "${1}" ] ; then
#		rm "${1}"
#	fi
#}

cleanup ()
{
if [ "$nocleanup" -eq 0 ] ; then
	## clean up temporary files
	myecho "[dvd-menu] cleanup..."
	rm -f "$tmpdir/slideshow_background.ppm"
#	rm -f "$tmpdir/silence.mp2"
	rm -f "$tmpdir/title_background_mask.png"
	rm -f "$tmpdir/button_background_mask.png"
	rm -f "$tmpdir/button_text.png"
	rm -f "$tmpdir/button_mask.png"
	rm -f "$tmpdir/button_mask_trans.png"
	rm -f "$tmpdir/menu.ppm"
	rm -f "$tmpdir/fadein.mpg"
	rm -f "$tmpdir/bg_fade.ppm"
	rm -f "$tmpdir/fadeout.mpg"
	rm -f "$tmpdir/menu.mpg"
	rm -f "$tmpdir/menu.spumux"
	rm -f "$tmpdir/menu_mask.png"
	rm -f "$tmpdir/menu_t.vob"
	rm -f "$tmpdir/menu_audio.wav"
	rm -f "$tmpdir/fade_audio.wav"
	rm -f "$tmpdir/fade_audio.ac3"
	rm -f "$tmpdir/fade_audio.mp2"
	rm -f "$tmpdir/audio.mp2"
	rm -f "$tmpdir/audio.wav"
	rm -f "$tmpdir/audio.ac3"
	rm -f "$tmpdir/embed.jpg"
	rm -f "$tmpdir/dvd_title.png"
	rm -f "$tmpdir/titles_vmgm.xml"
	rm -f "$tmpdir/embed.png"
	rm -f "$tmpdir/black.ppm"
	[ -f "$tmpdir"/fade_0001.ppm ] && rm "$tmpdir"/fade_*.ppm
        k=0
        dk=0
        for file in "${title[@]}"; do
                [ $k -lt 10 ] && dk="0$k" || dk=$k
#              	echo "k=$k  dk=$dk"
                rm -f "$tmpdir"/title_"$dk"_mask.png
                rm -f "$tmpdir"/title_"$k"_mask.png
                rm -f "$tmpdir/title_$dk.ppm"
                rm -f "$tmpdir/title_$k.ppm"
                let k=$k+1
        done
	rmdir "$tmpdir"
fi
}

forcequit () ## function gets run when we have some sort of forcequit...
{
	## clean up temporary files
	cleanup
	exit 1
}

trap 'forcequit' INT
trap 'forcequit' KILL
trap 'forcequit' TERM

## check for the necessary programs:
checkforprog ()
{
        it=`which $1`
        if [ -z "$it" ] ; then
                myecho "[dvd-menu] ERROR:  $1 not found! "
                myecho "[dvd-menu] Check the dependencies and make sure everything is installed."
                exit 1
        fi
}

rpmversion ()
## don't use this since most machines don't have rpm!
{
        if [ -z `which rpm 2> /dev/null` ] ; then
                ver=''
        else
                ver=`rpm -q $1`
        fi
        if [ `echo $ver | awk -F- '{print $1}'` == "$1" ] ; then
                # rpm returned version of program
                version=`echo $ver | awk -F- '{print $2}'`
#                myecho "[dvd-menu] Found $1 version $version"
        else
                # no rpm, try other methods
                version=0  # no version found (yet)
#                myecho "[dvd-menu] Found $1"
        fi
        echo "$version"  # returns 0 if no version found, but executable exists
}

hms ()
{
        ## pass a number in hundreths of seconds and get back a
        ## time code of the form HR:MM:SS:HU
        hours=$(( $1 / 360000 ))
        it=$(( $1 - $hours * 360000 ))
        minutes=$(( $it / 6000 ))
        it=$(( $1 - $minutes * 6000 ))
        seconds=$(( $it / 100 ))
        hundreths=$(( $it - $seconds * 100 ))
        it="$hours:$minutes:$seconds.$hundreths"
        echo "${it}"
}

white_title_mask ()
{
	convert -size "$dvd_width"x600 -quality 100 xc:transparent -depth 8 -fill white -draw "Rectangle 0,0,$dvd_width,$title_white_height" -background none -blur 0x4 -crop $resolution-0-0 "$1"
}

white_button_mask ()
{
	# white_button_mask outfile
#	convert -size "$dvd_width"x$menuh -quality 100 xc:transparent -depth 8 -fill white -draw "roundRectangle $menu_panel_location_x,$menu_panel_location_y,$menuright,$menubottom $radius,$radius" -draw "Rectangle 0,0,$dvd_width,$title_white_height" -crop $resolution-0-0 "$1"
	convert -size "$dvd_width"x$menuh -quality 100 xc:transparent -depth 8 -fill white -draw "roundRectangle $menu_panel_location_x,$menu_panel_location_y,$menuright,$menubottom $radius,$radius" -background none -blur 0x4 -crop $resolution-0-0 "$1"
}

title_text ()
{
	# title_text outfile
	echo "[dvd-menu] Creating title: $dvd_title"
	[ "$title_justify" == 'left' ] && title_justify=NorthWest
	[ "$title_justify" == 'right' ] && title_justify=NorthEast
	[ "$title_justify" == 'center' ] && title_justify=North
	echo "[dvd-menu] title justified $title_justify"
	convert -depth 8 -size $resolution xc:transparent -fill "$title_font_color" -pointsize $title_font_size -gravity $title_justify -font "$title_font" -draw "text $title_location_x,$title_location_y \"${dvd_title}\"" "$1" 
}

imagewidth ()  # IMAGEWIDTH
{
        it="`identify -format %w "$1"`"
        it="$(( $it * $sq_pixel_multiplier / 1000 ))"
        echo "$it"
}

imagewidth_sq ()  # IMAGEWIDTH_SQ
{
        it="`identify -format %w "$1"`"
        echo "$it"
}


imageheight ()
{
        it="`identify -format %h "$1"`"
        echo "$it"
}


checkforautocrop ()
{
	if [ "$autocrop" == 1 ] ; then
		# figure out whether to autocrop the image or not
	        image_width=`imagewidth "$1"`
	        image_height=`imageheight "$1"`
	        ratio="$(( 100* $image_width / $image_height ))"
	        out_ratio="$(( 100* $dvd_width / $dvd_height ))"
		do_autocrop_w=0 ; do_autocrop_h=0
		if [ "$ratio" -lt "$(( $out_ratio ))" ] ; then
			do_autocrop_h=1 # image too wide, crop height
		elif [ "$ratio" -gt "$(( $out_ratio ))" ] ; then
			do_autocrop_w=1 # image too tall, crop width
		fi
		[ $debug -ge 2 ] && myecho "[dvd-menu:checkforautocrop] image_width=$image_width image_height=$image_height ratio=$ratio out_ratio=$out_ratio"
		[ $debug -ge 2 ] && myecho "[dvd-menu:checkforautocrop] do_autocrop_w=$do_autocrop_w  do_autocrop_h=$do_autocrop_h"
	else
		do_autocrop_h=0 ; do_autocrop_w=0
	fi
}

background ()
{
		## input is:  $1 = effect/image
		# output is written to "$tmpdir"/slideshow_background.ppm 
		bg="$1"
		if [ -z "$2" ] ; then
			local outfile_bg="$tmpdir"/slideshow_background.ppm
		else
			local outfile_bg="$2"
		fi
		if [ -f "$bg" ] ; then # if effect is a background file
			myecho "[dvd-menu] Creating background image $( truncate_filename "$bg")"
			checkforautocrop "$bg"
                       if [ "$do_autocrop_w" -eq 1 ]; then   
                               # autocrop background image width (width too large)
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize x"$dvd_height" \
                               -gravity center -crop "$dvd_width"x"$dvd_height"'+0!+0!' -type TrueColorMatte -depth 8 "$outfile_bg"
                       elif [ "$do_autocrop_h" -eq 1 ]; then
                               # autocrop background image height (height too large)
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize "$dvd_width"x \
                               -gravity center -crop "$dvd_width"x"$dvd_height"'+0!+0!'  -type TrueColorMatte -depth 8 "$outfile_bg"
                       else
echo "no autocrop background"
                               #don't autorop
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize x"$dvd_height" -bordercolor black -border "$dvd_width"x240 \
                               -gravity center -crop "$dvd_width"x"$dvd_height"'+0!+0!' -type TrueColorMatte -depth 8 "$outfile_bg"
                       fi
			bgfile="$bg"
		elif [ "$bg" == 'black' ] ; then  # I guess we could add other types of backgrounds here!
			## use plain black background with no picture
			myecho "[dvd-menu] Creating black background"
			convert -size "$dvd_width"'x'"$dvd_height" xc:black -type TrueColorMatte -depth 8 "$outfile_bg"
			bgfile="black"
		elif [ "$bg" == 'white' ] ; then  # I guess we could add other types of backgrounds here!
			## use plain white background with no picture
			myecho "[dvd-menu] Creating white background"
			convert -size "$dvd_width"'x'"$dvd_height" xc:white -type TrueColorMatte -depth 8 "$outfile_bg"
			bgfile="white"
		elif [ "${bg:0:1}" == '#' ] ; then  # user passed a #RRGGBB hex color
			myecho "[dvd-menu] Creating $bg color background"
			convert -size "$dvd_width"'x'"$dvd_height" xc:"$bg" -type TrueColorMatte -depth 8 "$outfile_bg"
			bgfile="hex"
		fi
}

set_variables ()
{
        ## check for config variables:
        config1=`echo "$1" | cut -d= -f1 | tr -d [:blank:]`
#        config2=`echo "$1" | awk -F' #' '{print $2}' | cut -d= -f2 | tr -d '\047' | tr -d '\042' | tr -d [:blank:]`
        config2=`echo "$1" | tr '\t' ' ' | cut -d= -f2 | awk -F' #' '{print $1}' | tr -d '\047' | tr -d '\042'`
        [ -n "$2" -a "$2" == 1 ] && noecho=1 || noecho=0
        ## check for global configuration variables:
        case "$config1" in
                debug) debug="$config2" ; [ "$noecho" -eq 0 ] && echo "debug=$debug" ;;
                pal) pal="$config2" ; [ "$noecho" -eq 0 ] && echo "pal=$pal" ;;
                ac3) ac3="$config2" ; [ "$noecho" -eq 0 ] && echo "ac3=$ac3" ;;
                mpeg_encoder) mpeg_encoder="$config2" ; [ "$noecho" -eq 0 ] && echo "mpeg_encoder=$mpeg_encoder" ;;
# menus always autocrop?                 autocrop) autocrop="$config2" ; [ "$noecho" -eq 0 ] && echo "autocrop=$autocrop" ;;
                font) font="$config2" ; [ "$noecho" -eq 0 ] && echo "font=$font" ;;  #global font
                menu_font) font="$config2" ; [ "$noecho" -eq 0 ] && echo "font=$font" ;;  #global font
                # title
                menu_background) bgfile="$config2" ; [ "$noecho" -eq 0 ] && echo "bgfile=$bgfile" ;;
                menu_bg_color) bg_color="$config2" ; [ "$noecho" -eq 0 ] && echo "bg_color=$bg_color" ;;
                menu_panel_height) panel_height="$config2" ; [ "$noecho" -eq 0 ] && echo "panel_height=$panel_height" ;;
                menu_panel_width) panel_width="$config2" ; [ "$noecho" -eq 0 ] && echo "panel_width=$panel_width" ;;
                menu_panel_location_x) menu_panel_location_x="$config2" ; [ "$noecho" -eq 0 ] && echo "menu_panel_location_x=$menu_panel_location_x" ;;
                menu_panel_location_y) menu_panel_location_y="$config2" ; [ "$noecho" -eq 0 ] && echo "menu_panel_location_y=$menu_panel_location_y" ;;
                menu_button_font) button_font="$config2" ; [ "$noecho" -eq 0 ] && echo "button_font=$button_font" ;;
                menu_button_font_size) button_font_size="$config2" ; [ "$noecho" -eq 0 ] && echo "button_font_size=$button_font_size" ;;
                menu_button_font_color) button_font_color="$config2" ; [ "$noecho" -eq 0 ] && echo "button_font_color=$button_font_color" ;;
                menu_button_hilight_color) button_hilight_color="$config2" ; [ "$noecho" -eq 0 ] && echo "button_hilight_color=$button_hilight_color" ;;
                menu_title_font) title_font="$config2" ; [ "$noecho" -eq 0 ] && echo "title_font=$title_font" ;;
                menu_title_font_size) title_font_size="$config2" ; [ "$noecho" -eq 0 ] && echo "title_font_size=$title_font_size" ;;
                menu_title_font_color) title_font_color="$config2" ; [ "$noecho" -eq 0 ] && echo "title_font_color=$title_font_color" ;;
                menu_title_white_height) title_white_height="$config2" ; [ "$noecho" -eq 0 ] && echo "title_white_height=$title_white_height" ;;
                menu_title_location_x) title_location_x="$config2" ; [ "$noecho" -eq 0 ] && echo "title_location_x=$title_location_x" ;;
                menu_title_location_y) title_location_y="$config2" ; [ "$noecho" -eq 0 ] && echo "title_location_y=$title_location_y" ;;
                menu_title_justify) title_justify="$config2" ; [ "$noecho" -eq 0 ] && echo "title_justify=$title_justify" ;;
		theme) theme="$config2" ; [ "$noecho" -eq 0 ] && echo "theme=$theme" ;;  
		theme_designed_for) theme_designed_for="$config2" ; [ "$noecho" -eq 0 ] && echo "theme_designed_for=$theme_designed_for" ;;
                menu_type) menu_type="$config2" ; [ "$noecho" -eq 0 ] && echo "menu_type=$menu_type" ;;
                menu_audio) audiofile="$config2" ; [ "$noecho" -eq 0 ] && echo "audiofile=$audiofile" ;;
                menu_continuous_play) continuous="$config2"; continuous_method="$config2" ; [ "$noecho" -eq 0 ] && echo "continuous=$continuous" ;;
                menu_fadein) fadein="$config2" ; [ "$noecho" -eq 0 ] && echo "menu_fadein=$menu_fadein" ;;
                menu_fadeout) fadeout="$config2" ; [ "$noecho" -eq 0 ] && echo "menu_fadeout=$menu_fadeout" ;;
                menu_fade_to_image) fade_to_image="$config2" ; [ "$noecho" -eq 0 ] && echo "fade_to_image=$fade_to_image" ;;
                menu_panel_mask) panel_mask="$config2" ; [ "$noecho" -eq 0 ] && echo "panel_mask=$panel_mask" ;;


        esac
}

set_theme ()
{
        ## check for config variables: only specified once per menu
        config=`echo "$1" | cut -d= -f1 | tr -d [:blank:]`
        config2=`echo "$1" | tr '\t' ' ' | tr -d '\047' | tr -d '\042' | cut -d= -f2 | awk -F' #' '{print $1}' | tr -d [:blank:]`
#       config2=`echo "$1" | cut -d= -f2 | awk -F' #' '{print $1}' | tr -d [:blank:]`
        [ -n "$2" -a "$2" == 1 ] && local noecho=1 || local noecho=0
        ## check for global configuration variables:
        case "$config" in
                theme) theme="$config2" ; [ "$noecho" -eq 0 ] && echo "$theme" ;;  #global font
        esac
}


read_theme ()
# read_theme $theme
{
local theme=$1
[ -n "$2" -a "$2" == 1 ] && local noecho=1 || local noecho=0
## read in theme file if it exists:
if [ -n "$theme" ] ; then
        # check if directory
        if [ -d "$theme" ] ; then
                themedir="$theme"
                themefile="`ls -1 "$theme"/*.theme 2> /dev/null | tail -n 1`"
                [ "$noecho" == '0' ] && myecho "[dvd-menu] Using theme file to $(truncate_filename "$themefile")"
        elif [ -f "$theme" ] ; then
                themefile="`full_path "$theme"`"
                themedir="`dirname "$theme"`"
                [ "$noecho" == '0' ] && myecho "[dvd-menu] Using theme file to $(truncate_filename "$themefile")"
                [ "$noecho" == '0' ] && myecho "[dvd-menu] Using theme file $theme"
        else
                # check in default theme directory:
                if [ -d "$themedir"/"$theme" ] ; then
                        themedir="$themedir"/"$theme"
                        themefile="`ls -1 "$themedir"/*.theme 2> /dev/null | tail -n 1`"
                	[ "$noecho" == '0' ] && myecho "[dvd-menu] Using theme file to $(truncate_filename "$themefile")"
                # check in local theme directory:
                elif [ -d "$local_themedir"/"$theme" ] ; then
                        themedir="$local_themedir"/"$theme"
                        themefile="`ls -1 "$themedir"/*.theme 2> /dev/null | tail -n 1`"
                	[ "$noecho" == '0' ] && myecho "[dvd-menu] Using theme file to $(truncate_filename "$themefile")"
                else
                        [ "$noecho" == '0' ] && myecho "[dvd-menu] ERROR!  Bad theme name (not found)"
                        exit 1
                fi
        fi
        [ "$noecho" == '0' ] && myecho "[dvd-menu] Reading theme file..."
        while read thisline
        do
          set_variables "${thisline}" 1
          it=`set_variables "${thisline}" 0`
          if [ -n "$it" ] ; then
                [ "$noecho" == '0' ] && myecho "[dvd-menu] Set variable $it"
          fi
        done < "$themefile"
	## now, fix paths to background files and such if they were local paths:
	if [ -n "$bgfile" ] ; then
		if [ ! -f "$bgfile" ] ; then	
			bgfile2="$themedir"/"$bgfile"
			if [ -f "$bgfile2" ] ; then
				bgfile="$bgfile2"
			else
				# try hex color.
				if [ "${bgfile:0:1}" == '#' ] ; then  # user passed a #RRGGBB hex color
					bgfile="$bgfile"
				else
					echo "[dvd-menu] Error:  bad background filename in theme."
					echo "[dvd-menu] Using default background instead."
					bgfile=''
				fi
			fi
		fi
	fi
	if [ -n "$fade_to_image" ] ; then
		# add color option
		if [ ! -f "$fade_to_image" ] ; then	
			fade_to_image="$themedir"/"$fade_to_image"
			if [ ! -f "$fade_to_image" ] ; then
				echo "Error:  bad fade_to_image filename in theme."
			fi
		fi
	fi
	if [ -n "$audiofile" ] ; then
		if [ ! -f "$audiofile" ] ; then	
			audiofile="$themedir"/"$audiofile"
			if [ ! -f "$audiofile" ] ; then
				echo "Error:  bad audio filename in theme"
				audiofile=''
			fi
		fi
	fi
	if [ -n "$font" ] ; then
		if [ ! -f "$font" ] ; then	
			font="$themedir"/"$font"
			if [ ! -f "$font" ] ; then
				echo "Error:  bad font filename in theme"
			fi
		fi
	fi
	if [ -n "$button_font" ] ; then
		if [ ! -f "$button_font" ] ; then	
			button_font="$themedir"/"$button_font"
			if [ ! -f "$button_font" ] ; then
				echo "Error:  bad button_font filename in theme"
			fi
		fi
	fi
	if [ -n "$title_font" ] ; then
		if [ ! -f "$title_font" ] ; then	
			title_font="$themedir"/"$title_font"
			if [ ! -f "$title_font" ] ; then
				echo "Error:  bad title_font filename in theme"
			fi
		fi
	fi
fi
}

full_path ()   # from Shea Martin, modified by scott dylewski
{
unset CDPATH
if [ -f "$1" ]; then
        DIR="$(dirname "$1")"
        DIR=$(cd "$DIR" && pwd)
        FILE="$(basename $1)"
        RSLT=$?
        echo "$DIR/$FILE"
        return $RSLT

elif [ -d "$1" ]; then
        DIR="$1"
	FILE="$( cd "$DIR" && pwd)"
	RSLT=$?
        echo "$FILE"
	return $RSLT
else
        echo "unknown file: $1"
        return 1
fi
}

function fade ()
# fade image1.jpg image2.jpg duration[s]
{
	local image1="$1"
	local image2="$2"
	local duration="$3"  # in seconds
	local frames="$(( $frames_per_sec * $(seconds2ms $duration) / 1000 / 1000 ))"
#	echo frames="$frames duration=$duration"
#	for fr in `seq 1 1 $frames`; do  # fade from buttons to background
	for (( fr=1 ; fr<=frames ; fr++ )) ; do
		dj=`addzeros $fr`
		percent=$(( 100 * $fr / $frames ))
		(composite -compose src-over -gravity center -type TrueColorMatte -depth 8 -dissolve $percent "$image1" "$image2" "$tmpdir/fade_$dj.ppm" )
	done
	if [ "$mpeg_encoder" == 'mpeg2enc' ] ; then
        	find "$tmpdir" -name "fade*.ppm" -type f -print0 | sort -z -d | xargs -0 cat | ppmtoy4m -v 0 -n 0 -S "$subsample" -F $ppmtoy4m_frc -A $ppmtoy4m_aspect -I p | mpeg2enc $mpeg2enc_params -o "$tmpdir/fade.mpg" >> "$logfile" 2>&1
	else
        	find "$tmpdir" -name "fade*.ppm" -type f -print0 | sort -z -d | xargs -0 cat | ppmtoy4m -v 0 -n 0 -S "$subsample" -F $ppmtoy4m_frc -A $ppmtoy4m_aspect -I p | ffmpeg -f yuv4mpegpipe -i - -target $ffmpeg_target -r $framerate -an -aspect $aspect_ratio -s "$dvd_width"x"$dvd_height" -y -f mpeg2video "$tmpdir/fade.mpg" >> "$logfile" 2>&1
	fi
	find "$tmpdir" -name "fade*.ppm" -type f -print0 | xargs -0 rm
}

function seconds2ms ()
{
        ## break up the duration into the integer seconds and ms:
        out_duration=`echo $1 | awk '{ print $1 * 1000 }'`
        echo "$out_duration"
}

addzeros ()
{
                        [ $1 -lt 1000 ] && dj2="0$1" || dj2=$1
                        [ $1 -lt 100 ] && dj2="00$1" || dj2=$dj2
                        [ $1 -lt 10 ] && dj2="000$1" || dj2=$dj2
                        echo "$dj2"
}


hms ()
{
        ## pass a number in thousandths of seconds and get back a
        ## time code of the form HR:MM:SS.XXX
	if [ -z "$1" ] ; then
		myecho "[dvd-slideshow] Error in hms function"	
		function_error=1
	else
		hours=$(( $1 / 3600000 )) ; [ $hours -eq 0 ] && hours="0" 
		it=$(( $1 - $hours * 3600000 ))
		minutes=$(( $it / 60000 )) ; [ $minutes -eq 0 ] && minutes="0" 
		it=$(( $1 - $minutes * 60000 ))
		seconds=$(( $it / 1000 )) ; [ $seconds -eq 0 ] && seconds="0" 
		thousandths_out=$( printf %3.3d $(( $it - $seconds * 1000 )) );
                it="$hours:$minutes:$seconds.$thousandths_out"
                echo "${it}"
        fi
}

wavlength ()
# wavlength $file
# returns song length in ms
{
                sox "$1" $sox_nop stat 2> "$tmpdir"/trash.txt
                song_length=`cat "$tmpdir"/trash.txt | grep 'Length (seconds):' | awk -F: '{print $2}' | tr -d [:blank:]`
                song_length_seconds=`echo $song_length | awk -F. '{print $1}'`
                if [ -z "$song_length_seconds" ] ; then song_length_seconds=0 ; fi
                song_length_ms=`echo $song_length | awk -F. '{printf ("%3.3f",$0)}' | awk -F. '{print $2}'`
                ## make sure we have two decimal places?
                if [ -z "$song_length_ms" ] ; then song_length_ms=0 ; fi
                song_length=$( echo "scale=0; 1000 * $song_length_seconds + $song_length_ms" | bc )
                rm "$tmpdir"/trash.txt
                echo "$song_length"
}
 
truncate_filename ()
{
        # truncate filenames so they're not longer than 40 characters:
        local length="${#1}"
        if [ $length -gt 40 ] ; then
                local pos=$(( $length - 40 ))
                echo "...${1:$pos}"
        else
                echo "$1"
        fi
}


########################################### end of functions


###################################################################
# Default variables
# order of perference:
# program defaults --> ~/.dvd-slideshow/dvd-slideshowrc command-line options --> .txtfile settings

## setup program default variables (user configurable)
debug=0  # 1 or 0
pal=0
continuous=0 ; continuous_method=0
no_dvdauthor=0
ac3=1
mpeg_encoder='ffmpeg'  # or 'mpeg2enc'
iso=0
no_menu=0
autocrop=1
upAction=0
nocleanup=0
subtitle_code="EN"

# define some possible fonts:
font_dir="/usr/share/fonts/"
default_font1='n019004l.pfb' # helvetica bold URW fonts
default_font2='helb____.ttf' # helvetica bold truetype

## other cool colors:  cadetblue cornflowerblue midnightblue
bg_color='steelblue'  # imagemagick colors
# panel_width=400
panel_width=''
panel_height=''  # will be set later from the button_font_size
button_font_size=21
button_font_color="black"
button_hilight_color="red"
menu_panel_location_x=420
menu_panel_location_y=175  # distance of the top of the first menu item to the top of the screen
title_font_size=40
title_font_color="black"
title_white_height=125
radius=20
button_radius=10
title_location_x=80
title_location_y=50
title_justify=NorthWest

hilight_spacing_x=50
hilight_spacing_y=50
hilight_font_size=12

embed_image_width=260
embed_image_height=240

menu_type='textbar'
panel_mask=1

frames=20 	# number of frames in the menu mpg file. 
		# I guess this only really needs to be 1 frame
		# but 30 is easier to debug so we can play it.
		## don't forget that the audio is fixed at 1 second!

# soon to be user configurable:
logfilename='dvd-menu.log'
low_quality=0
high_quality=0
vcd=0 ; svcd=0
widescreen=0
themedir='/opt/dvd-slideshow/themes'  # LSB/FHS compliant:  see http://www.pathname.com/fhs/pub/fhs-2.3.html#OPTADDONAPPLICATIONSOFTWAREPACKAGES
local_themedir=~/.dvd-slideshow/themes  # local theme directory
fadein=0
fadeout=0
fade_to_image=''

# end of default  variables

#####################################################################
# read command-line options:

let n=0
let m=0
let q=0
let p=0
for arg
do
	case "$arg" in
	## config variables:
	-p) shift; commandline_pal=1 ;;
        -ac3) shift; commandline_ac3=1 ;;  # use ac3 audio
        -mp2) shift; commandline_ac3=0 ;;  # use mp2 audio
	## not config variables:
	-t) shift ; title[$n]="${1}"; let n=$n+1; shift ;;  # each title
	-f) shift ; xmlfile[$m]="$1"; let m=$m+1; shift ;;  # each titleset
	-e) shift ; embedfile[$p]="${1}"; let p=$p+1; shift ;;  # the menu hilight images
	-button) shift ; buttonfile[$q]="${1}"; let q=$q+1; shift ;;  # each button hilight images
        -u) shift; upAction="1" ;;
#        -action) shift; Action="1" ;;  # do manual button navigation later
	-o) shift; outdir="${1%%/}"; shift ;;  # dvd directory
	-b) shift; commandline_bgfile="$1"; shift ;;  # background jpeg
	-theme) shift; commandline_theme="$1"; shift ;;  # 
	-n) shift; dvd_title="$1"; shift ;; # name of DVD disk
	-c) shift; continuous=1 ; continuous_method=1 ;;
	-D) shift; no_dvdauthor=1 ;;
#	-W) shift; no_white=1 ;;
	-w) shift; widescreen=1 ;;  # not supported in dvdauthor
	-L) shift; low_quality=1 ;;
	-H) shift; high_quality=1 ;;  # not used yet...
	-M) shift; high_quality=0 ; low_quality=0 ;;  # Force normal quality
	-vcd) shift; vcd=1 ;;  # not used yet...
	-svcd) shift; svcd=1 ;;  # not used yet...
	-fadein) shift; commandline_fadein=1 ;;  # not used yet...
	-fadeout) shift; commandline_fadeout=1 ;;  # not used yet...
	-smp) shift ;;  # not used yet...
	-nomenu) shift; no_menu=1 ;;
	-nocleanup) shift; nocleanup=1 ;;
	-a) shift; audiofile="$1"; shift;; # audio file playing during menu
        -iso) shift; iso=1 ;;  # make iso image when finished
	-h) help ; exit 0 ; shift ;;
	-help) help ; exit 0 ; shift ;;
	-\?) help ; exit 0 ; shift ;;
	-v) echo "$version" ; exit 0 ; shift ;;
	-version) echo "$version" ; exit 0 ; shift ;;
	esac
done

# verify output directory exists:
if [ -z "$outdir" ] ; then
        if [ -w "`pwd`" ] ; then
                echo "[dvd-menu] Output directory not specified."
                echo "[dvd-menu] Using `pwd`"
                outdir="`pwd`"
        else
                echo '[dvd-menu] ERROR: Output directory not specified.'
                exit 1
        fi
else  # specified output directory.  make sure it exists:
	mkdir -p "$outdir"
	outdir="$( full_path "$outdir" )"
	echo "[dvd-menu] Using output directory $outdir"
fi

## dvdbaker doesn't use or need the autual xml or vob files!
#if [ -z "$xmlfile" ] && [ -n "$1" ] ; then
#	echo "using xml file $1".
#	xmlfile="$1"
#	if [ -z "$xmlfile" ] ; then
#		echo "[dvd-menu] ERROR: No xml file passed."
#		echo "[dvd-menu] use -f <filename> to pass a .xml, .vob, or .mpg file"
#		exit 1
#	fi
#fi
		
#for it in "${xmlfile[@]}"; do
#	# verify xml file exists:
#	echo "xmlfile=$it"
#	if [ -n "$it" ] && [ ! -r "$it" ] ; then
#		echo "[dvd-menu] ERROR: XML file $it does not exist."
#		exit 1
#	fi
#done

# verify that the number of buttons (-t) is equal to the number of files (-f)
#echo "xmlfiles=${#xmlfile[@]}"
#echo "xmlfiles=${xmlfile[@]}"
#if [ "${#xmlfile[@]}" -ge 1 ] ; then
#	if [ ${#xmlfile[@]} -ne ${#title[@]} ] ; then
#			echo "[dvd-menu] ERROR:  The number of buttons does not match the number of files!"
#			echo "[dvd-menu] buttons=${title[@]}"
#			echo "[dvd-menu] files=${xmlfile[@]}"
#			exit 1
#	fi
#fi

tmpdir="$outdir"/"dvd-menu_temp_$$"
if [ ! -d "$tmpdir" ] ; then
	mkdir -p "$tmpdir"
fi
dvd_dir="$outdir"/"dvd_fs"
if [ -d "$dvd_dir" ] ; then	
	## output directory already exists.  Delete old files
#	echo "[dvd-menu] Output directory already exists. "
	echo "[dvd-menu] Deleting existing AUDIO_TS and VIDEO_TS directories..."
	if [ -d "$dvd_dir/AUDIO_TS" ] ; then rm -r "$dvd_dir/AUDIO_TS" ; fi
	if [ -d "$dvd_dir/VIDEO_TS" ] ; then rm -r "$dvd_dir/VIDEO_TS" ; fi
else
	mkdir -p "$dvd_dir"
fi

## initialize log file:
logfile="$outdir"/"$logfilename"
echo "[dvd-menu] `date`" > "$logfile"
echo "[dvd-menu] dvd-menu version $version" >> "$logfile"
echo "[dvd-menu] `uname -a`" >> "$logfile"
echo "[dvd-menu] Output directory=$outdir" >> "$logfile"

############################################################# 
## Now, read in the ~/.dvd-slideshow/dvd-slideshowrc file if it exists:
if [ -f "${HOME}/.dvd-slideshow/dvd-slideshowrc" ] ; then         
	echo "[dvd-menu] Reading default variables in ${HOME}/.dvd-slideshow/dvd-slideshowrc"
        total_lines=`wc -l "${HOME}/.dvd-slideshow/dvd-slideshowrc" | awk '{print $1}'`         total_lines=$(( $total_lines + 1 ))
        line=1
        while [ $line -ne $total_lines -a $total_lines -ne 0 ];
        do
          thisline=`sed -n "$line"p "${HOME}/.dvd-slideshow/dvd-slideshowrc"`
          set_variables "${thisline}" 1
          it=`set_variables "${thisline}" 0`
          if [ -n "$it" ] ; then
                myecho "[dvd-menu] Set variable $it"           
	  fi
          line=$(( $line + 1 ))         
	done
fi


################################################################
## Now, set the variables that were passed on the command-line: 
[ -n "$commandline_pal" ] && pal=$commandline_pal
[ -n "$commandline_autocrop" ] && autocrop=$commandline_autocrop 
[ -n "$commandline_ac3" ] && ac3=$commandline_ac3 
[ -n "$commandline_debug" ] && debug=$commandline_debug
[ -n "$commandline_theme" ] && theme=$commandline_theme
[ -n "$commandline_fadein" ] && fadein=$commandline_fadein
[ -n "$commandline_fadeout" ] && fadeout=$commandline_fadeout
if [ -n "$commandline_bgfile" ] ; then
	if [ -f "$commandline_bgfile" ] ; then
		bgfile="$(full_path "$commandline_bgfile")"
	else
		bgfile="$commandline_bgfile"
		myecho "[dvd-menu] WARNING: $bgfile is not a known file"
		myecho "[dvd-menu] WARNING: If you pass a color on the command-line,"
		myecho "[dvd-menu] WARNING: you'd better know what you're doing"
		## must be a color or not exist?
	fi
fi

## now, read the theme file if it was passed on the commandline:
if [ -n "$theme" ] && [ "$theme" != 'default' ] ; then
	read_theme "$theme" 0
fi

# button_text_location=$(( $panel_height/2 + $button_font_size/2 ))
menu_button_location_y=$(( $menu_panel_location_y + $button_font_size/2 ))  # y-location of first button

if [ "$panel_mask" == "none" ] ; then
	no_white=1
elif [ "$panel_mask" == "default" ] ; then
	no_white=0
else
	no_white=0  # this will change later to allow specific panel masks...
fi	

if [ "$vcd" -eq 1 ] ; then
	# setup audio parameters
	ac3=0
	audio_bitrate=224
	audio_sample_rate=44100
	mplex_type=1
	ffmpeg_target='vcd'
elif [ "$svcd" -eq 1 ] ; then
	# setup audio parameters
	ac3=0
	audio_bitrate=224
	audio_sample_rate=44100
	mplex_type=4
	ffmpeg_target='svcd'
else
	audio_bitrate=128
	audio_sample_rate=48000
	mplex_type=8
	ffmpeg_target='dvd'
fi

if [ "$pal" -eq 1 ] ; then
        framerate='25'
        frames_per_sec=25000  # in ms
        sq_pixel_multiplier=$(( 1000 * 540 / 576 ))
	ppmtoy4m_frc='25:1'
	ppmtoy4m_aspect='59:54'
        if [ "$low_quality" -eq 1 ] || [ "$vcd" -eq 1 ] ; then
                dvd_width='352' ; dvd_height='288'
                resolution='352x288'
        elif [ "$svcd" -eq 1 ] ; then
                dvd_width='480' ; dvd_height='576'
                resolution='480x576'
        elif [ "$high_quality" -eq 1 ] ; then
                dvd_width='720' ; dvd_height='576'
                resolution='720x576'
        else
                dvd_width='720' ; dvd_height='576'
                resolution='720x576'
        fi
else  ## NTSC
        framerate='29.97'
        frames_per_sec=29970  # in ms
        sq_pixel_multiplier=$(( 1000 * 540 / 480 ))
	ppmtoy4m_frc='30000:1001'
	ppmtoy4m_aspect='10:11'
        if [ "$low_quality" -eq 1 ] || [ "$vcd" -eq 1 ] ; then
                dvd_width='352' ; dvd_height='240'
                resolution='352x240'
        elif [ "$svcd" -eq 1 ] ; then
                dvd_width='480' ; dvd_height='480'
                resolution='480x480'
        elif [ "$high_quality" -eq 1 ] ; then
                dvd_width='720' ; dvd_height='480'
                resolution='720x480'
        else
                dvd_width='720' ; dvd_height='480'
                resolution='720x480'
        fi
fi

if [ -z "$panel_width" ] ; then
	panel_width="$(( $dvd_width - $menu_panel_location_x ))"
	logecho "menu_panel_location_x=$menu_panel_location_x"
	logecho "panel_width=$panel_width"
fi
if [ -z "$panel_height" ] ; then
	panel_height="$(( $button_font_size * 125 / 100 +2 ))" # 25% larger than fontsize
fi

#######################################################3
# Summarize settings:
myecho "[dvd-menu] Final dvd filesystem is in $dvd_dir"
[ "$pal" -eq 1 ] && ntsc_or_pal="PAL" || ntsc_or_pal="NTSC"
[ -n "$VIDEO_FORMAT" ] && original_video_format="$VIDEO_FORMAT" || original_video_format=''
export VIDEO_FORMAT="$ntsc_or_pal"
[ "$ac3" -eq 1 ] && mp2_or_ac3="AC3" || mp2_or_ac3="MP2"
[ "$vcd" -eq 1 ] && myecho "[dvd-menu] Using VCD mode."
[ "$svcd" -eq 1 ] && myecho "[dvd-menu] Using SVCD mode."
myecho "[dvd-menu] Video: $ntsc_or_pal $resolution $framerate $aspect_ratio"
myecho "[dvd-menu] Audio: $mp2_or_ac3 $audio_bitrate $audio_sample_rate"
[ "$debug" -gt 0 ] && myecho "[dvd-menu] Debug=$debug"
if [ "$nocleanup" == 1 ] ; then
        myecho "[dvd-menu] Leaving all temporary files in temp directory"
	myecho "[dvd-menu] Temporary directory is $tmpdir"
fi
myecho "[dvd-menu] buttons=${#title[@]}"
#echo "[dvd-menu] audio files=$audiofile"
#echo "[dvd-menu] menu titles:" 
#echo "[dvd-menu] ${title[@]}" 

if [ -z "${title[0]}" ] ; then  # arrays start at index 0
	# no titles passed! assume user wants no menu
	no_menu=1
	echo "[dvd-menu] WARNING: No button titles passed! Not creating menu."
fi

#############################################################

if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
        aspect_ratio="4:3"
        mpeg2enc_params="-v 0 -a 2 -4 2 -2 1 -b 1150 -n n -s -f $mplex_type"
elif [ "$widescreen" -eq 1 ] ; then
        aspect_ratio="16:9"
        mpeg2enc_params="-v 0 -a 3 -q 4 -4 2 -2 1 -s -M 0 -f $mplex_type -E -N -R 2"
else
        aspect_ratio="4:3"
#       mpeg2enc_params="-v 0 -a 2 -q 4 -4 2 -2 1 -s -M 0 -f $mplex_type"
        mpeg2enc_params="-v 0 -a 2 -q 4 -4 2 -2 1 -s -M 0 -f $mplex_type -E -N -R 2"
fi

#resize_factor=`awk -vw=$dvd_width -vh=$dvd_height -var=$aspect_ratio 'BEGIN{if (ar=="4:3"){ar=4/3} else {ar=16/9};printf "%0.2f", (100/((h/w)*(ar)));exit;}'`
resize_factor=`awk -vw=$dvd_width -vh=$dvd_height -var=$aspect_ratio 'BEGIN{if (ar=="4:3"){ar=4/3} else {ar=16/9};printf "%0.2f", (100/((h/w)*(ar)));exit;}' | sed 's/,/\./g'`
sq_to_dvd_pixels="${resize_factor}x100%"

hilight_height=$(( ( $dvd_height - $hilight_spacing_y -$title_location_y - $title_font_size - 2*$hilight_spacing_y - 2*$hilight_font_size)/2 ))
hilight_width=$(( $hilight_height * $dvd_width / $dvd_height ))  # assume this aspect ratio.
hilight_start_location_x=$(( ($dvd_width - 3*$hilight_width - 2*$hilight_spacing_x )/2 ))
hilight_start_location_y=$(( $title_location_y + $title_font_size + $hilight_spacing_y ))

if [ "$low_quality" -eq 1 ] || [ "$vcd" -eq 1 ] ; then
	panel_height=$(( $panel_height / 2 ))
	panel_width=$(( $panel_width / 2 ))
	button_font_size=$(( $button_font_size / 2 ))
#	button_text_location=$(( $panel_height/2 + $button_font_size/2 ))
	menu_panel_location_x=$(( $menu_panel_location_x / 2 ))
	menu_panel_location_y=$(( $menu_panel_location_y / 2 ))  
	menu_button_location_y=$(( $menu_button_location_y / 2 ))  
	title_font_size=$(( $title_font_size / 2 ))
	title_white_height=$(( $title_white_height / 2 ))
	radius=$(( $radius / 2 ))
	button_radius=$(( $button_radius / 2 ))
	title_location_x=$(( $title_location_x / 2 ))
	title_location_y=$(( title_location_y / 2 ))
	
	embed_image_width=$(( $embed_image_width / 2 ))
	embed_image_height=$(( $embed_image_height / 2 ))

	hilight_spacing_x=$(( $hilight_spacing_x / 2 ))
	hilight_spacing_y=$(( $hilight_spacing_y / 2 ))
	hilight_font_size=$(( $hilight_font_size / 2 ))
fi

############################################################3
## Check for required programs

# ppmtoy4m
it=`which ppmtoy4m 2> /dev/null`
if [ -z "$it" ] ; then # no ppmtoy4m
               myecho "[dvd-menu] ERROR:  no mjpegtools found for audio processing"
               myecho "[dvd-menu]         You need to download and install mjpegtools."
               myecho "[dvd-menu]         http://mjpegtools.sourceforge.net"
                cleanup ; exit 0
fi
progver=`mplex 2>&1 | grep version | awk '{ print $4 }'`
logecho "[dvd-menu] Found mjpegtools version $progver"
if ppmtoy4m -S 420mpeg2 xxxxx 2>&1 | grep -q xxxxx; then
        logecho "[dvd-menu] Using mjpegtools subsampling -S 420mpeg2"
        subsample='420mpeg2'
else
        logecho "[dvd-menu] Using mjpegtools subsampling -S 420_mpeg2"
        subsample='420_mpeg2'
fi

# sox
it=`which sox 2> /dev/null`
if [ -z "$it" ] ; then # no sox
               myecho "[dvd-menu] ERROR:  no sox found for audio processing"
               myecho "[dvd-menu]         You need to download and install sox."
               myecho "[dvd-menu]         http://sox.sourceforge.net"
                cleanup ; exit 0
fi
progver=`sox -h 2>&1 | head -n 1 | awk '{ print $3 }'`
logecho "[dvd-menu] Found sox version $progver"
progver1="$( echo ${progver#v} | awk -F. '{print $1}' )"
if [ $progver1 -lt 14 ] ; then  # use old sox commands:
        sox_word='-w'
        sox_nop='-e'
	sox_int='-s' # signed integer
	soxi=0
else # use new commands ( > 14.0.0 )
#        sox_word='-2' # depreciated
	sox_word='-b 16'
	sox_int='-e signed-integer'
        sox_nop='-n'
	soxi=1
fi
logecho "[dvd-menu] using sox options $sox_word $sox_int $sox_nop"

# ImageMagick
it=`which convert 2> /dev/null`
if [ -z "$it" ] ; then # no convert
               myecho "[dvd-menu] ERROR:  no ImageMagick found for audio processing"
               myecho "[dvd-menu]         You need to download and install ImageMagick."
               myecho "[dvd-menu]         http://ImageMagick.sourceforge.net"
                cleanup ; exit 0
fi
progver=`convert -help | head -n 1 | awk '{ print $3 }'`
logecho "[dvd-menu] Found ImageMagick version $progver"

# dvdauthor
it=`which dvdauthor 2> /dev/null`
if [ -z "$it" ] ; then # no dvdauthor
               myecho "[dvd-menu] ERROR:  no dvdauthor found for audio processing"
               myecho "[dvd-menu]         You need to download and install dvdauthor."
               myecho "[dvd-menu]         http://dvdauthor.sourceforge.net"
                cleanup ; exit 0
fi
progver=`dvdauthor -h 2>&1 | head -n 1 | awk '{ print $3 }'`
logecho "[dvd-menu] Found dvdauthor version $progver"


# ffmpeg
it=`which ffmpeg 2> /dev/null`
if [ -z "$it" ] ; then
	# no ffmpeg!  use mp2 audio instead:
	myecho "[dvd-menu] Warning:  no ffmpeg found for AC3 audio encoding."
	myecho "[dvd-menu]           Using MP2 audio instead."
	myecho "[dvd-menu]           MP2 audio is less compatible with DVD player hardware."
	myecho "[dvd-menu]		  http://ffmpeg.sourceforge.net"
	ac3=0
	mpeg_encoder='mpeg2enc'
else
	# found ffmpeg
	logecho "`ffmpeg -version 2>&1`"
	## check to see if we have mpeg2video output option:
	it=`ffmpeg -f mpeg2video 2>&1 | grep 'Unknown input or output format: mpeg2video'`
	if [ -z "$it" ] ; then
		# mpeg2video should be ok
		[ "$mpeg_encoder" == 'ffmpeg' ] && mpeg_encoder='ffmpeg' 
	else
		[ "$mpeg_encoder" == 'ffmpeg' ] && mpeg_encoder='mpeg2enc' 
		myecho "[dvd-menu] Warning:  ffmpeg is not compiled with the mpeg2video option"
		myecho "[dvd-menu] 	  required for making dvds!  Using mpeg2enc instead."
		mpeg_encoder='mpeg2enc'
	fi
fi

if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
	checkforprog vcdimager
	ac3=0; mp2=1
fi

# genisoimage
checkforprog genisoimage

################################################## done finding programs

#Find the fonts
default_font1=`find -L $font_dir -name $default_font1 | head -n 1`
default_font2=`find -L $font_dir -name $default_font2 | head -n 1`

# verify fonts exist:
if [ -f "$default_font1" ] ; then
        default_font="$default_font1"
elif [ -f "$default_font2" ] ; then
        default_font="$default_font2"
else
        echo "[dvd-menu] Cannot find required fonts.  Using default ImageMagick font."
        default_font=""
fi
if [ -z "$font" ] ; then  # global font
	font="$default_font"	
else
	if [ -f "$font" ] ; then
		font="$font"
	else
		font="$default_font"
	fi	
fi
if [ -z "$title_font" ] ; then
	title_font="$font"	
else
	if [ -f "$title_font" ] ; then
		title_font="$title_font"
	else
		title_font="$font"
	fi	
fi
if [ -z "$button_font" ] ; then
	button_font="$font"	
else
	if [ -f "$button_font" ] ; then
		button_font="$button_font"
	else
		button_font="$default_font"
	fi
fi

logecho "[dvd-menu] title_font=$title_font"
logecho "[dvd-menu] button_font=$button_font"

###############################

myecho "[dvd-menu] ###################################"

###########################################################
# Start creating menus:
if [ "$no_menu" -eq 0 ] ; then
	myecho "[dvd-menu] Creating menu"
	## first, do the background image, then do the alpha mask:
	if [ -f "$bgfile" ] ; then
	        echo "[dvd-menu] Using background image $( truncate_filename "$bgfile")"
		background "$bgfile"
#	        convert "${bgfile}" -resize x"$dvd_height" -bordercolor black -border "$dvd_width"x240 -gravity center -crop "$dvd_width"x"$dvd_height"'+0+0!' -depth 8 -quality 100 "$tmpdir"/slideshow_background.ppm
	        mv "$tmpdir/slideshow_background.ppm" "$tmpdir/menu.ppm"
	else
		if [ -n "$bgfile" ] ; then
			# assume user passed a color that imagemagick can understand
			bg_color="$bgfile"  # over-rides internal setting
		fi
	        ## need to write out a blank image of the given size:
	        echo "[dvd-menu] Creating background image gradient"
	        convert -depth 8 -size "$dvd_width"'x'"$dvd_height" gradient:white-$bg_color -type TrueColorMatte "$tmpdir/menu.ppm"
	fi
	## save this menu background as the "background" for fadin/fadeout:
	cp "$tmpdir/menu.ppm" "$tmpdir/slideshow_background.ppm"
	
	## create overlay transparent image:
	convert -depth 8 -colors 3 +antialias -size "$dvd_width"'x'"$dvd_height" xc:transparent "$tmpdir"/button_background_mask.png
	cp "$tmpdir/button_background_mask.png" "$tmpdir/menu_mask.png"

	if [ -n "$dvd_title" ] ; then
	        echo "[dvd-menu] Creating white title mask"
		white_title_mask "$tmpdir/title_background_mask.png"
		composite -compose src-over -type TrueColorMatte -depth 8 -dissolve 60 "$tmpdir/title_background_mask.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
		title_text "$tmpdir/dvd_title.png"
		composite -compose src-over -depth 8 -type TrueColorMatte "$tmpdir/dvd_title.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
	fi

	## so, we have a few different layouts:
	## 1.  straight text on the right side
	## 2.  6 icon buttons with text underneath (not working)
	## 3.  6 icon buttons per page, multiple pages (not working)
	## 4.  15 icon buttons per page, no text?	 (not working)
	
#	if [ "${#buttonfile[@]}" -gt 1 ] ; then
#		menu_type="6images"  ## don't use this yet!!!
#	else
#		menu_type="textbar"
#	fi

	if [ "${#xmlfile[@]}" == 0 ] ; then
		echo "[dvd-menu] no xml files passed"
		i=0
		for it in "${title[@]}" ; do
			total_titles[$i]="${title[$i]}"
			i=$(( $i + 1 ))
		done
	else
		i=0
		for it in "${xmlfile[@]}" ; do
			total_titles[$i]="${xmlfile[$i]}"
			i=$(( $i + 1 ))
		done
	fi
	number_of_titles="$i"
	if [ $number_of_titles -gt 10 ] && [ "$menu_type" == 'textbar' ] ; then
		# use two-column layout by default:
		menu_type='2textbar'
		echo "using 2textbar layout"
	fi	
	## case 1:  old layout:
	if [ "$menu_type" == 'textbar' ] ; then  # plain text on right side
		menuright=$(( $menu_panel_location_x + $panel_width))
		menubottom=$(( $dvd_height + 50 ))  ## add 50 so we can chop off the bottom later
		menuh=$(( $dvd_height + 50 ))
		if [ "$no_white" -eq 0 ] ; then
		        echo "[dvd-menu] Creating white button mask"
			white_button_mask "$tmpdir/button_background_mask.png"
			composite -compose src-over -type TrueColorMatte -depth 8 -dissolve 50 "$tmpdir/button_background_mask.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
		fi
		## create embedded image if defined:
	        if [ -f "${embedfile[0]}" ]; then  # only one embedfile possible
	                echo "[dvd-menu] using image highlight ${embedfile[0]}"
	                # Resize to 240x320 (max), keep aspect ratio.
                        embed_height=$(identify -format %h "${embedfile[0]}")
                        embed_width=$(identify -format %w "${embedfile[0]}")
                        if [ $embed_width -gt $embed_height ]; then
                                embed_resolution=$embed_image_width"x"$embed_image_height
                        else
                                embed_resolution=$embed_image_height"x"$embed_image_width
                        fi
#                        echo "[dvd-menu] running convert"
	                convert "${embedfile[0]}" -resize "$sq_to_dvd_pixels" -resize "$embed_image_width"x"$embed_image_height" "$tmpdir"/embed.png
	                # Add embed image to menu background
	                embed_offset_y=$[ ( ($dvd_height-$menu_panel_location_y) - $(identify -format %h "$tmpdir"/embed.png) ) / 3 ]
	                embed_offset_x=$[ ( ($dvd_width-$menu_panel_location_x) - $(identify -format %w "$tmpdir"/embed.png) ) /2 ]
	                composite -compose src-over -geometry +$embed_offset_x+$[$menu_panel_location_y+$embed_offset_y] -type TrueColorMatte -depth 8 -dissolve 100 "$tmpdir"/embed.png "$tmpdir"/menu.ppm "$tmpdir"/menu.ppm
	        fi
		## loop over each title:
		let i=0
		for this_title in "${total_titles[@]}"; do
			echo "[dvd-menu] Title=${title[$i]}"
			# make each individual button:
			# to be fancy, let's add the button layers in two steps.  first the
			# button background, then the text:
			if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
				# add numbers to each line for VCD
				convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -gravity West -fill "$button_font_color" -draw "text 20,0 \"$(( $i + 1 )). ${title[$i]}\"" "$tmpdir"/button_text.png
			else
				convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -gravity West -fill "$button_font_color" -draw "text 20,0 \"${title[$i]}\"" "$tmpdir"/button_text.png
			fi
			# make each individual button mask (for when it's selected)
			convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -fill $button_hilight_color -gravity West +antialias -colors 3 -draw "text 20,0 \"${title[$i]}\"" "$tmpdir"/button_mask.png
	
			## add each button as necessary, and increment the location downward
			echo "[dvd-menu] adding title button ${title[$i]} at $menu_panel_location_x , $menu_button_location_y"
			composite -compose src-over -depth 8 -geometry +$menu_panel_location_x+$menu_button_location_y "$tmpdir/button_text.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
			## add each overlay mask for the selection of buttons:
			composite -compose src-over -depth 8 -colors 3 -quality 100 -geometry +$menu_panel_location_x+$menu_button_location_y "$tmpdir/button_mask.png" "$tmpdir/menu_mask.png" "$tmpdir/menu_mask.png"
		
			## now record the button location:
			right[$i]="$(( $menu_panel_location_x + $panel_width ))"
			[ "${right[$i]}" -gt "$dvd_width" ] && right[$i]=$(( $dvd_width - 10 ))
			left[$i]="$(( $menu_panel_location_x ))"
			top[$i]="$(( $menu_button_location_y +1 ))"
			bottom[$i]="$(( $menu_button_location_y + $panel_height ))"
			[ "${bottom[$i]}" -gt "$dvd_height" ] && bottom[$i]=$(( $dvd_height - 10 ))

			echo "[dvd-menu] setting button mask l,r,t,b = ${left[$i]} ${right[$i]} ${top[$i]} ${bottom[$i]}"
	
			vts="$(( $i + 1 ))"
#			echo "[dvd-menu] i=$i vts=$vts"
				
			let menu_button_location_y=$menu_button_location_y+$panel_height
			let i=$i+1
		done
	elif [ "$menu_type" == '2textbar' ] ; then  # two columns of text.  no hilight?
		menuright=$(( $menu_panel_location_x + $panel_width))
		menubottom=$(( $dvd_height + 50 ))  ## add 50 so we can chop off the bottom later
		menuh=$(( $dvd_height + 50 ))
		if [ "$no_white" -eq 0 ] ; then
		        echo "[dvd-menu] Creating white button mask"
			white_button_mask "$tmpdir/button_background_mask.png"
			composite -compose src-over -type TrueColorMatte -depth 8 -dissolve 50 "$tmpdir/button_background_mask.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
			convert "$tmpdir/button_background_mask.png" -flop "$tmpdir/button_background_mask.png"
			composite -compose src-over -type TrueColorMatte -depth 8 -dissolve 50 "$tmpdir/button_background_mask.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
		fi
		# split the titles between the left and right:
		menu_panel_location_x_l=0
		menu_panel_location_x_r=$menu_panel_location_x
		menu_panel_location_x=$menu_panel_location_x_l  # default starting point
		menu_button_location_y_orig=$menu_button_location_y
		button_alignment="East"
		## loop over each title:
		let i=0
		for this_title in "${total_titles[@]}"; do
			echo "[dvd-menu] Title $i = ${title[$i]}"
			# make each individual button:
			# to be fancy, let's add the button layers in two steps.  first the
			# button background, then the text:
			if [ "$i" -eq "$(( $number_of_titles / 2 + 1 ))" ] ; then
				# switch to right panel and reset the height:
				menu_panel_location_x=$menu_panel_location_x_r
				menu_button_location_y=$menu_button_location_y_orig
				button_alignment="West"
			fi
			if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
				# add numbers to each line for VCD
				convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -gravity $button_alignment -fill "$button_font_color" -draw "text 20,0 \"$(( $i + 1 )). ${title[$i]}\"" "$tmpdir"/button_text.png
			else
				convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -gravity $button_alignment -fill "$button_font_color" -draw "text 20,0 \"${title[$i]}\"" "$tmpdir"/button_text.png
			fi
			# make each individual button mask (for when it's selected)
			convert -size "$panel_width"x"$panel_height" xc:transparent -quality 100 -font "$button_font" -pointsize $button_font_size -fill $button_hilight_color -gravity $button_alignment +antialias -colors 3 -draw "text 20,0 \"${title[$i]}\"" "$tmpdir"/button_mask.png
	
			## add each button as necessary, and increment the location downward
			echo "[dvd-menu] adding title button ${title[$i]} at $menu_panel_location_x , $menu_button_location_y"
			composite -compose src-over -depth 8 -geometry +$menu_panel_location_x+$menu_button_location_y "$tmpdir/button_text.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
			## add each overlay mask for the selection of buttons:
			composite -compose src-over -depth 8 -colors 3 -quality 100 -geometry +$menu_panel_location_x+$menu_button_location_y "$tmpdir/button_mask.png" "$tmpdir/menu_mask.png" "$tmpdir/menu_mask.png"
		
			## now record the button location:
			right[$i]="$(( $menu_panel_location_x + $panel_width ))"
			[ "${right[$i]}" -gt "$dvd_width" ] && right[$i]=$(( $dvd_width - 10 ))
			left[$i]="$(( $menu_panel_location_x ))"
			top[$i]="$(( $menu_button_location_y ))"
			bottom[$i]="$(( $menu_button_location_y + $panel_height ))"
			[ "${bottom[$i]}" -gt "$dvd_height" ] && bottom[$i]=$(( $dvd_height - 10 ))

			echo "[dvd-menu] setting button mask l,r,t,b = ${left[$i]} ${right[$i]} ${top[$i]} ${bottom[$i]}"
	
			vts="$(( $i + 1 ))"
#			echo "[dvd-menu] i=$i vts=$vts"
				
			let menu_button_location_y=$menu_button_location_y+$panel_height
			let i=$i+1
		done

	elif [ "$menu_type" == '6images' ] ; then   ### don't use this yet!
		## loop over each title:
		let i=0
		let embed=1  # embedfile is for normal menu type
		let embed_row=1

		# grab any info from .xml file comment lines:
		for this_title in "${total_files[@]}"; do
			suffix=`echo "${xmlfile[$i]}" | awk -F. '{print $NF}'`
			if [ "$suffix" == 'xml' ] ; then
				xml_button[$i]=$( grep '<!-- button=' "${xmlfile[$i]}" | awk -F= '{print $2}' | awk -F' -->' '{print $1}' | tr -d '\047' | tr -d '\042' )
#echo "xml_button[$i]=${xml_button[$i]}"
				[ -n "${xml_button[$i]}" ] && echo "[dvd-menu] xml_button $i = $( truncate_filename "${xml_button[$i]}" )"
				xml_title[$i]=$( grep '<!-- title="' "${xmlfile[$i]}" | awk -F= '{print $2}' | awk -F'" -->' '{print $1}' | tr -d '\047' | tr -d '\042' )
#echo "xml_title[$i]=${xml_title[$i]}"
				[ -n "${xml_title[$i]}" ] && echo "[dvd-menu] xml_title $i = ${xml_title[$i]}"
			fi
			# make each individual button:
			# to be fancy, let's add the button layers in two steps.  first the
			# button background, then the text:
		        ## create embedded image hilight if required:
			dvd_width_scaled=$(( $dvd_width * $sq_pixel_multiplier / 1000 ))
			hilight_height=90
			hilight_width=135
#			hilight_height=$(( ( $dvd_height - $title_white_height - 3 * $hilight_spacing_y - 2*$hilight_font_size)/2 ))
#			hilight_width=$(( $hilight_height * $dvd_width / $dvd_height ))
			buttons="${#xmlfile[@]}"
			if [ "$buttons" -gt 6 ] ; then
				echo "[dvd-menu] ERROR:  too many buttons for this menu type!"
				exit 1
			elif [ "$buttons" == 4 ] ; then
				hilight_spacing_x=100
				hilight_start_location_x=$(( ($dvd_width - 2*$hilight_width - 1*$hilight_spacing_x )/2 ))
				hilight_start_location_y=$(( $title_white_height + $hilight_spacing_y / 2 ))
			elif [ "$buttons" -gt 3 ] ; then
				hilight_start_location_x=$(( ($dvd_width - 3*$hilight_width - 2*$hilight_spacing_x )/2 ))
				hilight_start_location_y=$(( $title_white_height + $hilight_spacing_y / 2 ))
			else
				[ "$buttons" == 2 ] && hilight_spacing_x=100
				hilight_start_location_x=$(( ($dvd_width - $buttons * $hilight_width - ( $buttons - 1 )*$hilight_spacing_x )/2 ))
				hilight_start_location_y=$(( $title_white_height + $hilight_spacing_y ))
			fi
			## size constraint seems to be vertically when we have a title.
			## so, eventually, we could have different situations, depending on whether or not
			## a title exists, etc...  let's assume it does for now!

			[ -z "${buttonfile[$i]}" ] && buttonfile[$i]="${xml_button[$i]}"
			[ -z "${title[$i]}" ] && title[$i]="${xml_title[$i]}"
			echo "[dvd-menu] Title=${title[$i]}"
			echo "[dvd-menu] Image=${buttonfile[$i]}"
			button_text_white=1
			if [ "$button_text_white" == 1 ] ; then
				text_height=$(( 3* $hilight_font_size ))
				text_width=$(( $hilight_width + $hilight_spacing_x / 2 ))
				convert -size "$text_width"x$text_height xc:transparent -depth 8 "$tmpdir"/button_mask_trans.png
				convert -size "$text_width"x$text_height xc:transparent -depth 8 -fill white -draw "roundRectangle 0,0,$text_width,$text_height $button_radius,$button_radius" -background none -blur 0x3 "$tmpdir"/button_mask.png
				composite -compose src-over -type TrueColorMatte -depth 8 -dissolve 60 "$tmpdir"/button_mask.png "$tmpdir"/button_mask_trans.png "$tmpdir"/button_mask.png

				convert -size ${text_width}x$text_height xc:transparent -quality 100 -font "$button_font" -fill "$button_font_color" -pointsize $button_font_size -gravity center -draw "text 0,0 \"${title[$i]}\"" "$tmpdir"/button_text.png
				composite -compose src-over -type TrueColorMatte -depth 8 "$tmpdir"/button_text.png "$tmpdir"/button_mask.png "$tmpdir"/button_text.png
			fi
#echo "buttonfile=${buttonfile[$i]}".
		        if [ -e "${buttonfile[$i]}" ]; then
		                echo "[dvd-menu] using image $( truncate_filename "${buttonfile[$i]}" )"
#				echo "[dvd-menu] hilight_width=$hilight_width hilight_height=$hilight_height"
		                convert "${buttonfile[$i]}" -resize "$hilight_width"x"$hilight_height" -quality 100 "$tmpdir/embed.jpg"
				actual_hilight_width=$( imagewidth_sq "$tmpdir"/embed.jpg )
				actual_hilight_height=$( imageheight "$tmpdir"/embed.jpg )
				# make each individual button mask (for when it's selected)
				convert -colors 3 +antialias -size "$actual_hilight_width"x"$actual_hilight_height" xc:transparent -fill transparent -stroke $button_hilight_color -strokewidth 5 -draw "rectangle 0,0 $actual_hilight_width,$actual_hilight_height" -quality 100 "$tmpdir/button_mask.png"
	
				## calculate location of this image:
				if [ "$buttons" == "4" ] ; then
					[ $embed -ge 3 ] && embed_row=2 || embed_row=1
					[ $embed -ge 3 ] && embed_col=$(( $embed - 2 )) || embed_col=$embed
				elif [ "$buttons" -gt 3 ] ; then
					[ $embed -ge 4 ] && embed_row=2 || embed_row=1
					[ $embed -ge 4 ] && embed_col=$(( $embed - 3 )) || embed_col=$embed
				elif [ "$buttons" -le 3 ] ; then
					embed_row=1
					embed_col=$embed
				fi
				hilight_x=$(( $hilight_start_location_x + ($embed_col - 1)*($hilight_spacing_x + $hilight_width) ))
				hilight_y=$(( $hilight_start_location_y + ($embed_row - 1)*($hilight_spacing_y + $hilight_height + $hilight_font_size ) ))
				button_text_location_x=$(( $hilight_x - $hilight_spacing_x / 4 ))
				button_text_location_y=$(( $hilight_y + $hilight_height ))
				echo "[dvd-menu] r,c=$embed_row,$embed_col hilight_x_loc=$hilight_x hilight_y_loc=$hilight_y"
				
				## add each button as necessary, and increment the location 
				composite -compose src-over -type TrueColorMatte -depth 8 -geometry +$button_text_location_x+$button_text_location_y "$tmpdir/button_text.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"

		                # Add embed image to menu background
				difference_x=$(( ( $hilight_width - $actual_hilight_width )/2 ))
				difference_y=$(( ( $hilight_height - $actual_hilight_height )/2 ))
				## adjust hilight location:
				hilight_x=$(( $hilight_x + $difference_x ))
				hilight_y=$(( $hilight_y + $difference_y ))
				composite -compose src-over -type TrueColorMatte -depth 8 -geometry +$hilight_x+$hilight_y "$tmpdir/embed.jpg" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
				## add each overlay mask for the selection of buttons:
				composite -compose src-over -depth 8 -colors 3 -quality 100 -geometry +$hilight_x+$hilight_y "$tmpdir/button_mask.png" "$tmpdir/menu_mask.png" "$tmpdir/menu_mask.png"
	
				## now record the button location:
				right[$i]="$(( $hilight_x + $hilight_width ))"
				left[$i]="$(( $hilight_x ))"
				top[$i]="$(( $hilight_y ))"
				bottom[$i]="$(( $hilight_y + $hilight_height ))"
				let embed=$embed+1
		        fi
			##################################################################33
			
			vts="$(( $i + 1 ))"
			echo "[dvd-menu] i=$i vts=$vts"
				
			let i=$i+1
		done
	fi	# end of different menu types

	## now convert the menu background to a mpg
	if [ "$mpeg_encoder" == 'mpeg2enc' ] ; then
		ppmtoy4m -v 0 -n "$frames" -r -S "$subsample" -F $ppmtoy4m_frc -A $ppmtoy4m_aspect -I p "$tmpdir/menu.ppm" | mpeg2enc $mpeg2enc_params -o "$tmpdir/menu.mpg" 
	else
		ppmtoy4m -v 0 -n "$frames" -r -S "$subsample" -F $ppmtoy4m_frc -A $ppmtoy4m_aspect -I p "$tmpdir/menu.ppm" | ffmpeg -f yuv4mpegpipe -i - -target $ffmpeg_target -r $framerate -an -aspect $aspect_ratio -s "$dvd_width"x"$dvd_height" -y -f mpeg2video "$tmpdir/menu.mpg" >> "$logfile" 2>&1
	fi
	## or, for a background video, cat all the frame through a composite call to add the button text and mask
	## all at once.  (and, use transparent background for button text)


	if [ "$fade_to_image" == 'black' ] ; then
		background black "$tmpdir"/bg_fade.ppm
	elif [ -f "$fade_to_image" ] ; then
		background "$fade_to_image" "$tmpdir"/bg_fade.ppm
	fi
	if [ "$fadein" -eq 1 ] ; then
		if [ "$fade_to_image" == 'black' ] ; then
			echo "[dvd-menu] Creating fadein from black..."
			fade "$tmpdir/menu.ppm" "$tmpdir"/bg_fade.ppm 2
		elif [ -f "$fade_to_image" ] ; then
			echo "[dvd-menu] Creating fadein from image..."
			fade "$tmpdir/menu.ppm" "$tmpdir"/bg_fade.ppm 2
		else
			echo "[dvd-menu] Creating fadein from background..."
			fade "$tmpdir/menu.ppm" "$tmpdir/slideshow_background.ppm" 2
		fi
		mv "$tmpdir/fade.mpg" "$tmpdir/fadein.mpg"
	fi
	if [ "$fadeout" -eq 1 ] ; then
		if [ "$fade_to_image" == 'black' ] ; then
			echo "[dvd-menu] Creating fadeout to black..."
			fade "$tmpdir"/bg_fade.ppm "$tmpdir"/menu.ppm 2
		elif [ -e "$fade_to_image" ] ; then
			echo "[dvd-menu] Creating fadeout to image..."
			fade "$tmpdir"/bg_fade.ppm "$tmpdir"/menu.ppm 2
		else
			echo "[dvd-menu] Creating fadeout to background..."
			fade "$tmpdir/slideshow_background.ppm" "$tmpdir/menu.ppm" 2
		fi
		mv "$tmpdir/fade.mpg" "$tmpdir/fadeout.mpg"
	fi
	
	# optionally fade out at a certain time?
	if [ -n "$audiofile" ] ; then
		## get type of file:  mp3, ogg, or wav
		suffix=`echo "$audiofile" | awk -F. '{print $NF}'`
		echo "[dvd-menu] Decoding $suffix audio file"
		if [ "$suffix" == "mp3" ] ; then
			checkforprog lame
		        lame --decode "$audiofile" "$tmpdir/audio_tmp.wav" >> "$logfile" 2>&1
			# convert it to 48khz:
                        song_length_ms=`wavlength "$tmpdir/audio_tmp.wav"`
                        song_length_hms=`hms $song_length_ms`
			sox -v 0.90 "$tmpdir/audio_tmp.wav" -r $audio_sample_rate "$tmpdir/audio.wav" fade t 2 "$song_length_hms" 2
			rm "$tmpdir/audio_tmp.wav"
		elif [ "$suffix" == "ogg" ] ; then
			checkforprog oggdec
		        oggdec -o "$tmpdir/audio_tmp.wav" "$audiofile" >> "$logfile" 2>&1
                        song_length_ms=`wavlength "$tmpdir/audio_tmp.wav"`
                        song_length_hms=`hms $song_length_ms`
echo "song=$audiofile"
			# convert it to 48khz:
			sox -v 0.90 "$tmpdir/audio_tmp.wav" -r $audio_sample_rate "$tmpdir/audio.wav" fade t 2 "$song_length_hms" 2
			rm "$tmpdir/audio_tmp.wav"
		elif [ "$suffix" == "wav" ] ; then
			# convert it to 48khz:
                        song_length_ms=`wavlength "$audiofile"`
                        song_length_hms=`hms $song_length_ms`
echo "song=$audiofile"
			sox -v 0.90 "$audiofile" -r $audio_sample_rate "$tmpdir/audio.wav" fade t 2 "$song_length_hms" 2
		else
		        echo "[dvd-menu] ERROR:  Unknown audio file format.  Must be .mp3, .ogg, or .wav"
		fi
	else
		## create silence:  
		echo "[dvd-menu] creating silent audio track"
		## create audio for 1 second:
		sox -t raw $sox_int $sox_word -c 2 -r $audio_sample_rate /dev/zero -t wav - trim 0 0:01.000 > "$tmpdir/audio.wav" 
	fi

	if [ "$fadein" -eq 1 ] || [ "$fadeout" -eq 1 ] ; then
		# create slience .wav file for 1 second for fadein/fadeout
		sox -t raw $sox_int $sox_word -c 2 -r $audio_sample_rate /dev/zero -t wav - trim 0 0:02.000 > "$tmpdir/fade_audio.wav" 
	fi
	

	if [ "$ac3" -eq 1 ] ; then
	        echo "[dvd-menu] Creating ac3 audio..."
	        rm -f "$tmpdir/audio.ac3"
	        ffmpeg -i "$tmpdir/audio.wav" -vn -y -ab "$audio_bitrate"k -acodec ac3_fixed -ar $audio_sample_rate -ac 6 "$tmpdir/audio.ac3" >> "$logfile" 2>&1
		if [ "$fadein" -eq 1 ] || [ "$fadeout" -eq 1 ] ; then
	        	ffmpeg -i "$tmpdir/fade_audio.wav" -vn -y -ab "$audio_bitrate"k -acodec ac3_fixed -ar $audio_sample_rate -ac 6 "$tmpdir/fade_audio.ac3" >> "$logfile" 2>&1
		fi
	else
		## twolame is way faster! (3x in my test)
		it=`which twolame`
		if [ -n "$it" ] ; then
		        twolame_version=`twolame -h | head -n 4 | grep version | awk '{ print $3 }'`
		        echo "[dvd-menu] using twolame $twolame_version..."
		        if [ "$twolame_version" == '0.2m' ] ; then
		        	twolame -s $audio_sample_rate -b $audio_bitrate "$tmpdir/audio.wav" "$tmpdir/audio.mp2" >> "$logfile" 2>&1
		        else
				## need to hard code in the sample rate here because the syntax is bad
		        	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
		        		twolame -s 44100 -b $audio_bitrate "$tmpdir/audio.wav" "$tmpdir/audio.mp2" >> "$logfile" 2>&1
				else
		        		twolame -s 48000 -b $audio_bitrate "$tmpdir/audio.wav" "$tmpdir/audio.mp2" >> "$logfile" 2>&1
				fi
		        fi
		else
		        myecho "[dvd-menu] using mp2enc"
		        mp2enc -v 0 -b $audio_bitrate -r $audio_sample_rate -s -o "$tmpdir/audio.mp2" < "$tmpdir/audio.wav" 
		fi
		if [ "$fadein" -eq 1 ] || [ "$fadeout" -eq 1 ] ; then
		        mp2enc -v 0 -b $audio_bitrate -r $audio_sample_rate -s -o "$tmpdir/fade_audio.mp2" < "$tmpdir/fade_audio.wav" 
		fi
	fi

	## now multiplex the audio and video:
	myecho "[dvd-menu] mplexing the files............."
	if [ "$ac3" -eq 1 ] ; then
		mplex -v 0 -f $mplex_type -o "$tmpdir/menu_t.vob" "$tmpdir/menu.mpg" "$tmpdir"/audio.ac3 
		if [ "$fadein" -eq 1 ] ; then
			mplex -v 0 -f $mplex_type -o "$outdir/fadein.vob" "$tmpdir/fadein.mpg" "$tmpdir"/fade_audio.ac3 
		fi
		if [ "$fadeout" -eq 1 ] ; then
			mplex -v 0 -f $mplex_type -o "$outdir/fadeout.vob" "$tmpdir/fadeout.mpg" "$tmpdir"/fade_audio.ac3 
		fi
	else
		mplex -v 0 -f $mplex_type -o "$tmpdir/menu_t.vob" "$tmpdir/menu.mpg" "$tmpdir"/audio.mp2 
		if [ "$fadein" -eq 1 ] ; then
			mplex -v 0 -f $mplex_type -o "$outdir/fadein.vob" "$tmpdir/fadein.mpg" "$tmpdir"/fade_audio.mp2 
		fi
		if [ "$fadeout" -eq 1 ] ; then
			mplex -v 0 -f $mplex_type -o "$outdir/fadeout.vob" "$tmpdir/fadeout.mpg" "$tmpdir"/fade_audio.mp2 
		fi
	fi

	## start of button subpicture XML file creation for spumux:
	## now define the buttons:
	echo '<subpictures>' > "$tmpdir/menu.spumux"
	echo '	<stream>' >> "$tmpdir/menu.spumux"
	echo -n '		<spu start="00:00:00.00" end="00:00:00.00" highlight="' >> "$tmpdir/menu.spumux"
	echo "$tmpdir/menu_mask.png"'" force="yes" >' >> "$tmpdir/menu.spumux"
	## now loop over the buttons:
	let i=0
	for it in "${left[@]}"; do
		echo -n '                       <button ' >> "$tmpdir/menu.spumux"
		# insert an up link to the upAction for the first button
		if [ $i -eq 0 ] && [ "$upAction" -eq 1 ]; then
			echo -n ' up="upAction" ' >> "$tmpdir/menu.spumux"
		fi
		echo 'x0="'${left[$i]}'" y0="'${top[$i]}'" x1="'${right[$i]}'" y1="'${bottom[$i]}'" />' >> "$tmpdir/menu.spumux"

		let i=$i+1
	done
        # append an up action to the button list
        if [ "${upAction}" -eq 1 ] ; then
                echo '                  <action name="upAction" />' >> "$tmpdir/menu.spumux"
        fi
	echo '		</spu>' >> "$tmpdir/menu.spumux"
	echo '	</stream>' >> "$tmpdir/menu.spumux"
	echo '</subpictures>' >> "$tmpdir/menu.spumux"

	myecho "[dvd-menu] ###############################################"
	myecho "[dvd-menu] spumuxing the files............."
	## using spumux now:
	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
		mv "$tmpdir/menu_t.vob" "$outdir/menu.mpg"
	else  # DVD
#		spumux -v 0 -P "$tmpdir/menu.spumux" < "$tmpdir/menu_t.vob" > "$outdir/menu.vob" >> "$logfile" 2>&1
		spumux -v 0 -P "$tmpdir/menu.spumux" < "$tmpdir/menu_t.vob" > "$outdir/menu.vob" 2>> "$logfile"
		if [ $? -ne 0 ] ; then
			## spumux errored
			myecho "[dvd-menu] ERROR during spumux execution!"
			myecho "[dvd-menu] see $logfile for details"
			exit 1
		fi
	fi
fi
# end if we're actually making a menu.  otherwise, just to the stuff below

#############################################################
## menu part of .xml file:

# write header:
if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
	# vcd menus:
	echo "[dvd-menu] Creating vcd menu"
	echo '<?xml version="1.0"?>
<!DOCTYPE videocd PUBLIC "-//GNU//DTD VideoCD//EN" "http://www.gnu.org/software/vcdimager/videocd.dtd">' > "$outdir/vmgm.xml"
	if [ "$vcd" -eq 1 ] ; then
		echo '<videocd xmlns="http://www.gnu.org/software/vcdimager/1.0/" class="vcd" version="2.0">' >> "$outdir/vmgm.xml"
	elif [ "$svcd" -eq 1 ] ; then
		echo '<videocd xmlns="http://www.gnu.org/software/vcdimager/1.0/" class="svcd" version="1.0">' >> "$outdir/vmgm.xml"
	else
		echo '[dvd-menu] ERROR: Bad vcd type' ; exit 1
	fi
	echo '<info>
    <album-id>666</album-id>
    <volume-count>1</volume-count>
    <volume-number>1</volume-number>
    <restriction>0</restriction>
</info>
<pvd>
    <volume-id>VIDEOCD</volume-id>
    <system-id>CD-RTOS CD-BRIDGE</system-id>
    <preparer-id>scott_preparer</preparer-id>
    <publisher-id></publisher-id>
</pvd>' >> "$outdir/vmgm.xml"
	echo '<sequence-items>' >> "$outdir/vmgm.xml"
	echo '   <sequence-item src="'"$outdir/menu.mpg"'" id="seq-menu"/>' >> "$outdir/vmgm.xml"
	total_files="${#xmlfile[*]}" ; i=0
#	for it in `seq 1 1 $total_files` ; do
	for (( it=1 ; it<=total_files ; it++ )) ; do
		if [ "$(( $i + 1 ))" -eq "$total_files" ] ; then continuous=0 ; fi
		myecho "[dvd-menu] sequencefile=${xmlfile[$i]}"
		suffix=`echo "${xmlfile[$i]}" | awk -F. '{print $NF}'`
		if [ "$suffix" == 'xml' ] ; then
			file_base=`basename "${xmlfile[$i]}" .xml`
			xml_button[$i]=$( grep '<!-- button=' "${xmlfile[$i]}" | awk -F= '{print $2}' | awk -F' -->' '{print $1}' )
#			[ -n "${xml_button[$i]}" ] && echo "[dvd-menu] xml_button $i = ${xml_button[$i]}"
			xml_title[$i]=$( grep '<!-- title=' "${xmlfile[$i]}" | awk -F= '{print $2}' | awk -F' -->' '{print $1}' )
#			[ -n "${xml_title[$i]}" ] && echo "[dvd-menu] xml_title $i = ${xml_title[$i]}"
		elif [ "$suffix" == 'mpg' ] ; then
			file_base=`basename "${xmlfile[$i]}" .mpg`
		elif [ "$suffix" == 'vob' ] ; then
			file_base=`basename "${xmlfile[$i]}" .vob`
		else
			echo '[dvd-menu] error... filetype not supported!'
			exit
		fi
#		myecho "[dvd-menu] file=${xmlfile[$i]} file_base=$file_base suffix=$suffix"
		## create <sequence-items> at the same time:
		echo '     <sequence-item src="'"$(cat ${xmlfile[$i]})"'" id="'"$file_base"_$(($i+1))'"/>' >> "$outdir/vmgm.xml"
		i=$(($i+1))
	done
	echo '</sequence-items>' >> "$outdir/vmgm.xml"

else	# dvd menus:
	## now build the dvdauthor xml file:
	## do we need "jumppad"?
	echo '<dvdauthor dest="'$dvd_dir'" jumppad="0">' > "$outdir/vmgm.xml"
	echo '        <vmgm>' >> "$outdir/vmgm.xml"
	echo '           <menus>' >> "$outdir/vmgm.xml"
	echo '               <video format="'$ntsc_or_pal'" />' >> "$outdir/vmgm.xml"
fi

# write menu
if [ "$no_menu" -eq 0 ] ; then
	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
		echo "<pbc>" >> "$outdir/vmgm.xml"
		echo '    <selection id="menu">' >> "$outdir/vmgm.xml"
		echo '      <bsn>1</bsn>' >> "$outdir/vmgm.xml"
		echo '      <loop jump-timing="immediate">0</loop>' >> "$outdir/vmgm.xml"
		echo '      <play-item ref="seq-menu"/>' >> "$outdir/vmgm.xml"
	else  # dvd
		echo '               <pgc entry="title" >' >> "$outdir/vmgm.xml"
		## put fadein before each menu:
		if [ "$fadein" -eq 1 ] ; then
		echo '                    <vob file="'$outdir/fadein.vob'"/>' >> "$outdir/vmgm.xml"
		fi
		echo '                    <vob file="'$outdir/menu.vob'" pause="inf"/>' >> "$outdir/vmgm.xml"
	fi


	## now loop over each title for select buttons:
#	myecho "[dvd-menu] xmlfiles=${xmlfile[@]}"
	let i=1   ## titles start from 1, not zero.
	let m=1   ## menus start at 1
	for it in "${xmlfile[@]}"; do
		# verify xml file exists:
		if [ ! -r "$it" ] ; then
			echo "[dvd-menu] ERROR: XML file $it does not exist."
			exit 1
		fi
		suffix=`echo "$it" | awk -F. '{print $NF}'`
		if [ "$suffix" == 'xml' ] ; then
			file_base=`basename "$it" .xml`
		elif [ "$suffix" == 'mpg' ] ; then
			file_base=`basename "$it" .mpg`
		elif [ "$suffix" == 'vob' ] ; then
			file_base=`basename "$it" .vob`
		else
			echo '[dvd-menu] error... filetype not supported!'
			exit 1
		fi
		if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
			echo '      <select ref="'video$i'"/>' >> "$outdir/vmgm.xml"
			select[$i]="$file_base"_$i
		else  # dvd
			if [ -f "$file_base"_submenu.xml ] ; then
				echo '             		<button> jump menu '$m'; </button>' >> "$outdir/vmgm.xml"
				let m=$m+1
			else
				echo '             		<button> jump title '$i'; </button>' >> "$outdir/vmgm.xml"
			fi
		fi
		let i=$i+1
		let m=$m+1
	done



	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
		echo '    </selection>' >> "$outdir/vmgm.xml"
	else  # dvd
		echo '                    <post> jump vmgm menu 1; </post>' >> "$outdir/vmgm.xml"
		echo '                </pgc>' >> "$outdir/vmgm.xml"
	fi
fi

## close last section and open next one:
if [ "$vcd" -eq 0 ] && [ "$svcd" -eq 0 ] ; then ## DVD
	echo '           </menus>' >> "$outdir/vmgm.xml"
	echo '        </vmgm>' >> "$outdir/vmgm.xml"
	echo '        <titleset>' >> "$outdir/vmgm.xml"
	## add a menu to the vtsm that pops back to the main menu
	## for DVD players that default to first vtsm menu when you
	## press the DVD menu button.  (thanks for fix from will guaraldi)
	echo '           <menus>' >> "$outdir/vmgm.xml"
	echo '              <video format="'$ntsc_or_pal'" />' >> "$outdir/vmgm.xml"
	echo '              <pgc>' >> "$outdir/vmgm.xml"
	echo '              <post> jump vmgm menu; </post>' >> "$outdir/vmgm.xml"
	echo '              </pgc>' >> "$outdir/vmgm.xml"
	echo '           </menus>' >> "$outdir/vmgm.xml"
	## every dvd has at least one title!
	echo '		<titles>' > "$tmpdir/titles_vmgm.xml"
	echo '		<subpicture lang="'$subtitle_code'"/>' >> "$tmpdir/titles_vmgm.xml"
fi	
## now cat the other ones into this file...
let i=0   
let title=1   
let m=1   

## get rid of existing files:
#rm -f "$tmpdir/menus_vmgm.xml"

total_files="${#xmlfile[*]}"
#for it in `seq 1 1 $total_files` ; do
for (( it=1 ; it<=total_files ; it++ )) ; do
	if [ "$(( $i + 1 ))" -eq "$total_files" ] ; then continuous=0 ; fi
	let j=$i+2
	myecho "[dvd-menu] sequencefile=${xmlfile[$i]}"
	suffix=`echo "${xmlfile[$i]}" | awk -F. '{print $NF}'`
	if [ "$suffix" == 'xml' ] ; then
		file_base=`basename "${xmlfile[$i]}" .xml`
	elif [ "$suffix" == 'mpg' ] ; then
		file_base=`basename "${xmlfile[$i]}" .mpg`
	elif [ "$suffix" == 'vob' ] ; then
		file_base=`basename "${xmlfile[$i]}" .vob`
	else
		echo '[dvd-menu] error... filetype not supported!'
		exit
	fi

	nextmenu=$(( $m + 1 ))
	lastmenu=$(( $m - 1 ))
#	myecho "[dvd-menu] file=${xmlfile[$i]} file_base=$file_base suffix=$suffix"
	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then  # VCD
		echo '   <playlist id="video'$(($i+1))'">' >> "$tmpdir/titles_vmgm.xml"
#		echo '      <select ref="'"$file_base"_$i'"/>' >> "$outdir/vmgm.xml"
		if [ "$continuous" -eq 1 ] ; then
			echo '     <next ref="'video$(($i+2))'"/>' >> "$tmpdir/titles_vmgm.xml"
		elif [ "$no_menu" -eq 0 ] ; then
			echo '     <next ref="menu"/>' >> "$tmpdir/titles_vmgm.xml"
		fi
		echo '     <wait>0</wait>' >> "$tmpdir/titles_vmgm.xml"
		echo '     <play-item ref="'"$file_base"_$(($i+1))'"/>' >> "$tmpdir/titles_vmgm.xml"
#		echo '     <sequence-item src="'"$(full_path ${xmlfile[$i]})"'" id="'"$file_base"_$(($i+1))'"/>' >> "$tmpdir/titles_seq.xml"
		echo '   </playlist>' >> "$tmpdir/titles_vmgm.xml"
	else # dvd
		# make sure palette file exists:
#		if [ -e "$outdir"/palette.rgb ] ; then
#			echo '		<pgc palette="'$outdir/palette.rgb'">' >> "$tmpdir/titles_vmgm.xml"
#		else
			echo '		<pgc>' >> "$tmpdir/titles_vmgm.xml"
#		fi
		## fadeout from vmgm
		if [ "$fadeout" -eq 1 ] && [ "$continuous_method" -ne 1 ] ; then
			# non-continuous play means we want to fadeout from every menu
			echo '                <vob file="'$outdir/fadeout.vob'"/>' >> "$tmpdir/titles_vmgm.xml"
		elif [ "$fadeout" -eq 1 ] && [ "$continuous_method" -eq 1 ] && [ "$i" -eq 0 ] ; then
			# continuous play means we only fadeout from the menu on the first .vob
			echo '                <vob file="'$outdir/fadeout.vob'"/>' >> "$tmpdir/titles_vmgm.xml"
		fi
		## now, add in the .vob file:
		if [ "$suffix" == 'mpg' ] || [ "$suffix" == 'vob' ]; then
			echo '		<vob file="'${xmlfile[$i]}'"  />' >> "$tmpdir/titles_vmgm.xml"
		else
			cat "${xmlfile[$i]}" >> "$tmpdir/titles_vmgm.xml"
		fi
		if [ "$continuous" -eq 1 ] ; then
			echo "		<post> jump title $j; </post>" >> "$tmpdir/titles_vmgm.xml"
		elif [ "$no_menu" -eq 0 ] ; then
			echo '		<post> call vmgm menu 1; </post>' >> "$tmpdir/titles_vmgm.xml"
		fi
		echo '		</pgc>' >> "$tmpdir/titles_vmgm.xml"
	fi
	let i=$i+1
	let title=$title+1
done

## finish up menus tag:
#if [ $m -gt 1 ] ; then  # only if we had submenus:
#	echo '	</menus>' >> "$tmpdir/menus_vmgm.xml"
#fi

if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then  # VCD
	echo '</pbc>' >> "$tmpdir/titles_vmgm.xml"
#	echo '</sequence-items>' >> "$tmpdir/titles_seq.xml"
	cat "$tmpdir/titles_vmgm.xml" >> "$outdir/vmgm.xml"
#	cat "$tmpdir/titles_seq.xml" >> "$outdir/vmgm.xml"
	echo '</videocd>' >> "$outdir/vmgm.xml"
else
	## finish up titles tag:
	echo '	</titles>' >> "$tmpdir/titles_vmgm.xml"
	## join the menus and titles with the vmgm menu:
	#if [ -f "$tmpdir/menus_vmgm.xml" ] ; then # only if submenus exist!
	#	cat "$tmpdir/menus_vmgm.xml" >> "$outdir/vmgm.xml"
	#fi
	cat "$tmpdir/titles_vmgm.xml" >> "$outdir/vmgm.xml"
	echo '	</titleset>' >> "$outdir/vmgm.xml"
	echo '</dvdauthor>' >> "$outdir/vmgm.xml"
fi


myecho "[dvd-menu] ##############################"

## run dvdauthor?
if [ "$no_dvdauthor" -eq 0 ] ; then
	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then  # VCD
		myecho "[dvd-menu] Running vcdxbuild..."
		vcdxbuild -c "$outdir"/videocd.cue -b "$outdir"/videocd.bin -p "$outdir/vmgm.xml" 
		if [ $? -ne 0 ] ; then
			## vcdxbuild errored
			myecho "[dvd-menu] ERROR during vcdxbuild execution!"
			myecho "[dvd-menu] see $logfile for details"
			exit 1
		else
			myecho "[dvd-menu] vcdxbuild ran ok. See logfile for details."
		fi
	else
		myecho "[dvd-menu] Running dvdauthor..."
#		dvdauthor -o "$dvd_dir" -x "$outdir/vmgm.xml" >> "$logfile" 2>&1
		dvdauthor -o "$dvd_dir" -x "$outdir/vmgm.xml" 2>> "$logfile"
		if [ $? -ne 0 ] ; then
			## dvdauthor errored
			myecho "[dvd-menu] ERROR during dvdauthor execution!"
			myecho "[dvd-menu] see $logfile for details"
			exit 1
		else
			myecho "[dvd-menu] dvdauthor ran ok. See logfile for details."
		fi
	fi
	# create iso image?
	if [ "$iso" -eq 1 ] ; then
		## create iso image afterward:
		if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then  # VCD
			echo "supposed to create vcd image"
		else
			myecho "[dvd-menu] Creating iso image dvd.iso"
			genisoimage -dvd-video -udf -o dvd.iso "$dvd_dir" 
			if [ $? -ne 0 ] ; then
				## genisoimage errored
				myecho "[dvd-menu] ERROR during genisoimage execution!"
				myecho "[dvd-menu] see $logfile for details"
				exit 1
			fi
		fi
	fi
else
	if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then  # VCD
		myecho "[dvd-menu] Not running vcdimager.  Edit the vmgm.xml file"
		myecho "[dvd-menu] if you wish, and then run vcdimager: "
		myecho "[dvd-menu] vcdimager vmgm.xml"
		echo " "
	else
		myecho "[dvd-menu] Not running dvdauthor.  Edit the vmgm.xml file"
		myecho "[dvd-menu] if you wish, and then run dvdauthor: "
		myecho "[dvd-menu] dvdauthor -x vmgm.xml"
		echo " "
	fi
fi

if [ "$nocleanup" -eq 0 ] ; then
	cleanup
fi
# cleanup the logfile a little bit:
sed -e '/ bytes of data written[[:cntrl:]]INFO/d' "$logfile" > "$outdir"/tmp.txt
mv "$outdir"/tmp.txt "$logfile"

export VIDEO_FORMAT="$original_video_format"

echo "[dvd-menu] Output directory is $outdir"
echo "[dvd-menu] Logfile output is at $logfile"

if [ "$vcd" -eq 1 ] || [ "$svcd" -eq 1 ] ; then
	echo "[dvd-menu] The vcd output is "
	echo "[dvd-menu] "
	echo "[dvd-menu] You can play this video using one of the following commands:"
	echo "[dvd-menu] xine -g vcd:\"$dvd_dir\""
	echo "[dvd-menu] "
	echo "[dvd-menu] or, if you just want to watch the videos without the menus:"
	echo "[dvd-menu] mplayer -sid 0 vcd:// -dvd-device $dvd_dir"
	echo "[dvd-menu] "
	echo "[dvd-menu] Burn the video cd using:"
	echo "[dvd-menu] cdrdao write --device ATA:1,0,0 videocd.cue"
else
	echo "[dvd-menu] The dvd filesystem is $dvd_dir"
	echo "[dvd-menu] "
	echo "[dvd-menu] You can play this video using one of the following commands:"
	echo "[dvd-menu] totem $dvd_dir"
	echo "[dvd-menu] vlc dvd://$dvd_dir"
	echo "[dvd-menu] gxine -S dvd:\"$dvd_dir\""
	echo "[dvd-menu] xine -g -u 0 dvd:\"$dvd_dir\""
	echo "[dvd-menu] "
fi
myecho "[dvd-menu] Done!"
echo