#!/usr/bin/env bash

PROG_NAME="$0"
BASE_NAME=$(basename $PROG_NAME)
ARGS="$@"
VERSION="4.14-1.1.1"
INTERACTIVE=True
CONFIG=/boot/config.txt
CMDLINE=/boot/cmdline.txt
TITLE="Kali-Pi Touchscreen Setup  Tool ($BASE_NAME)"
ROTATION=0
ASK_TO_REBOOT=0
TARGET_HOMEDIR=$HOME
OVERLAY_MISSING=False 

function print_version() {
    echo "Kali-Pi TFT Config $VERSION"
    exit 0
}


function do_update() {
    if [ ! "$INTERACTIVE" = True ]; then
        if [[ $EUID -ne 0 ]]; then
            bail "kalipi-tft-config must be run as root. try: sudo re4son-pi-tft-setup"
        fi
    fi
    TEMP_FILE="/tmp/${BASE_NAME}"
    info UPDATE "Downloading update"
    wget -O ${TEMP_FILE} https://raw.githubusercontent.com/Re4son/RPi-Tweaks/master/kalipi-tft-config/kalipi-tft-config
    cp $TEMP_FILE $PROG_NAME
    chmod +x $PROG_NAME
    rm -f $TEMP_FILE
    echo "Replaced old version:"
    echo "${BASE_NAME} $VERSION"
    echo
    echo "With new version:"
    "$PROG_NAME" -v
    if [ "$INTERACTIVE" = True ]; then
        sleep 2 &&
        exec "$PROG_NAME"
    fi
    exit 0
}

function do_check_overlay() {
    if [ ! -z "$1" ]; then
        if [ -f /boot/overlays/$1.dtbo ]; then
            return 0
        fi
    fi
    OVERLAY_MISSING=True 
    return 1
}

