# Common functions shared by LTSP init scripts

. /usr/share/ltsp/ltsp_config
. /usr/share/ltsp/ltsp-common-functions

warn() {
    msg="$1"
    logger -p user.warning -t ltsp-client  "warning: $msg"
}

start_sound() {
    if boolean_is_true "$SOUND" ; then
        # Detect and report a common problem with thin clients
        if [ ! -c /dev/dsp ] ; then
            warn "Sound requested but /dev/dsp is missing.  Continuing."
        fi
        case "$SOUND_DAEMON" in
            pulse|'') # The default when no value is set
                /usr/bin/pulseaudio --system \
                --exit-idle-time=-1 \
                --disable-shm \
                --no-cpu-limit \
                --resample-method=trivial \
                --high-priority \
                -L module-detect \
                -L "module-esound-protocol-tcp auth-anonymous=1" \
                -L "module-native-protocol-tcp auth-anonymous=1" \
                -L module-volume-restore \
                -L module-rescue-streams \
                -L module-native-protocol-unix \
                -n &
                ;;
            esd) 
                /usr/bin/esd -nobeeps -public -tcp &
                ;;
            nasd)
                /usr/bin/nasd -aa &
                # Line copied from old LTSP:  Should we use it? [pere 2006-03-03]
                #aumix-minimal -v100 -w100 -c90 -m10
                ;;
            *)
                warn "Unable to start unsupported sound daemon: '$SOUND_DAEMON'"
                ;;
        esac
    fi
}

configure_sound_volume() {
    # Set up sound volume
    ## Set higher volume than default if not specified by lts.conf
    [ -z "$VOLUME" ]           && VOLUME=90
    [ -z "$HEADPHONE_VOLUME" ] && HEADPHONE_VOLUME=90
    [ -z "$PCM_VOLUME" ]       && PCM_VOLUME=90
    [ -z "$CD_VOLUME" ]        && CD_VOLUME=90
    [ -z "$FRONT_VOLUME" ]     && FRONT_VOLUME=90

    if [ "$VOLUME" != "0" ]; then
       /usr/bin/amixer -c0 sset Master,0 ${VOLUME}% unmute 2>/dev/null
       /usr/bin/amixer -c0 sset 'Master Digital',0 ${VOLUME}% unmute 2>/dev/null
    fi
    if [ "$HEADPHONE_VOLUME" != "0" ]; then
       /usr/bin/amixer -c0 sset Headphone,0 ${HEADPHONE_VOLUME}% unmute 2>/dev/null
    fi
    if [ "$PCM_VOLUME" != "0" ]; then
       /usr/bin/amixer -c0 sset PCM,0 ${PCM_VOLUME}% unmute 2>/dev/null
    fi
    if [ "$CD_VOLUME" != "0" ]; then
       /usr/bin/amixer -c0 sset CD,0 ${CD_VOLUME}% unmute cap 2>/dev/null
    fi
    if [ "$FRONT_VOLUME" != "0" ]; then
       /usr/bin/amixer -c0 sset Front,0 ${FRONT_VOLUME}% unmute cap 2>/dev/null
    fi

    # Microphone can be controlled by Pulseaudio, so let it be set by lts.conf
    if [ -n "$MIC_VOLUME" ]; then
       /usr/bin/amixer -c0 sset Mic,0 ${MIC_VOLUME}% unmute cap 2>/dev/null
    fi

}

configure_localdev() {
    if boolean_is_true "$LOCALDEV" ; then
        # Make this sessions secret auth cookie for ltspfs
        if [ ! -f /var/run/ltspfs_token ]; then
            mcookie > /var/run/ltspfs_token
        fi
        if [ -z "$(pgrep ltspfsd)" ]; then
            /usr/bin/ltspfsd
        fi
        # cdrom devices are handled by the cdpingerponger
        if [ -z "$(pgrep -l -f -x "/usr/sbin/cdpinger cdrom$")" ]; then
            /usr/sbin/cdpinger cdrom # default for usb cdroms
        fi

        # and start one for every additional cdrom device
        if [ -L /dev/cdrom?* ];then
            for CDDEV in $(ls /dev/cdrom?*); do
                if [ -z "$(pgrep -l -f -x "/usr/sbin/cdpinger ${CDDEV}$")" ]; then
                    /usr/sbin/cdpinger $(basename ${CDDEV})
                fi
            done
        fi
    fi
}

