#!/bin/bash

#############################################################
##                                                         ##
##     Dieses Script installiert das Futaba mdm166a        ##
##     Display in Ubuntu 16.04 automatisch                 ##
##                                                         ##
##                                                         ##
##     während der Installation erscheint nur ein Fenster  ##
##     wo man mit Ja bestätigen muss.                      ##
##                                                         ##
##                                                         ##
##     Die Installation benötigt je nach PC ca. 9 Minuten  ## 
##                                                         ##
##                                                         ##
##     Nach der Installation muss, zur Verwendung in Kodi  ##
##     noch zwingend das Addon XBMC LCDproc installiert    ##
##     werden.                                             ##
##                                                         ##
##                                                         ##
#############################################################
#
#
#### Update machen ####
#	 
set -x
sudo apt-get update &&
sudo apt-get -y upgrade
set +x
#
### Vorbereitung zur Installation ###
#
set -x
sudo apt-get -y install git-core build-essential lcdproc  &&
sudo apt-get -y install libusb-dev &&
set +x
#
### libhid herunterladen,entpacken und configurieren ###
#
set -x   
wget http://repository.timesys.com/buildsources/l/libhid/libhid-0.2.16/libhid-0.2.16.tar.gz &&
tar xzvf libhid-0.2.16.tar.gz &&
cd libhid-0.2.16 &&
./configure --disable-werror &&
make; sudo make install &&
#
cd
set +x
#
### lcdproc herunterladen ,entpacken und configurieren ###
#
set -x
wget http://sourceforge.net/projects/lcdproc/files/latest/download/lcdproc-0.5.7.tar.gz	
tar xzvf lcdproc-0.5.7.tar.gz
cd lcdproc-0.5.7
./configure --enable-drivers=mdm166a
cd server/drivers
make
sudo cp mdm166a.so /usr/lib/x86_64-linux-gnu/lcdproc/
cd
set +x
#
set -x
#
### LCDd.conf bearbeiten ###
#
sudo rm /etc/LCDd.conf
#
#
touch LCDd.conf
#
echo "# LCDd.conf -- configuration file for the LCDproc server daemon LCDd
#
# This file contains the configuration for the LCDd server.
#
# The format is ini-file-like. It is divided into sections that start at
# markers that look like [section]. Comments are all line-based comments,
# and are lines that start with '#' or ';'.
#
# The server has a 'central' section named [server]. For the menu there is
# a section called [menu]. Further each driver has a section which
# defines how the driver acts.
#
# The drivers are activated by specifying them in a driver= line in the
# server section, like:
#
#   Driver=curses
#
# This tells LCDd to use the curses driver.
# The first driver that is loaded and is capable of output defines the
# size of the display. The default driver to use is curses.
# If the driver is specified using the -d <driver> command line option,
# the Driver= options in the config file are ignored.
#
# The drivers read their own options from the respective sections.



## Server section with all kinds of settings for the LCDd server ##
[server]

# Where can we find the driver modules ?
# IMPORTANT: Make sure to change this setting to reflect your
#            specific setup! Otherwise LCDd won't be able to find
#            the driver modules and will thus not be able to
#            function properly.
# NOTE: Always place a slash as last character !
DriverPath=/usr/lib/x86_64-linux-gnu/lcdproc/

# Tells the server to load the given drivers. Multiple lines can be given.
# The name of the driver is case sensitive and determines the section
# where to look for further configuration options of the specific driver
# as well as the name of the dynamic driver module to load at runtime.
# The latter one can be changed by giving a File= directive in the
# driver specific section.
#
# The following drivers are supported:
#   bayrad, CFontz, CFontzPacket, curses, CwLnx, ea65, EyeboxOne, g15, glcd,
#   glcdlib, glk, hd44780, icp_a106, imon, imonlcd,, IOWarrior, irman, joy,
#   lb216, lcdm001, lcterm, lirc, lis, MD8800,, mdm166a, ms6931, mtc_s16209x,
#   MtxOrb, mx5000, NoritakeVFD, picolcd,, pyramid, rawserial, sdeclcd,
#   sed1330, sed1520, serialPOS, serialVFD, shuttleVFD, sli,, stv5730, svga,
#   t6963, text, tyan, ula200, vlsys_m428, xosd
Driver=mdm166a

# Tells the driver to bind to the given interface. [default: 127.0.0.1]
Bind=127.0.0.1