function do_get_overlays() {
    local TEMP_DIR="/tmp/"
    local OVERLAYS_DIR="/boot/overlays"
    local OVERLAYS_URL="https://re4son-kernel.com/overlays/"
    if [ -d ${TEMP_DIR}/overlays ]; then
        rm -rf ${TEMP_DIR}/overlays
    fi
    if ! wget -P ${TEMP_DIR} -r -nH --no-parent -e robots=off -R "index.html*" ${OVERLAYS_URL}; then
        whiptail --msgbox "Could not update overlays, please try manually.\nDownload failed" 20 60 
        return 1
    fi
    if ! cp -f ${TEMP_DIR}/overlays/*.dtb* ${OVERLAYS_DIR}; then
        whiptail --msgbox "Could not update overlays, please try manually.\nInstallation failed" 20 60
    fi
    whiptail --msgbox "3rd Party overlays successfully updated." 20 60
    return 0
}
function remove_tft() {
    if [ "$1" = silent ]; then
        SILENT=True
    else
        SILENT=False
    fi
    if [ ! "$INTERACTIVE" = True ]; then
        info UPDATE "Removing TFT configuration"
    fi
    rm -f /etc/X11/xorg.conf.d/99-fbdev.conf
    rm -f /etc/X11/xorg.conf.d/99-calibration.conf
    rm -f /etc/pointercal
    rm -f /dev/input/touchscreen
    rm -f /etc/udev/rules.d/95-*.rules
    if [ -e ${TARGET_HOMEDIR}/99-fbturbo.conf ]; then
        mv ${TARGET_HOMEDIR}/99-fbturbo.conf /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    fi
    rm -f ${TARGET_HOMEDIR}/99-fbdev.conf
    rm -f ~/99-fbdev.conf
    rm -f /etc/modprobe.d/re4son.conf
    rm -f /usr/share/X11/xorg.conf.d/45-evdev.conf
    sed -i 's/rootwait fbcon=map:10 fbcon=font:VGA8x8/rootwait/g' "/boot/cmdline.txt"
    sed -i 's/4dpi.rotate=[0-9]* rootwait/rootwait/g' "/boot/cmdline.txt"
    sed -i 's/BLANK_TIME=0/BLANK_TIME=10/g' "/etc/kbd/config"
    sed -i '/added by re4son-pi-tft-setup/,/end re4son-pi-tft-setup/d' "/boot/config.txt"
    sed -i '/added by re4son-pi-tft-setup/,/end re4son-pi-tft-setup/d' "/etc/modules"
    sed -i '/added by re4son-pi-tft-setup/,/end re4son-pi-tft-setup/d' "${TARGET_HOMEDIR}/.profile"
    if [ -e ${TARGET_HOMEDIR}/.60-libinput.conf.bak ]; then
        mv ${TARGET_HOMEDIR}/.60-libinput.conf.bak /usr/share/X11/xorg.conf.d/60-libinput.conf
    fi
    systemctl stop hyperpixel4-init.service> /dev/null 2>&1
    systemctl disable hyperpixel4-init.service> /dev/null 2>&1
    systemctl stop hyperpixel-init.service> /dev/null 2>&1
    systemctl disable hyperpixel-init.service> /dev/null 2>&1
    systemctl stop hyperpixel-touch.service> /dev/null 2>&1
    systemctl disable hyperpixel-touch.service> /dev/null 2>&1
    # Do not delete hyperpixel service files so we can use them again
    # Do not delete 15-i2c-modprobe.rules and i2cprobe as they may come in handy later
    if [ "$INTERACTIVE" = True ]; then
        if [ "$SILENT" = False ]; then 
            whiptail --msgbox "Touchscreen configuration successfully removed." 20 60
            ASK_TO_REBOOT=1
        fi
    else
        info UPDATE "TFT config removed"
        echo
        read -p "Reboot to apply changes? (y/n): " -n 1 -r
        echo

        if [[ $REPLY =~ ^[Yy]$ ]]; then
            reboot
        fi
        exit 0
    fi
}

function print_help() {
    echo "Usage: $0 -t [pitfttype]"
    echo "    -h            Print this help"
    echo "    -v            Print version information"
    echo "    -u            Update Re4son Pi-TFT Setup"
    echo "    -r            Remove TFT configuration (undo setup)"
    echo "    -d [dir]      Specify path of user's home directory to back up and restore some files (defaults to /home/pi)"
    echo "    -t [type]     Specify the type of TFT:"
    echo "                                             '24r'      (Adafruit 2.4\" hat)"
    echo "                                             '28r'      (Adafruit 2.8\" resistive)"
    echo "                                             '28c'      (Adafruit 2.8\" capacitive)"
    echo "                                             '32r'      (Adafruit 3.2\")"
    echo "                                             '35r'      (Adafruit 3.5\")"
    echo "                                             '35ro'     (Adafruit 3.5\") Overclocked"
    echo "                                             '22'       (Adafruit 2.2\")"
    echo "                                             '4dpi'     (4D Systems 2.4\",3.2\" and 3.5\")"
    echo "                                             'elec22'   (Elecfreak 2.2\")"
    echo "                                             'hy28b'    (Hotmcu HY28B 2.8\")"
    echo "                                             'hyper35'    (HyperPixel touch 3.5\")"
    echo "                                             'hyper4t'  (HyperPixel touch 4.0\")"
    echo "                                             'jb35'     (JBTek 3.5\")"
    echo "                                             'kum35'    (Kuman 3.5\")"
    echo "                                             'kum35h'   (Kuman 3.5\" HDMI)"
    echo "                                             'pi70'     (Raspberry Pi 7\")"
    echo "                                             'sain32'   (Sainsmart 3.2\")"
    echo "                                             'sain35'   (Sainsmart 3.5\")"
    echo "                                             'wave32'   (Waveshare 3.2\")"
    echo "                                             'wave35'   (Waveshare 3.5\" (a))"
    echo "                                             'wave35b'  (Waveshare 3.5\" (b))"
    echo "                                             'wave35o'  (Waveshare 3.5\" Overclocked)"
    echo "                                             'wave35h'  (Waveshare 3.5\" HDMI)"
    echo "                                             'wave35c'  (Waveshare 3.5\" Clones, such as:"
    echo "                                                                         Elecrow 3.5\""
    echo "                                                                         KeDei 3.5\""
    echo "                                                                         Osoyoo 3.5\")"
    echo "                                             'wave40'   (Waveshare 4\")"
    echo "                                             'wave50'   (Waveshare 5\" HDMI)"
    echo
    echo "You must specify a type of display. Try \"wave35c\" if you are unsure what to use."
    exit 1
}

group=RE4SON
function info() {
    if [ ! "$INTERACTIVE" = True ]; then
        system="$1"
        group="${system}"
        shift
        FG="1;32m"
        BG="40m"
        echo -e "[\033[${FG}\033[${BG}${system}\033[0m] $*"
    fi
}

function bail() {
    FG="1;31m"
    BG="40m"
    echo -en "[\033[${FG}\033[${BG}${group}\033[0m] "
    if [ -z "$1" ]; then
        echo "Exiting due to error"
    else
        echo "Exiting due to error: $*"
    fi
    exit 1
}

function ask() {
    # http://djm.me/ask
    while true; do

        if [ "${2:-}" = "Y" ]; then
            prompt="Y/n"
            default=Y
        elif [ "${2:-}" = "N" ]; then
            prompt="y/N"
            default=N
        else
            prompt="y/n"
            default=
        fi

        # Ask the question
        read -p "$1 [$prompt] " REPLY

        # Default?
        if [ -z "$REPLY" ]; then
            REPLY=$default
        fi

        # Check if the reply is valid
        case "$REPLY" in
            Y*|y*) return 0 ;;
            N*|n*) return 1 ;;
        esac
    done
}

function check_bin(){
    BIN_PATH=$(which $1)
    if [ "" = "$BIN_PATH" ]; then
        echo 0
    else
        echo 1
    fi
}

function check_pkg(){
    PKG_STATUS=$(dpkg-query -W --showformat='${Status}\n' $1|grep "install ok installed")
    if [ "" = "$PKG_STATUS" ]; then
        echo 0
    else
        echo 1
    fi
}

function check_dependencies() {
    local PROGRAMS=("whiptail")
    local PACKAGES=("whiptail")
    local missingprogs=""
    local missingpkgs=""
    for i in "${!PROGRAMS[@]}"; do
        if [ $(check_bin ${PROGRAMS[$i]}) -eq 0 ]; then
            missingpkgs="${missingpkgs}${PACKAGES[$i]} "
        fi
    done
    if [ "" != "$missingpkgs" ]; then
        printf "\nThe following packages are missing:         $missingpkgs\n"
        printf "Please install them before continuing, i.e.:\n"
        printf "\nsudo apt install $missingpkgs\n\n"
        exit 1
    fi

    if grep -q boot /proc/mounts; then
        :
    else
	mount /boot
        if grep -q boot /proc/mounts; then
            :
        else
            whiptail --msgbox "\
Error: Cannot mount /boot.

If you're sure it is mounted, proceed - otherwise quit here and try:

sudo mount /dev/mmcblk0p1 /boot
" 20 70 1
        fi
    fi
}

function calc_wt_size() {
    # NOTE: it's tempting to redirect stderr to /dev/null, so supress error 
    # output from tput. However in this case, tput detects neither stdout or 
    # stderr is a tty and so only gives default 80, 24 values
    WT_HEIGHT=17
    WT_WIDTH=$(tput cols)

    if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 100 ]; then
        WT_WIDTH=90
    elif [ "$WT_WIDTH" -gt 178 ]; then
        WT_WIDTH=120
    else
        WT_WIDTH=$((WT_WIDTH-10))
    fi
    WT_MENU_HEIGHT=$(($WT_HEIGHT-7))
}

function do_about() {
    whiptail --msgbox "\
kalipi-tft-config version $VERSION

This tool provides a straight-forward way of setting
up various TFT screens for the Kali-Pi.
" 20 70 1
}
function install_xserver-xorg-input-evdev {
## Debian releases after early 2017 break touch input - this will fix it
    info PI-TFT "Checking for xserver-xorg-input-evdev:"
    PKG_STATUS=$(dpkg-query -W --showformat='${Status}\n' xserver-xorg-input-evdev|grep "install ok installed")
    if [ "" == "$PKG_STATUS" ]; then
        info PI-TFT "**** Installing xserver-xorg-input-evdev package ****"
        info PI-TFT "No xserver-xorg-input-evdev. Installing it now."
        apt update
        apt install -y xserver-xorg-input-evdev
    fi
    if [ ! -f /usr/share/X11/xorg.conf.d/45-evdev.conf ]; then
            info PI-TFT "Creating /usr/share/X11/xorg.conf.d/45-evdev.conf"
            ln -s /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/45-evdev.conf
    fi
    info PI-TFT "**** xserver-xorg-input-evdev package installed ****"
}

function update_xorg_conf() {
    mkdir -p /etc/X11/xorg.conf.d
    cat > /etc/X11/xorg.conf.d/99-fbdev.conf <<EOF
Section "Device"
  Identifier "myfb"
  Driver "fbdev"
  Option "fbdev" "$FRAMEBUFFER"
EndSection
EOF
}

function update_fbturbo_conf() {
    local fbturbo_path="/usr/share/X11/xorg.conf.d/99-fbturbo.conf"
    if [ -e $fbturbo_path ]; then
        info PI-TFT "Moving ${fbturbo_path} to ${TARGET_HOMEDIR}"
        mv "$fbturbo_path" "$TARGET_HOMEDIR"
    fi
}

function update_profile() {
    if grep -q "export FRAMEBUFFER=/dev/fb" "${TARGET_HOMEDIR}/.profile"; then
        if [ ${FRAMEBUFFER} == "/dev/fb0" ]; then
            sed "${TARGET_HOMEDIR}/.profile" -i -e "s/export FRAMEBUFFER=\/dev\/fb./export FRAMEBUFFER=\/dev\/fb0/"
        elif [ ${FRAMEBUFFER} == "/dev/fb1" ]; then
            sed "${TARGET_HOMEDIR}/.profile" -i -e "s/export FRAMEBUFFER=\/dev\/fb./export FRAMEBUFFER=\/dev\/fb1/"
        fi
    fi
}

function install_console() {
    if ! grep -q 'fbcon=map:10 fbcon=font:VGA8x8' /boot/cmdline.txt; then
        info PI-TFT "Updating /boot/cmdline.txt"
        sed -i 's/rootwait/rootwait fbcon=map:10 fbcon=font:VGA8x8/g' "/boot/cmdline.txt"
    else
        info PI-TFT "/boot/cmdline.txt already updated"
    fi
    if [ ! -f /etc/kbd/config ]; then
        info PI-TFT "Creating /etc/kbd/config"
        mkdir -p /etc/kbd
        touch /etc/kbd/config
    fi
    sed -i 's/BLANK_TIME=.*/BLANK_TIME=0/g' "/etc/kbd/config"
}

