# 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 \
                --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_localdev() {
    if boolean_is_true "$LOCALDEV" ; then
        # Make this sessions secret auth cookie for ltspfs
        mcookie > /var/run/ltspfs_token
        /usr/bin/ltspfsd
        # cdrom devices are handled by the cdpingerponger
        /usr/sbin/cdpinger cdrom # default for usb cdroms

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

configure_x() {
    if boolean_is_true "$CONFIGURE_X" ; then
        ${CONFIGURE_X_COMMAND:-/usr/share/ltsp/configure-x.sh}
    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
        nbd-client $SWAP_SERVER $NBD_PORT /dev/nbd0 && \
            swap_devices="$swap_devices /dev/nbd0"
    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\}
        [ -n "${DEVICE}" ] && \
            eval PORT=\$\{PRINTER_${I}_PORT:="910${I}"\} && \
            /usr/sbin/jetpipe ${DEVICE} ${PORT}
    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
}