# Listen on this specified port. [default: 13666]
Port=13666

# Sets the reporting level; defaults to warnings and errors only.
# [default: 2; legal: 0-5]
#ReportLevel=3

# Should we report to syslog instead of stderr? [default: no; legal: yes, no]
ReportToSyslog=yes

# User to run as.  LCDd will drop its root privileges and run as this user
# instead. [default: nobody]
#User=nobody

# The server will stay in the foreground if set to yes.
# [default: no, legal: yes, no]
#Foreground=no

# Hello message: each entry represents a display line; default: builtin
#Hello="  Willkommen"
#Hello="   LCDproc!"

# GoodBye message: each entry represents a display line; default: builtin
#GoodBye="Thanks for using"
#GoodBye="   LCDproc!"

# Sets the default time in seconds to displays a screen. [default: 4]
WaitTime=5

# If set to no, LCDd will start with screen rotation disabled. This has the
# same effect as if the ToggleRotateKey had been pressed. Rotation will start
# if the ToggleRotateKey is pressed. Note that this setting does not turn off
# priority sorting of screens. [default: on; legal: on, off]
#AutoRotate=no

# If yes, the the serverscreen will be rotated as a usual info screen. If no,
# it will be a background screen, only visible when no other screens are
# active. The special value 'blank' is similar to no, but only a blank screen
# is displayed. [default: on; legal: on, off, blank]
ServerScreen=blank

# Set master backlight setting. If set to 'open' a client may control the
# backlight for its own screens (only). [default: open; legal: off, open, on]
#Backlight=open

# Set master heartbeat setting. If set to 'open' a client may control the
# heartbeat for its own screens (only). [default: open; legal: off, open, on]
#Heartbeat=open

# set title scrolling speed [default: 10; legal: 0-10]
#TitleSpeed=10

# The "...Key=" lines define what the server does with keypresses that
# don't go to any client. The ToggleRotateKey stops rotation of screens, while
# the PrevScreenKey and NextScreenKey go back / forward one screen (even if
# rotation is disabled.
# Assign the key string returned by the driver to the ...Key setting. These
# are the defaults:
ToggleRotateKey=Enter
PrevScreenKey=Left
NextScreenKey=Right
#ScrollUpKey=Up
#ScrollDownKey=Down


## The menu section. The menu is an internal LCDproc client. 
[menu]
# You can configure what keys the menu should use. Note that the MenuKey
# will be reserved exclusively, the others work in shared mode.

# Up to six keys are supported. The MenuKey (to enter and exit the menu), the
# EnterKey (to select values) and at least one movement keys are required.
# These are the default key assignments:
MenuKey=Escape
EnterKey=Enter
UpKey=Up
DownKey=Down
#LeftKey=Left
#RightKey=Right


### Driver sections are below this line, in alphabetical order  ##

## Futuba MDM166A Display ##
[mdm166a]
# Show self-running clock after LCDd shutdown
# Possible values: [default: no; legal: no, small, big]
Clock=big
# Dim display, no dimming gives full brightness [default: no, legal: yes, no]
Dimming=no
# Dim display in case LCDd is inactive [default: no, legal: yes, no]
OffDimming=yes 
##

# EOF" >LCDd.conf

#
#
sudo cp LCDd.conf /etc/ &&
#
#
unlink LCDd.conf
#
set +x
#
set -x
#
### rc.local bearbeiten ###
#
sudo rm /etc/rc.local
#
#
#
cat <<EOF >rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/etc/init.d/LCDd start
exit 0

EOF
#
#
#
sudo chmod +x rc.local /etc/ &&
#
#
#
sudo cp  rc.local /etc/ &&
#
#
#
unlink rc.local
#
set +x
#
#
#
sleep 25
#
set -x
#
sudo apt-get update -y &&
#
sudo apt-get -y upgrade
#
### sorces.list aktualisieren ###
#
sudo su -c "grep '^deb ' /etc/apt/sources.list | \
sed 's/^deb/deb-src/g' > /etc/apt/sources.list.d/deb-src.list"
#
sleep 25
#
sudo apt-get update -y &&
#
sleep 25
#
sudo apt-get -y build-dep lcdproc -y 
#
sleep 25
#
sudo apt-get update -y &&
#
sudo apt-get -y upgrade
#
set +x
#
set -x
sleep 25
#
sudo /etc/init.d/LCDd restart &&
set +x
#
#
set -x
sudo lcdproc
set +x