function uninstall_console() {
    sed -i 's/rootwait fbcon=map:10 fbcon=font:VGA8x8/rootwait/g' "/boot/cmdline.txt"
    sed -i 's/BLANK_TIME=0/BLANK_TIME=10/g' "/etc/kbd/config"
    info PI-TFT "Screen blanking time reset to 10 minutes"
}

function ask_console() {
    if [ ! "$INTERACTIVE" = True ]; then
        if ask "Would you like the console to appear on the PiTFT display?" "Y"; then
            info PI-TFT "Updating console to PiTFT..."
            install_console || bail "Unable to configure console"
        else
            info PI-TFT "Making sure console doesn't use PiTFT"
            uninstall_console || bail "Unable to configure console"
        fi
    else 
        install_console || bail "Unable to configure console"
    fi
}

function do_install_onoffbutton() {
    info PI-TFT  "Adding rpi_power_switch to /etc/modules"
    if [ "$INTERACTIVE" = True ]; then
        whiptail --yesno "Would you like GPIO #27 to act as a on/off button?" 20 60 2
        if [ $? -ne 0 ]; then 
            return 0 
        fi
    else
        if ask "Would you like GPIO #27 to act as a on/off button?"; then
            :
        else
            return 0
        fi
    fi
    if grep -xq "rpi_power_switch" "${chr}/etc/modules"; then
        info PI-TFT "Already had rpi_power_switch"
    else
        info PI-TFT "Adding rpi_power_switch"
        date=`date`
        cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
rpi_power_switch
# --- end re4son-pi-tft-setup $date ---
EOF
    fi

    info PI-TFT "Adding rpi_power_switch config to /etc/modprobe.d/re4son.conf"
    if grep -xq "options rpi_power_switch gpio_pin=27 mode=0" "${chr}/etc/modprobe.d/re4son.conf"; then
        info PI-TFT "Already had rpi_power_switch config"
    else
        info PI-TFT "Adding rpi_power_switch"
        date=`date`
        cat >> /etc/modprobe.d/re4son.conf <<EOF
# --- added by re4son-pi-tft-setup $date ---
options rpi_power_switch gpio_pin=27 mode=0
# --- end re4son-pi-tft-setup $date ---
EOF
    fi
}

function do_reboot() {
    if [ "$INTERACTIVE" = True ]; then
        ASK_TO_REBOOT=1
    else
        if ask "Reboot to apply changes?" "Y"; then
            reboot
        fi 
    fi
}
    
##################################################################
##                                                              ##
##           Screen Configuration Sections                      ##

function do_install_22() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft22,rotate=${ROTATION},speed=32000000,fps=20"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    ##info PI-TFT "Updating X11 default calibration..."

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    ##info PI-TFT "Updating TSLib default calibration..."

    # update /etc/udev/rules.d/95-xxxxx.rules
    ##info PI-TFT "Updating udev rules for Touchscreen..."

    # update /etc/modules
    ##info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # Install on/off button
    #do_install_onoffbutton

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_28c() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft28c,rotate=${ROTATION},speed=64000000,fps=60"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
         Identifier "captouch"
         MatchProduct "ft6x06_ts"
         Option "SwapAxes" "1"
         Option "InvertY" "0"
         Option "Calibration" "0 320 240 0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
         Identifier "captouch"
         MatchProduct "ft6x06_ts"
         Option "SwapAxes" "1"
         Option "InvertY" "1"
         Option "Calibration" "0 320 0 240"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
         Identifier "captouch"
         MatchProduct "ft6x06_ts"
         Option "SwapAxes" "1"
         Option "InvertY" "0"
         Option "Calibration" "320 0 0 240"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
         Identifier "captouch"
         MatchProduct "ft6x06_ts"
         Option "SwapAxes" "1"
         Option "InvertY" "1"
         Option "Calibration" "320 0 240 0"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="-54855 -2746 14927070 1699 -67111 20400204 65536 240 320"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="320 65536 0 -65536 0 15728640 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="56011 699 189872 -2778 68277 -159244 65536 240 320"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-1250 -65726 21361704 56035 1848 278316 65536 320 240"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ft6206.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ft6x06_ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    date=`date`
        cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
ft6x06_ts
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # Install on/off button
    #do_install_onoffbutton

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_24r() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft28-resistive,rotate=${ROTATION},speed=64000000,fps=30"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 3800 200"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 200 3800"
        Option  "SwapAxes"      "1"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 200 3800"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 3800 200"
        Option  "SwapAxes"      "1"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="4232 11 -879396 1 5786 -752768 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="33 -5782 21364572 4221 35 -1006432 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-4273 61 16441290 4 -5772 21627524 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-9 5786 -784608 -4302 19 16620508 65536"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    date=`date`
    cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
stmpe_ts
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # Install on/off button
    #do_install_onoffbutton

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_28r() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft28-resistive,rotate=${ROTATION},speed=64000000,fps=30"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 3800 200"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 200 3800"
        Option  "SwapAxes"      "1"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 200 3800"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 3800 200"
        Option  "SwapAxes"      "1"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="4232 11 -879396 1 5786 -752768 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="33 -5782 21364572 4221 35 -1006432 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-4273 61 16441290 4 -5772 21627524 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-9 5786 -784608 -4302 19 16620508 65536"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    date=`date`
    cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
stmpe_ts
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # Install on/off button
    #do_install_onoffbutton

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_32r() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft28-resistive,rotate=${ROTATION},speed=64000000,fps=30"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 3800 200"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 200 200 3800"
        Option  "SwapAxes"      "1"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 200 3800"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "200 3800 3800 200"
        Option  "SwapAxes"      "1"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="4232 11 -879396 1 5786 -752768 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="33 -5782 21364572 4221 35 -1006432 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-4273 61 16441290 4 -5772 21627524 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-9 5786 -784608 -4302 19 16620508 65536"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    date=`date`
    cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
stmpe_ts
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # Install on/off button
    #do_install_onoffbutton

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_35r() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=pitft35-resistive,rotate=${ROTATION},speed=20000000,fps=20"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "120 3800 200 3900"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "120 3800 3900 200"
        Option  "SwapAxes"      "1"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 120 3900 200"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "stmpe-ts"
        Option  "Calibration"   "3800 120 200 3900"
        Option  "SwapAxes"      "1"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="5724 -6 -1330074 26 8427 -1034528 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="5 8425 -978304 -5747 61 22119468 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-5682 -1 22069150 13 -8452 32437698 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="3 -8466 32440206 5703 -1 -1308696 65536"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    date=`date`
    cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