configure_swap() {
    if boolean_is_true "$USE_LOCAL_SWAP" ; then
        # Enable local swap partition if found on local disk
        for part in `sfdisk -l 2>/dev/null | awk '/ 82 / { print $1}'`; do
            swap_devices="$swap_devices $part"
        done
    fi

    if boolean_is_true "$NBD_SWAP" ; then
        SWAP_SERVER=${SWAP_SERVER:-"$SERVER"}
        NBD_PORT=${NBD_PORT:-"9572"}
        modprobe nbd
        # Detect first unused nbd device, skip nbd0
        for num in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
            nbd-client -c /dev/nbd${num} > /dev/null
            [ $? -eq 1 ] && break
        done
        nbd-client $SWAP_SERVER $NBD_PORT /dev/nbd${num} && \
            swap_devices="$swap_devices /dev/nbd${num}"
    fi

    if boolean_is_true "$ENCRYPT_SWAP" ; then
        if [ -x /sbin/cryptsetup ]; then
            modprobe dm_crypt
        else
            echo "ERROR: ENCRYPT_SWAP=Y, but /sbin/cryptsetup not found. disabling swap."
            swap_devices=""
        fi
    fi

    num=0
    for device in $swap_devices ; do
        swap="$device"
        if boolean_is_true "$ENCRYPT_SWAP" ; then
            if [ -x /sbin/cryptsetup ]; then
                cryptsetup -d /dev/urandom create swap$num $swap && swap="/dev/mapper/swap$num"
                num=$(($num+1))
            fi
        fi
        mkswap $swap
        swapon $swap
    done
}

configure_printer() {
    for I in 0 1 2; do
        eval DEVICE=\$\{PRINTER_${I}_DEVICE\}
        if [ -n "${DEVICE}" ]; then
            eval PORT=\$\{PRINTER_${I}_PORT:="910${I}"\} 
            eval BAUD=\$\{PRINTER_${I}_SPEED:-"9600"\}
            eval SIZE=\$\{PRINTER_${I}_DATABITS:-"8"\}
            eval PARITY=\$\{PRINTER_${I}_PARITY:-"none"\}
            eval FLOW=\$\{PRINTER_${I}_FLOWCTRL:-"soft"\}

            JETPIPE_ARGS=
            [ -n "$BAUD" ] && JETPIPE_ARGS="${JETPIPE_ARGS} -b ${BAUD}"
            [ -n "$SIZE" ] && JETPIPE_ARGS="${JETPIPE_ARGS} -y ${SIZE}"
            [ -n "$PARITY" ] && JETPIPE_ARGS="${JETPIPE_ARGS} -p ${PARITY}"
            if [ -n "$FLOW" ]; then
                if [ "$FLOW" = "soft" ]; then
                    JETPIPE_ARGS="${JETPIPE_ARGS} -x"  
                else
                    JETPIPE_ARGS="${JETPIPE_ARGS} -r" 
                fi
            fi

            /usr/sbin/jetpipe ${JETPIPE_ARGS} ${DEVICE} ${PORT}
        fi
    done
}

configure_serial_mouse() {
      if [ -n "$X_MOUSE_DEVICE" ] && \
          [ -n "$X_MOUSE_PROTOCOL" ] && \
          type inputattach >/dev/null 2>/dev/null && \
          [ -n "$(echo $X_MOUSE_DEVICE | awk '/\/dev\/ttyS[0-9]/')" ]; then
          inputattach --"$X_MOUSE_PROTOCOL" "$X_MOUSE_DEVICE" &
      fi
}

set_time() {
    # Set up timezone
    if [ -n "$TIMEZONE" ] && [ -e "/usr/share/zoneinfo/$TIMEZONE" ]; then
        cp /usr/share/zoneinfo/$TIMEZONE /etc/localtime 2>/dev/null
    fi

    # Set timeserver to $SERVER if set to autodetect
    if [ "$TIMESERVER" = "auto" ]; then
        TIMESERVER=${SERVER}
    fi

    # Set up timeserver
    [ -n "$TIMESERVER" ] && ntpdate $TIMESERVER &
}
