#!/bin/bash
 ##########################################################################
 # sample - Example of use of gesconf .
 # Version:     0.3.8 13/10/2007
 # File:        samplefunc
 # Description: Function for example of use of gesconf .
 # Platform(s):	GNU/Linux, All
 # Author(s):	Della Bianca Giuseppe <bepi@adria.it><bepii@libero.it>
 ##########################################################################
function PrintWait (){
    clear
    echo -e $1
    cat $2
    echo

    if [ "$3" ]; then
	echo "Result"
	$3
	echo
    fi

    read -n 1 -s -p "Press a key" KeyPress
    echo
}

function BasicExample (){
#
text="Simple and fast read of all key of group MODE1 (with multiple key) \nand set shell variables:\n"
cat >sample.tmp <<EOF
    # The IFS set and -ps option is necessary only if space 
    # is present in group or key

    GesConfProg="../src/gesconf -v 2 -ps \$GFS -fm \$FileMulty -vs" 
    group="INTERFACE:VIDEO:MODE1" pref="c_" VarName=""

    IFS=\$GFS
    while read stat key val; do
	VarName=\$pref\$key
	eval "export '\$VarName'='\$val'"
    done < <(IFS=\$OLDIFS; \$GesConfProg " | " -l "\$group" "" K)
    IFS=\$OLDIFS

    echo 'c_color=' \$c_color
    echo 'c_resolution=' \$c_resolution
    echo 'c_refresh=' \$c_refresh
    echo 'c_mode=' \$c_mode
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast read of several key of group MODE1 (with multiple key) \nand set shell variables:\n"
cat >sample.tmp <<EOF
    # The IFS set and -ps option is necessary only if space 
    # is present in group or key"

    GesConfProg="../src/gesconf -v 2 -ps \$GFS -fm \$FileMulty -vs" 
    group="INTERFACE:VIDEO:MODE1" pref="cc_" VarName=""

    IFS=\$GFS
    while read stat key val; do
	VarName=\$pref\$key
	eval "export '\$VarName'='\$val'"
    done < <(IFS=\$OLDIFS; \$GesConfProg " | " -g "\$group" color -g "\$group" resolution)
    IFS=\$OLDIFS

    echo 'cc_color=' \$cc_color
    echo 'cc_resolution=' \$cc_resolution
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast read of single key of group MODE1 (with multiple key) \nand set shell variable:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 1 -fm \$FileMulty -vs"
    group="INTERFACE:VIDEO:MODE1"

    ccc_resolution=\`\$GesConfProg " | " -g "\$group" resolution\`

    echo 'ccc_resolution=' \$ccc_resolution
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast set of several key of group MODE1 (with multiple key) \nand list key of group:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO:MODE1"

    echo -e \\
    \$group color \"new1 32\" '\n' \\
    \$group resolution \"800 x 600\" \"new2 800 x 600\" '\n' \\
    \$group resolution \"640 x 480\" \"new3 640 x 480\" '\n' \\
    \$group refresh \"new4 70 120\" '\n' \\
    \$group mode \"new5 framebuffer\" | \\
    \$GesConfProg - -s -l \$group "" K 
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast set of group MODE1 from file (with multiple key) \nand list key of group:\n"
cat >sample.tmp <<EOF
    # The IFS set and -ps option is necessary only if space 
    # is present and is not possible using " .. " as delimitator

    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO:MODE1"

    echo -e \\
    \$group color \"file new1 32\" '\n' \\
    \$group resolution \"new2 800 x 600\" \"file new2 800 x 600\" '\n' \\
    \$group resolution \"new3 640 x 480\" \"file new3 640 x 480\" '\n' \\
    \$group refresh \"file new4 70 120\" '\n' \\
    \$group mode \"file new5 framebuffer\" \\
    > sample.data

    cat sample.data | \$GesConfProg - -s -l \$group "" K 
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast set single key of group MODE1 and get key:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO:MODE1"

    \$GesConfProg -s \$group color "8 16 32" -g \$group color
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast list the value of the group:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO"

    \$GesConfProg -l \$group
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast list the value of the key of the group:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO"

    \$GesConfProg -l \$group "" K
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

text="Simple and fast list the value of same group:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO"

    \$GesConfProg -l \$group:
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

}

function LockExample (){
#
text="Simple and fast lock the value of the key:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO:MODE2"

    \$GesConfProg -lk \$group color L
    echo
    \$GesConfProg -s \$group color 32  -g \$group color
    echo
    \$GesConfProg -ws \$group color 32 -l \$group color
    echo
    \$GesConfProg -os \$group color 32 -l \$group color K
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast unlock the value of the key:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fm \$FileMulty"
    group="INTERFACE:VIDEO:MODE2"

    \$GesConfProg -lk \$group color U
    echo
    \$GesConfProg -s \$group color 16  -l \$group color
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

}

function X11Example (){
#
text="Simple and fast set at nvidia the driver for screen:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 1 -fx \$FileX11"
    SerGrp="ServerLayout"

    ScrId=\`\$GesConfProg -l \$SerGrp: Screen\`
    DevId=\`\$GesConfProg -g "Screen:\$ScrId" Device\`

    echo ScrId= \$ScrId
    echo DevId= \$DevId
    echo 
    \$GesConfProg -v 4 -g "Device:\$DevId" Driver -s "Device:\$DevId" Driver nvidia -l "Device:\$DevId"
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

#
text="Simple and fast remove gxl module from list of load module:\n"
cat >sample.tmp <<EOF
    GesConfProg="../src/gesconf -v 4 -fx \$FileX11"
    group="Module"

    \$GesConfProg -v 3 -g \$group Load glx
    echo
    \$GesConfProg -d \$group Load glx -l \$group Load
EOF
PrintWait "$text" "./sample.tmp" "./sample.tmp"

}