stmpe_ts
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_4dpi() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration" "3933 227 241 3893"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    cat > /etc/pointercal <<EOF
5803 131 -2411836 53 4296 -937452 65536
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-AR1020.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="AR1020 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
spi-bcm2835_dma
i2c-dev
pwmbl
ar1020-i2c
#4dpi_touch
4dpi
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console
    if [ "$ROTATION" = 0 ]; then
        sed -i 's/rootwait/4dpi.rotate=0 rootwait/g' "/boot/cmdline.txt"
    elif [ "$ROTATION" = 90 ]; then
        sed -i 's/rootwait/4dpi.rotate=90 rootwait/g' "/boot/cmdline.txt"
    elif [ "$ROTATION" = 180 ]; then
        sed -i 's/rootwait/4dpi.rotate=180 rootwait/g' "/boot/cmdline.txt"
    elif [ "$ROTATION" = 270 ]; then
        sed -i 's/rootwait/4dpi.rotate=270 rootwait/g' "/boot/cmdline.txt"
    fi

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_elec22() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=elec22"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    ##info PI-TFT "Updating X11 default calibration..."

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    ##info PI-TFT "Updating TSLib default calibration..."

    # update /etc/udev/rules.d/95-xxxxx.rules
    ##info PI-TFT "Updating udev rules for Touchscreen..."

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_ecrow35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "3933 227 3893 241"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 3893 241"
        Option  "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_hy28b() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=hy28b,rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7843 Touchscreen"
        Option  "Calibration"   "3800 200 3800 200"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7843 Touchscreen"
        Option  "Calibration"   "3800 200 200 3800"
        Option  "SwapAxes"      "1"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7843 Touchscreen"
        Option  "Calibration"   "200 3800 200 3800"
        Option  "SwapAxes"      "0"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7843 Touchscreen"
        Option  "Calibration"   "200 3800 3800 200"
        Option  "SwapAxes"      "1"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="4232 11 -879396 1 5786 -752768 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="33 -5782 21364572 4221 35 -1006432 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-4273 61 16441290 4 -5772 21627524 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-9 5786 -784608 -4302 19 16620508 65536"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7843 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_hyper35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=hyperpixel"
    overlay2="dtoverlay=hyperpixel-gpio-backlight"

    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
overscan_left=0
overscan_right=0
overscan_top=0
overscan_bottom=0
framebuffer_width=800
framebuffer_height=480
enable_dpi_lcd=1
display_default_lcd=1
dpi_group=2
dpi_mode=87
dpi_output_format=0x6f016
display_rotate=2
hdmi_timings=800 0 50 20 50 480 1 3 2 3 0 0 0 60 0 32000000 6

# Use a basic GPIO backlight driver with on/off support
$overlay2
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    if [ $(check_pkg python-evdev) -eq 0 ]; then
        apt install -y python-evdev
    fi

    if [ $(check_pkg python-smbus) -eq 0 ]; then
        apt install -y python-smbus
    fi

    # install hyperpixel init service
    info PI-TFT "Installing HyperPixel init service"
    if [ ! -f /usr/bin/hyperpixel-init ]; then
        wget -O /usr/bin/hyperpixel-init https://raw.githubusercontent.com/pimoroni/hyperpixel4/pi3/dist/hyperpixel4-init
	if [ ! $? == 0 ]; then
            whiptail --msgbox "\
Error:
Failure to download the "hyperpixel-init" service file.
Please check your Internet connection and try again.
" 20 70 1
            rm -f /usr/bin/hyperpixel-init
            return 1
        fi
    fi
    chmod 755 /usr/bin/hyperpixel-init
    if [ ! -f /etc/systemd/system/hyperpixel-init.service ]; then
        cat >> /etc/systemd/system/hyperpixel-init.service <<EOF
[Unit]
Description=Hyperpixel LCD Display Initialization
DefaultDependencies=no
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/hyperpixel-init

[Install]
WantedBy=multi-user.target
EOF
    fi
    systemctl enable hyperpixel-init.service
    systemctl start hyperpixel-init.service

    # install hyperpixel touch service
    info PI-TFT "Installing HyperPixel touch service"
    if [ ! -f /usr/bin/hyperpixel-touch ]; then
        wget -O /usr/bin/hyperpixel-touch https://github.com/pimoroni/hyperpixel/raw/master/requirements/usr/bin/hyperpixel-touch
	if [ ! $? == 0 ]; then
            whiptail --msgbox "\
Error:
Failure to download the "hyperpixel-touch" service file.
Please check your Internet connection and try again.
" 20 70 1
            rm -f /usr/bin/hyperpixel-touch
            return 1
        fi
    fi
    chmod 755 /usr/bin/hyperpixel-touch
    if [ ! -f /etc/systemd/system/hyperpixel-touch.service ]; then
        cat >> /etc/systemd/system/hyperpixel-touch.service <<EOF
[Unit]
Description=Hyperpixel LCD Touch Screen Driver Daemon
DefaultDependencies=no
After=local-fs.target

[Service]
ExecStart=/usr/bin/hyperpixel-touch
Restart=on-failure
Type=forking
PIDFile=/var/run/hyperpixel-touch.pid

[Install]
WantedBy=multi-user.target
EOF
    fi
    systemctl enable hyperpixel-touch.service
    systemctl start hyperpixel-touch.service

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="107331 -724 1595748 -1133 37223 1482024 65536 800 480 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="108299 -918 -190660 -408 38342 -381668 65536 800 480 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    ##info PI-TFT "Updating udev rules for Touchscreen..."


    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."
    if ! grep -q "^i2c[-_]dev" /etc/modules; then
        cat >> /etc/modules <<EOF
# --- added by re4son-pi-tft-setup $date ---
i2c-dev
# --- end re4son-pi-tft-setup $date ---
EOF
    sudo modprobe -a i2c-dev
fi

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0yt
    fi
    
}

function do_install_hyper4t() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 180 ]; then
        overlay="dtoverlay=hyperpixel4-touch:rotate"
        rotate="display_rotate=1"
    else
        overlay="dtoverlay=hyperpixel4-touch"
        rotate="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
overscan_left=0
overscan_right=0
overscan_top=0
overscan_bottom=0
framebuffer_width=800
framebuffer_height=480
enable_dpi_lcd=1
display_default_lcd=1
dpi_group=2
dpi_mode=87
dpi_output_format=0x7f216
$rotate
hdmi_timings=480 0 10 16 59 800 0 15 113 15 0 0 0 60 0 32000000 6
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # install hyperpixel4 init service
    info PI-TFT "Installing HyperPixel 4 service"
    if [ ! -f /usr/bin/hyperpixel4-init ]; then
        wget -O /usr/bin/hyperpixel4-init https://raw.githubusercontent.com/pimoroni/hyperpixel4/pi3/dist/hyperpixel4-init
	if [ ! $? == 0 ]; then
            whiptail --msgbox "\
Error:
Failure to download the "hyperpixel4-init" service file.
Please check your Internet connection and try again.
" 20 70 1
            rm -f /usr/bin/hyperpixel4-init
            return 1
        fi
    fi
    chmod 755 /usr/bin/hyperpixel4-init
    if [ ! -f /etc/systemd/system/hyperpixel4-init.service ]; then
        cat >> /etc/systemd/system/hyperpixel4-init.service <<EOF
[Unit]
Description=HyperPixel 4.0" LCD Display Initialization
DefaultDependencies=no
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/hyperpixel4-init

[Install]
WantedBy=multi-user.target
EOF
    fi
    systemctl enable hyperpixel4-init.service
    systemctl start hyperpixel4-init.service

    # install Raspberry Pi udev workaround:
    # The normal udev rule only looks up the i2c alias, so the compatible string would have to say "<something>,GDIX1001" and the distinction about which specific device is required would be lost.
    # Raspbian uses a workaround: a custom shell command and a udev rule to run it. The shell command calls modprobe using the i2c alias, but if that fails it tries the device tree alias instead.
    info PI-TFT "Installing udev rule 15-i2c-modprobe.rules for automatic detection"
    if [ ! -f /usr/lib/kalipi-sys-mods/i2cprobe ]; then
        mkdir -p /usr/lib/kalipi-sys-mods/
        cat > /usr/lib/kalipi-sys-mods/i2cprobe <<EOF
#!/bin/sh
modprobe ${MODALIAS} || modprobe "of:N${OF_NAME}T<NULL>C\${OF_COMPATIBLE_0}"
EOF

    chmod 755 /usr/lib/kalipi-sys-mods/i2cprobe
    fi

    if [ ! -f /lib/udev/rules.d/15-i2c-modprobe.rules ]; then 
        cat >> /lib/udev/rules.d/15-i2c-modprobe.rules <<EOF
SUBSYSTEM=="i2c|spi", ENV{MODALIAS}=="?*", ENV{OF_NAME}=="?*", ENV{OF_COMPATIBLE_0}=="?*", RUN+="/usr/lib/kalipi-sys-mods/i2cprobe"
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="107331 -724 1595748 -1133 37223 1482024 65536 800 480 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="108299 -918 -190660 -408 38342 -381668 65536 800 480 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-stmpe.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="Goodix Capacitive TouchScreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF



    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_jb35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=jbtek"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    ##info PI-TFT "Updating X11 default calibration..."

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    ##info PI-TFT "Updating TSLib default calibration..."

    # update /etc/udev/rules.d/95-xxxxx.rules

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay jbtek

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kedei35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration" "3932 227 3893 241"
        Option "EmulateThirdButton" "0"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 3893 241"
        Option  "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "3933 227 3893 241"
        Option "EmulateThirdButton" "0"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 3893 241"
        Option  "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35h() {
    # Kuman 35" HDMI at 480 x 320
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=87
hdmi_mode=87
hdmi_cvt 480 320 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=2
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 3939 183"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 3939 183"
	Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="49 -8339 32800020 -5522 -71 21892008 65536 480 320 0"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="-5515 -53 21792490 -53 8379 -1552476 65536 320 480 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="24 8434 -2066100 5567 36 -1003052 65536 480 320 0"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="5487 96 -928370 -69 -8394 33161326 65536 320 480 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35h6() {
    # Kuman 35" HDMI at 600 x 400
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=87
hdmi_mode=87
hdmi_cvt 600 400 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=2
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 3939 183"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 3939 183"
	Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="111 -10494 40991504 -6950 -30 27367770 65536 600 400 0"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="-7103 29 27716298 -42 10658 -2279772 65536 400 600 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="99 10556 -2282908 7074 -9 -1398644 65536 600 400 0"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="6857 124 -1416342 85 -10531 41099732 65536 400 600 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35h7() {
    # Kuman 35" HDMI at 720 x 480
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=87
hdmi_mode=87
hdmi_cvt 720 480 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=2
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 3939 183"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 3939 183"
	Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

	# update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="-101 -12810 50063088 -8449 42 32757136 65536 720 480 0"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="-8317 -8 32721028 -70 12572 -2255128 65536 480 720 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-23 12526 -2131056 8255 -5 -966036 65536 720 480 0"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="8430 96 -1631276 24 -12683 49434504 65536 480 720 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35h8() {
    # Kuman 35" HDMI at 810 x 540
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=87
hdmi_mode=87
hdmi_cvt 810 540 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=2
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 3939 183"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 3939 183"
	Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="97 -14209 55526076 -9389 -75 37065468 65536 808 540 0"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="-9364 -45 36864664 50 14104 -2863648 65536 540 808 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="5 14257 -2927936 9424 6 -1514748 65536 808 540 0"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="9542 95 -2116512 -38 -14143 55558220 65536 540 808 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_kum35h9() {
    # Kuman 35" HDMI at 960 x 640
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=87
hdmi_mode=87
hdmi_cvt 960 640 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=2
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 3939 183"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "3945 233 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 183 3939"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration"   "233 3945 3939 183"
	Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="-148 -16973 66660160 -11185 -227 43960616 65536 960 640 0"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="-11344 -102 44311648 97 16927 -3730000 65536 640 960 0"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="103 16972 -3599688 11287 10 -1907544 65536 960 640 0"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="11386 174 -2711920 24 -16637 65922712 65536 640 960 0"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}


function do_install_oso35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "3933 327 3893 241"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 3893 241"
        Option  "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_pi70() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        :
    elif [ "$ROTATION" = 90 ]; then
        cat >>  $CONFIG <<EOF
# --- added by re4son-pi-tft-setup $date ---
display_rotate=1
# --- end re4son-pi-tft-setup $date ---
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat >>  $CONFIG <<EOF
# --- added by re4son-pi-tft-setup $date ---
lcd_rotate=2
# --- end re4son-pi-tft-setup $date ---
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat >>  $CONFIG <<EOF
# --- added by re4son-pi-tft-setup $date ---
display_rotate=3
# --- end re4son-pi-tft-setup $date ---
EOF
    fi

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    cat > /etc/pointercal <<EOF
66242 1508 -591604 531 67406 -647360 65536 800 480
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ft5406.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="FT5406 memory based driver", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_sain32() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=sainsmart32"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    #info PI-TFT "Updating X11 default calibration..."

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    #info PI-TFT "Updating TSLib default calibration..."

    # update /etc/udev/rules.d/95-xxxxx.rules
    #info PI-TFT "Updating udev rules for Touchscreen..."

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay sainsmart32

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_sain35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration" "3933 227 3893 241"
        Option "EmulateThirdButton" "0"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 3893 241"
        Option  "SwapAxes"      "1"
        Option  "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave32() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare32b:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "160 3723 181 3896"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "160 3723 3896 181"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3723 160 3896 181"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3723 160 181 3896"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare32b

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave35() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "3933 227 3893 241"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3933 227 241 3893"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "227 3933 241 3893"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "227 3933 3893 241"
        Option "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35a

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave35b() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35b:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "1"
        Option "Calibration" "3933 227 3893 241"
        Option "EmulateThirdButton" "0"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "3933 227 241 3893"
        Option "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "227 3933  3893 241"
        Option "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "227 3933 241 3893"
        Option "SwapAxes"      "1"
        Option "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # check overlay
    do_check_overlay waveshare35b

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave35h() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=3"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=1
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "190 3991 3895 243"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "190 3991 243 3895"
        Option "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "3991 190 243 3895"
        Option "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option "Calibration"   "3991 190 3895 243"
        Option "SwapAxes"      "1"
        Option "InvertY"       "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave40() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb1"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    overlay="dtoverlay=waveshare35a:rotate=${ROTATION}"
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
$overlay
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier "calibration"
        MatchProduct "ADS7846 Touchscreen"
        Option "SwapAxes" "0"
        Option "Calibration" "3950 172 194 3871"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3950 172 3871 194"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "172 3950 3871 194"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "172 390 194 3871"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="8 -8432 32432138 5699 -112 -965922 65536"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="-36 8394 -1871700 -5554 -26 21789796 65536 480 320"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

function do_install_wave50() {
    # set framebuffer
    FRAMEBUFFER="/dev/fb0"

    # update /boot/config.txt
    info PI-TFT "Updating /boot/config.txt..."
    date=`date`
    if [ "$ROTATION" = 0 ]; then
        overlay="display_rotate=0"
    elif [ "$ROTATION" = 90 ]; then
        overlay="display_rotate=1"
    elif [ "$ROTATION" = 180 ]; then
        overlay="display_rotate=2"
    elif [ "$ROTATION" = 270 ]; then
        overlay="display_rotate=2"
    fi
    cat >> /boot/config.txt <<EOF
# --- added by re4son-pi-tft-setup $date ---
dtparam=spi=on
dtparam=i2c1=on
dtparam=i2c_arm=on
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0
dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900
hdmi_drive=1
$overlay
hdmi_force_hotplug=1
###########################################
####  Overclocking the micro sdcard    ####
#### Uncomment  84 for Raspberry Pi 2  ####
# dtparam=sd_overclock=84
#### Uncomment 100 for Raspberry Pi 3  ####
# dtparam=sd_overclock=100
###########################################
# --- end re4son-pi-tft-setup $date ---
EOF

    # update /etc/X11/xorg.conf.d
    info PI-TFT "Updating X11 configuration..."
    update_xorg_conf

    # update /etc/X11/xorg.conf.d/99-calibration.conf
    info PI-TFT "Updating X11 default calibration..."
    if [ "$ROTATION" = 0 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "138 3952 214 4002"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 90 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "138 3952 4002 214"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 180 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3952 138 214 4002"
        Option  "SwapAxes"      "0"
        Option "InvertY" "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    elif [ "$ROTATION" = 270 ]; then
        cat > /etc/X11/xorg.conf.d/99-calibration.conf <<EOF
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3952 138 4002 214"
        Option  "SwapAxes"      "0"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF
    fi

    # moving /usr/share/X11/xorg.conf.d/99-fbturbo.conf
    info PI-TFT "Removing X11 fbturbo.conf..."
    update_fbturbo_conf

    # update ~/.profile
    info PI-TFT "Updating user profile..."
    update_profile

    # update /etc/pointercal
    info PI-TFT "Updating TSLib default calibration..."
    if [ "$ROTATION" = 0 ]; then
        pointercal="13874 297 -2846048 -151 8401 -1722344 65536 800 480"
    elif [ "$ROTATION" = 90 ]; then
        pointercal="13874 297 -2846048 -151 8401 -1722344 65536 800 480"
    elif [ "$ROTATION" = 180 ]; then
        pointercal="-13922 5 54388156 52 -8417 33460254 65536 800 480"
    elif [ "$ROTATION" = 270 ]; then
        pointercal="13874 297 -2846048 -151 8401 -1722344 65536 800 480"
    fi
    cat > /etc/pointercal <<EOF
$pointercal
EOF

    # update /etc/udev/rules.d/95-xxxxx.rules
    info PI-TFT "Updating udev rules for Touchscreen..."
    cat > /etc/udev/rules.d/95-ADS7846.rules <<EOF
SUBSYSTEM=="input", ATTRS{name}=="ADS7846 Touchscreen", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
EOF

    # update /etc/modules
    info PI-TFT "Updating /etc/modules..."

    # update /boot/cmdline.txt
    info PI-TFT "Updating console to TFT..."
    ask_console

    # resolve dependencies
    info PI-TFT "Resolving dependencies..."
    install_xserver-xorg-input-evdev

    # success
    do_setup_success

    # reboot?
    do_reboot

    # Otherwise exit if not interactive
    if [ ! "$INTERACTIVE" = True ]; then
        exit 0
    fi
    
}

##           End Screen Configuration Sections                  ##
##                                                              ##
##################################################################


##################################################################
##                                                              ##
##                    Menus                                     ##


function do_setup_success() {
    if [ "$INTERACTIVE" = True ]; then
        if [ ! "$OVERLAY_MISSING" = True ]; then
            whiptail --msgbox "\
Success.

The setup completed successfully.
A restart is required before the new
settings take effect.
" 20 70 1
        else
            whiptail --msgbox "\
WARNING!

The setup completed successfully but:

The overlay required by the selected screen cannot be
found on the system.

\$\$ Try running option \"4 Update Overlays\" in the main menu. \$\$


A restart is required before the new
settings take effect.
" 20 70 1
        fi
    fi
}

function do_kalipi_config() {
    BIN_PATH=$(which kalipi-config)
    if [ "" = "$BIN_PATH" ]; then
    whiptail --msgbox "\
Error:

The tool "kalipi-config" cannot be found on this system.
It can be installed from this site:

https://github.com/Re4son/RPi-Tweaks/tree/master/kalipi-config
" 20 70 1
    else
        exec $BIN_PATH
    fi
}

do_finish() {
    if [ $ASK_TO_REBOOT -eq 1 ]; then
        whiptail --yesno "Would you like to reboot now?" 20 60 2
        if [ $? -eq 0 ]; then # yes
            sync
            reboot
        fi
    fi
    exit 0
}

function do_confirm_menu() {
    pitfttype=$1
    whiptail --yesno "\
Would you like to setup the screen with the following parameters now:

Display:     $pitfttype
Rotation:    $ROTATION
Homedir:     $TARGET_HOMEDIR\
" 20 60 2

    if [ $? -eq 0 ]; then # yes
        remove_tft silent
        do_install_$pitfttype 
    fi
}

function do_homedir_menu() {
    while true; do
        homedir=$(whiptail --inputbox "Please enter home directory" 20 60 $TARGET_HOMEDIR --cancel-button Skip 3>&1 1>&2 2>&3)
        if [ $? -ne 0 ]; then
            return 0
        elif [ -d "$homedir" ]; then
	    TARGET_HOMEDIR=$homedir
            break
        fi
    done
}

function do_rotation_menu() {
    def_item=$1
    calc_wt_size
    ROT=$(whiptail --title "$def_item" --menu "Raspberry Pi Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --default-item "$def_item" --ok-button Select --cancel-button Skip\
        "ROT000 0" "Do not rotate screen" \
        "ROT090 90" "Rotate screen by 90 degrees" \
        "ROT180 180" "Rotate screen by 180 degrees" \
        "ROT270 270" "Rotate screen by 270 degrees" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$ROT" in
            ROT000\ *) ROTATION=0 ;;
            ROT090\ *) ROTATION=90 ;;
            ROT180\ *) ROTATION=180 ;;
            ROT270\ *) ROTATION=270 ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $ROT" 20 60 1
    fi
} 

function do_4dsystems_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "4D-Systems Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "4dpi" "4D-Systems Touchscreen Display" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            4dpi*) do_rotation_menu "ROT180 180" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_adafruit_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Adafruit Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "22" " - Adafruit 2.2\" screen without touch" \
        "24r" " - Adafruit 2.4\" resistive touchscreen hat" \
        "28c" " - Adafruit 2.8\" capacitive touchscreen" \
        "28r" " - Adafruit 2.8\" resistive touchscreen" \
        "32r" " - Adafruit 3.2\" resistive touchscreen" \
        "35r" " - Adafruit 3.5\" resistive touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            22*) do_rotation_menu "ROT270 270" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            24r*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            28c*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            28r*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            32r*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            35r*) do_rotation_menu "ROT270 270" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_elecrow_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Elecrow Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "ecrow35" "Elecrow 3.5\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            ecrow35*) do_rotation_menu "ROT270 270" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_elecfreak_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Elecfreak Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "elec22" "Elecfreak 2.2\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            elec22*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_hotmcu_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Hotmcu Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "hy28b" "Hotmcu HY28B 2.2\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            hy28b*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_hyperpixel_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "HyperPixel Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "hyper35" "Pimoroni HyperPixel 3.5\" Touchscreen" \
        "hyper4t" "Pimoroni HyperPixel 4.0\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            hyper35*) do_rotation_menu "ROT000 000" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            hyper4t*) do_rotation_menu "ROT180 180" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_jbtek_menu_() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "JBTek Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "jb35" "JBTek 3.5\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            jb35*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_kuman_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Kuman Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "kum35" "Kuman 3.5\" Touchscreen" \
        "kum35h" "Kuman 3.5\" HDMI Touchscreen 480x320" \
		"kum35h6" "Kuman 3.5\" HDMI Touchscreen 600x400" \
		"kum35h7" "Kuman 3.5\" HDMI Touchscreen 720x480" \
		"kum35h8" "Kuman 3.5\" HDMI Touchscreen 810x540" \
		"kum35h9" "Kuman 3.5\" HDMI Touchscreen 960x640" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            "kum35") do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
			"kum35h") do_rotation_menu "ROT000 0" && do_homedir_menu && do_confirm_menu $SCREEN ;;
			"kum35h6") do_rotation_menu "ROT000 0" && do_homedir_menu && do_confirm_menu $SCREEN ;;
			"kum35h7") do_rotation_menu "ROT000 0" && do_homedir_menu && do_confirm_menu $SCREEN ;;
			"kum35h8") do_rotation_menu "ROT000 0" && do_homedir_menu && do_confirm_menu $SCREEN ;;
			"kum35h9") do_rotation_menu "ROT000 0" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_kedei_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Kedei Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "kedei35" "KeDei 3.5\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            kedei35*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_osoyoo_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Osoyoo Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "oso35" "Osoyoo 3.5\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            oso35*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_raspberry_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Raspberry Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "pi70" "Original Raspberry Pi 7\" Touchscreen Display" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            pi70*) do_rotation_menu "ROT180 180" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_sainsmart_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Sainsmart Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "sain32" "Sainsmart 3.2\" Touchscreen" \
        "sain35" "Sainsmart 3.5\" Touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            sain32*) do_rotation_menu && do_homedir_menu && do_confirm_menu $SCREEN ;;
            sain35*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_waveshare_menu() {
    calc_wt_size
    SCREEN=$(whiptail --title "$TITLE" --menu "Waveshare Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "wave32" " - Waveshare 3.2\" touchscreen" \
        "wave35" " - Waveshare 3.5\" (A) touchscreen" \
        "wave35b" " - Waveshare 3.5\" (B) touchscreen" \
        "wave35h" " - Waveshare 3.5\" HDMI touchscreen" \
        "wave40" " - Waveshare 4.0\" touchscreen" \
        "wave50" " - Waveshare 5.0\" HDMI touchscreen" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$SCREEN" in
            wave32*) do_rotation_menu "ROT270 270" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            wave35*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            wave35b*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            wave35h*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            wave40*) do_rotation_menu "ROT090 90" && do_homedir_menu && do_confirm_menu $SCREEN ;;
            wave50*) do_rotation_menu && do_homedir_menu && do_confirm_menu $SCREEN ;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $SCREEN" 20 60 1
    fi
}

function do_setup_menu() {
    calc_wt_size
    FUN=$(whiptail --title "$TITLE" --menu "Touchscreen setup" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
        "S010 4D-Systems" "Set up 4D-Systems touchscreens" \
        "S020 Adafruit" "Set up Adafruit touchscreens" \
        "S030 Elecfreak" "Set up Elecfreak touchscreens" \
        "S040 Elecrow" "Set up Elecrow touchscreens" \
        "S050 Hotmcu" "Set up Hotmcu touchscreens" \
        "S060 HyperPixel" "Set up HyperPixel touchscreens" \
        "S070 JBTek" "Set up JBTek touchscreens" \
        "S080 KeDei" "Set up KeDei touchscreens" \
        "S090 Kuman" "Set up Kuman touchscreens" \
        "S100 Osoyoo" "Set up Osoyoo touchscreens" \
        "S110 Raspberry Pi" "Set up original Raspberry Pi touchscreens" \
        "S120 Sainsmart" "Set up Sainsmart touchscreens" \
        "S130 Waveshare" "Set up Waveshare touchscreens" \
    3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        return 0
    elif [ $RET -eq 0 ]; then
        case "$FUN" in
            S010\ *) do_4dsystems_menu;;
            S020\ *) do_adafruit_menu;;
            S030\ *) do_elecfreak_menu;;
            S040\ *) do_elecrow_menu;;
            S050\ *) do_hotmcu_menu;;
            S060\ *) do_hyperpixel_menu;;
            S070\ *) do_jbtek_menu;;
            S080\ *) do_kedei_menu;;
            S090\ *) do_kuman_menu;;
            S100\ *) do_osoyoo_menu;;
            S110\ *) do_raspberry_menu;;
            S120\ *) do_sainsmart_menu;;
            S130\ *) do_waveshare_menu;;
            *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
    fi
}

function do_show_config() {
    if [ -f "/boot/cmdline.txt" ]; then
        local CMDLINETXT=$(cat "/boot/cmdline.txt")
    fi
    if [ -f "/boot/config.txt" ]; then
        local CONFIGTXT=$(sed -n '/added by re4son-pi-tft-setup/,/end re4son-pi-tft-setup/p' "/boot/config.txt")
    fi
    if [ -f "/etc/X11/xorg.conf.d/99-calibration.conf" ]; then
        local CALIBCONF=$(cat "/etc/X11/xorg.conf.d/99-calibration.conf")
    fi
    if [ -f "/etc/pointercal" ]; then
        local POINTERCAL=$(cat "/etc/pointercal")
    fi
    if [ -f "/dev/input/touchscreen" ]; then
        local INTOUCH=$(cat "/dev/input/touchscreen")
    fi
    if [ -f "/etc/udev/rules.d/95-*.rules" ]; then
        local UDEV=$(cat "/etc/udev/rules.d/95-*.rules")
    fi
    if [ -f "/etc/modprobe.d/re4son.conf" ]; then
        local MODPROBED=$(cat "/etc/modprobe.d/re4son.conf")
    fi
    if [ -f "/usr/share/X11/xorg.conf.d/45-evdev.conf" ]; then
        local EVDEV=$(cat "/usr/share/X11/xorg.conf.d/45-evdev.conf")
    fi
    if [ -f "/etc/kbd/config" ]; then
        local KBDCONF=$(cat "/etc/kbd/config")
    fi
    if [ -f "/etc/X11/xorg.conf.d/99-fbdev.conf" ]; then
        local FBDEV=$(cat "/etc/X11/xorg.conf.d/99-fbdev.conf")
    fi
    if [ -f "${TARGET_HOMEDIR}/.profile" ]; then
        local USRPROFILE=$(sed -n '/added by re4son-pi-tft-setup/,/end re4son-pi-tft-setup/p' "${TARGET_HOMEDIR}/.profile")
    fi
    if [ -f "/etc/systemd/system/hyperpixel4-init.service" ]; then
        local HPPXL4INIT=$(cat "/etc/systemd/system/hyperpixel4-init.service")
        local HPPXL4INITSTAT=$(systemctl status hyperpixel4-init.service)
    fi
    if [ -f "/etc/systemd/system/hyperpixel-init.service" ]; then
        local HPPXLINIT=$(cat "/etc/systemd/system/hyperpixel-init.service")
        local HPPXLINITSTAT=$(systemctl status hyperpixel-init.service)
    fi
    if [ -f "/etc/systemd/system/hyperpixel-touch.service" ]; then
        local HPPXLTOUCH=$(cat "/etc/systemd/system/hyperpixel-touch.service")
        local HPPXLTOUCHSTAT=$(systemctl status hyperpixel-touch.service)
    fi
    if [ -f "/usr/lib/kalipi-sys-mods/i2cprobe" ]; then
        local I2CPROBE=$(cat "/usr/lib/kalipi-sys-mods/i2cprobe")
    fi
    if [ -f "/lib/udev/rules.d/15-i2c-modprobe.rules" ]; then
        local I2CRULES=$(cat "/lib/udev/rules.d/15-i2c-modprobe.rules")
    fi  
    whiptail --title "Current Display Configuration" --scrolltext --msgbox "\

/boot/cmdline.txt:
${CMDLINETXT}
 
/boot/config.txt:
${CONFIGTXT}

/etc/modprobe.d/re4son.conf:
${MODPROBED}

/etc/X11/xorg.conf.d/99-calibration.conf:
${CALIBCONF}

/etc/pointercal:
${POINTERCAL}

/etc/udev/rules.d/95-*.rules:
${UDEV}

/dev/input/touchscreen:
${INTOUCH}

/usr/share/X11/xorg.conf.d/45-evdev.conf:
${EVDEV}

/etc/X11/xorg.conf.d/99-fbdev.conf:
${FBDEV}

/etc/kbd/config:
${KBDCONF}

/etc/systemd/system/hyperpixel-init.service:
${HPPXLINIT}

Status hyperpixel-init.service:
${HPPXLINITSTAT}

/etc/systemd/system/hyperpixel-touch.service:
${HPPXTOUCH}

Status hyperpixel-touch.service:
${HPPXLTOUCHSTAT}

/etc/systemd/system/hyperpixel4-init.service:
${HPPXLINIT}

Status hyperpixel4-init.service:
${HPPXLINITSTAT}

/usr/lib/kalipi-sys-mods/i2cprobe:
${I2CPROBE}

/lib/udev/rules.d/15-i2c-modprobe.rules
${I2CRULES}

~/.profile:
${USRPROFILE}
" $WT_HEIGHT $WT_WIDTH 1 
return 0
}

function do_main_menu() {
    INTERACTIVE=True
    check_dependencies
    calc_wt_size
    if [ -f /proc/device-tree/model ]; then
        PIMODEL=$(cat /proc/device-tree/model| tr -d '\0')
    fi
    while true; do
        FUN=$(whiptail --title "$TITLE" --backtitle "$PIMODEL" --cancel-button Finish --menu "Main Menu" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --ok-button Select \
            "1 Setup Display" "Set up a new display" \
            "2 Remove Display" "Remove any existing display configuration" \
            "3 Show Config" "Show current display configuration" \
            "4 Update Overlays" "Download latest 3rd party overlays" \
            "5 Kali-Pi Config" "Run the kalipi-config setup tool for Kali-Pi" \
            "6 About $BASE_NAME" "Information about $BASE_NAME" \
            3>&1 1>&2 2>&3)
        RET=$?
        if [ $RET -eq 1 ]; then
            do_finish
        elif [ $RET -eq 0 ]; then
            case "$FUN" in
                1\ *) do_setup_menu ;;
                2\ *) remove_tft ;;
                3\ *) do_show_config ;;
                4\ *) do_get_overlays ;;
                5\ *) do_kalipi_config ;;
                6\ *) do_about ;;
                *) whiptail --msgbox "Program error: unrecognized option" 20 60 1 ;;
            esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
        else
            exit 1
        fi
    done
}

    
# MAIN

if [[ $EUID -ne 0 ]]; then
    bail "re4son-pi-tft-setup must be run as root. try: sudo $PROG_NAME"
fi

if [[ $# -eq 0 ]] ; then
    do_main_menu
    do_finish
    exit 0
fi

args=$(getopt -uo 'hvurit:o:d:' -- $*)
[ $? != 0 ] && do_main_menu && do_finish
INTERACTIVE=False
set -- $args

for i
do
    case "$i"
    in
        -h)
            print_help
            ;;
        -v)
            print_version
            ;;
        -u)
            do_update
            ;;
        -r)
            removetft="YES"
            shift
            ;;
        -d)
            TARGET_HOMEDIR="$2"
            info PI-TFT "Homedir = ${2}"
            shift
            shift
            ;;
        -t)
            pitfttype="$2"
            info PI-TFT "Type = ${2}"
            shift
            shift
            ;;
    esac
done

# check init system (technique borrowed from raspi-config):
info PI-TFT 'Checking init system...'
if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  info PI-TFT "Found systemd"
  SYSTEMD=1
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  info PI-TFT "Found sysvinit"
  SYSTEMD=0
else
  bail "Unrecognised init system"
fi

if grep -q boot /proc/mounts; then
    info PI-TFT "/boot is mounted"
else
    if ask "Cannot find /boot. Maybe it is not mounted. Shall I try mounting it?" "Y"; then
        info PI-TFT "Mounting /boot"
	mount /dev/mmcblk0p1 /boot
    fi
    if grep -q boot /proc/mounts; then
        info PI-TFT "/boot is mounted"
    else
        echo "/boot must be mounted. If you're sure it is, proceed - otherwise quit here and try: sudo mount /dev/mmcblk0p1 /boot"
	if ask "Continue?" "N"; then
	    info PI-TFT "Proceeding."
	else
	    bail "Aborting."
	fi
    fi
fi

if [[ ! -e "$TARGET_HOMEDIR" || ! -d "$TARGET_HOMEDIR" ]]; then
    bail "$TARGET_HOMEDIR must be an existing directory (use -d /home/foo to specify)"
fi

if [ "${removetft}" == "YES" ]; then
    remove_tft || bail "Unable to remove tft configuration"
fi

## Start config
case "${pitfttype}" in
	4dpi)
                ROTATION=180
                do_install_4dpi
		;;
	24r)
		ROTATION=90
                do_install_24r
		;;
	28r)
		ROTATION=90
                do_install_28r
		;;
	28c)
		ROTATION=90
                do_install_28c
		;;
	32r)
		ROTATION=90
                do_install_32r
		;;
	35r)
		ROTATION=270
                do_install_35r
		;;
	22)
		ROTATION=90
                do_install_22
		;;
	ecrow35)
		ROTATION=90
                do_install_ecrow35
		;;
	elec22)
		ROTATION=90
                do_install_elec22
		;;
	hy28b)
		ROTATION=90
                do_install_hy28b
		;;
	hyper35)
		ROTATION=0
                do_install_hyper35()
		;;
	hyper4t)
		ROTATION=0
                do_install_hyper4t()
		;;
	jb35)
		ROTATION=90
                do_install_jb35
		;;
        kedei35)
		ROTATION=90
                do_install_kedei35
                ;;
	kum35)
		ROTATION=90
                do_install_kum35 
		;;
	kum35h)
		ROTATION=0
                do_install_kum35 
		;;
        oso35)
                do_install_oso35
                ;; 
	pi70)
		ROTATION=180
		do_install_pi70
		;;
	sain32)
		ROTATION=90
                do_install_sain32
		;;
	sain35)
		ROTATION=90
                do_install_sain35
		;;
	wave32)
		ROTATION=90
                do_install_wave32
		;;
	wave35)
		ROTATION=90
                do_install_wave35
		;;
	wave35b)
		ROTATION=90
                do_install_wave35b
		;;
	wave35h)
		ROTATION=90
                do_install_wave35b
		;;
	wave40)
		ROTATION=90
                do_install_wave40
		;;
	wave50)
                do_install_wave50
		;;
	*)
		echo "Invalid screen. Choose a valid type from the list below."
		print_help
esac

