#!/bin/sh
#
#  This script upgrades a pre-gdi Platinum module to use gdi exclusively.
#  Some settings (e.g. with CD1.1) are kept; mostly, however, we revert to
#  sensible defaults.
#



# Only run once
[ -n "${POST_UPGRADE_CACHE}" ] && touch "${POST_UPGRADE_CACHE}/`basename $0`"



gdi_upgrade_message_shown="0"
show_gdi_upgrade_message() {
    [ "${gdi_upgrade_message_shown}" -ne 0 ] && return 0
    gdi_upgrade_message_shown="1"

    cat <<EOF
==============================================================================
                             Important: gdi upgrade
==============================================================================

Your platinum module is being upgraded to use "gdi", or Guralp Data
Interconnect. This is a new generation seismic data subsystem which aims to
simplify configuration and operation of the Guralp data logging system.

In simple cases, your configuration will be upgraded completely automatically
and no further action will be required. If you have set up something more
complex, then user intervention is required.

Please contact support@guralp.com if you have queries or need help with the
new system.

You *will* need to reboot after this upgrade!
EOF
}



autostart_remove() {
    grep -q "$1" "/etc/init.local/autostart" || return 0
    grep -v "$1" "/etc/init.local/autostart" > "/etc/init.local/.autostart.tmp"
    mv "/etc/init.local/.autostart.tmp" "/etc/init.local/autostart"
}



######################
####  IF modules  ####
######################



if [ -d "/etc/data-mux-if" ]
then
    show_gdi_upgrade_message
    cat <<EOF

 * Switching from data-mux-if to gdi-base. If you have set up more than one
   data-mux-if module, you will need to review the new configuration.
EOF

    rm -rf "/etc/data-mux-if" "/etc/init.local/data-mux-if".*
    autostart_remove "data-mux-if"
fi



if [ -d "/etc/data-conv-gcf-if" ]
then
    show_gdi_upgrade_message
    cat <<EOF

 * Removing data-conv-gcf-if. All channel names are now mapped in the output
   modules only (e.g. in the CD1.1 converter module or in the GSMS sender
   module). An automatic naming mode has also been implemented.
EOF
    rm -rf "/etc/data-conv-gcf-if" "/etc/init.local/data-conv-gcf-if".*
    autostart_remove "data-conv-gcf-if"
fi



#######################
####  GCF modules  ####
#######################



if [ -d "/etc/data-mux-gcf" ]
then
    show_gdi_upgrade_message
    cat <<EOF

 * Switching from data-mux-gcf to gdi2gcf. If you have changed the buffer
   size or location, or used more than one module, you will need to review
   the new configuration.
EOF

    rm -rf "/etc/data-mux-gcf" "/etc/init.local/data-mux-gcf".*
    autostart_remove "data-mux-gcf"
fi



if [ -d "/etc/data-out-gcf-whisper" -o -d "/etc/data-in-gcf-whisper" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-out-gcf-whisper/"* "/etc/data-in-gcf-whisper/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing Whisper services. Please set up gdi-link instead. This has
   equivalent functionality but is easier to configure.
EOF
    fi

    rm -rf "/etc/data-out-gcf-whisper" "/etc/data-in-gcf-whisper" \
        "/etc/init.local/data-out-gcf-whisper".* "/etc/init.local/data-in-gcf-whisper".*
    autostart_remove "data-out-gcf-whisper"
    autostart_remove "data-in-gcf-whisper"
fi



if [ -d "/etc/data-out-gcf-scream" ]
then
    show_gdi_upgrade_message
    cat <<EOF

 * Upgrading data-out-gcf-scream to gcf-out-scream. You can set up multiple
   new scream servers, each with a different set of channels.
EOF

    rm -rf "/etc/data-out-gcf-scream" "/etc/init.local/data-out-gcf-scream".*
    autostart_remove "data-out-gcf-scream"
fi



if [ -d "/etc/data-in-gcf-scream" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-in-gcf-scream/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing data-in-gcf-scream modules. Please set up gcf-in-scream instead.
EOF
    fi

    rm -rf "/etc/data-in-gcf-scream" "/etc/init.local/data-in-gcf-scream".*
    autostart_remove "data-in-gcf-scream"
fi



if [ -d "/etc/data-in-gcf-brp-tcp" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-in-gcf-brp-tcp/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing data-in-gcf-brp-tcp modules. Please set up gcf-in-brp instead.
EOF
    fi

    rm -rf "/etc/data-in-gcf-brp-tcp" "/etc/init.local/data-in-gcf-brp-tcp".*
    autostart_remove "data-in-gcf-brp-tcp"
fi



###############################
####  Serial port modules  ####
###############################



if [ -d "/etc/conf.d/serial/data-out-gcf-brp" ]
then
    for cf in /etc/conf.d/serial/data-out-gcf-brp/*
    do
        [ -e "${cf}" ] || continue
        portname="$(basename "${cf}")"
        newcf="/etc/conf.d/serial/gcf-out-brp/${portname}"
        echo " * Upgrading GCF output for serial port ${portname}"
        mkdir -p "/etc/conf.d/serial/gcf-out-brp"
        echo "gcflog_size = 262144" > "${newcf}"
        grep "^acknak-timeout" "${cf}" | sed -e 's,acknak-timeout,acknak_timeout,' > "${newcf}"
    done

    rm -rf "/etc/conf.d/serial/data-out-gcf-brp"
fi



if [ -d "/etc/conf.d/serial/data-in-gcf-brp" ]
then
    for cf in /etc/conf.d/serial/data-in-gcf-brp/*
    do
        [ -e "${cf}" ] || continue
        portname="$(basename "${cf}")"
        newcf="/etc/conf.d/serial/gcf-in-brp/${portname}"
        echo " * Upgrading GCF input for serial port ${portname}"
        mkdir -p "/etc/conf.d/serial/gcf-in-brp"
        echo "gcflog_size = 262144" > "${newcf}"
        grep "^transmission-delay" "${cf}" | sed -e 's,transmission-delay,rtstatus_delay,' >> "${newcf}"
        grep "^disable-rewind" "${cf}" | sed -e 's,disable-rewind,brp_disable_rewind,' >> "${newcf}"
    done

    rm -rf "/etc/conf.d/serial/data-in-gcf-brp"
fi



#########################
####  CD1.1 modules  ####
#########################



for svc in "/etc/data-mux-cd11/"*.local
do
    [ ! -r "${svc}" ] && continue
    [ "${svc%.ctl.local}" != "${svc}" ] && continue

    # application_description field differentiates between old config and new
    grep -q "^application_description *=" "${svc}" && continue

    svc_cl="$(echo ${svc} | sed -e 's/.local/.ctl.local/')"
    [ -e "${svc_cl}" ] && source "${svc_cl}"
    module="$(basename "${svc}" ".local")"
    desc="${DESC:-CD1.1 multiplexor module, instance ${module}}"
    show_gdi_upgrade_message
    echo " * Converting data-mux-cd11.${module} (${desc})"

    if ! grep -q "^sockpath *=" "${svc}"
    then
        echo "sockpath = /var/run/data-mux-cd11.${module}" >> "${svc}"
    fi
    echo "application_description = ${desc}" >> "${svc}"
done



if [ -d "/etc/data-conv-if-cd11" ]
then
    show_gdi_upgrade_message
    echo " * Upgrading data-conv-if-cd11 to gdi2cd11"

    for svc in /etc/data-conv-if-cd11/*.local
    do
        [ ! -r "${svc}" ] && continue
        [ "${svc%.ctl.local}" != "${svc}" ] && continue
	svc_cl="$(echo ${svc} | sed -e 's/.local/.ctl.local/')"
	[ -e "${svc_cl}" ] && source "${svc_cl}"
        module="$(basename "${svc}" ".local")"
        new_svc="/etc/gdi2cd11/${module}.local"
        desc="${DESC:-CD1.1 converter, instance ${module}}"
        echo "    - converting ${svc} (${desc})"
        echo "      (please review channel mappings, they will need changing)"

        mkdir -p "/etc/gdi2cd11"
        cat > "/etc/gdi2cd11/${module}.ctl.local" <<EOF
# File created by /usr/lib/upgrade/100_gdi.sh, $(isodate -e)
DESC="${desc}"
EOF
        cat > "/etc/init.local/gdi2cd11.${module}" <<EOF
#!/sbin/svc
#  Script to start gdi2cd11 instance ${module}

MYLISTING="${desc}"
MYCFG="/etc/gdi2cd11/${module}.local"

depends() {
    depends_on gdi2gcf
}
start() {
    daemonize /usr/bin/gdi2cd11 --config "\${MYCFG}"
}
stop() {
    kill_pid
}
EOF
        chmod 0755 "/etc/init.local/gdi2cd11.${module}"

        mux_path="$(gcs_rwvar "gcs_get_varcf2" "output_socket" "${svc}" "")"
        subframe_transformation="$(gcs_rwvar "gcs_get_varcf2" "subframe_transformation" "${svc}" "")"

        cat > "${new_svc}" <<EOF
# File created by /usr/lib/upgrade/100_gdi.sh, $(isodate -e)
application_description = ${desc}
max_clock_differential = 1000
mux_path = ${mux_path:-/var/run/data-mux-cd11.0}
subframe_transformation = ${subframe_transformation:-compress}
EOF

        for var in data_frame_duration spyrus_slot auth_key_id log_file log_level management_socket
        do
            grep "^${var} *=" "${svc}" >> "${svc_new}"
        done

        if grep -q "^modules *=.*tamper_monitor.*" "${svc}"
        then
            echo "enable_libtamper = true" >> "${svc_new}"
        fi

        copying="0"
        while read -r line
        do
            case "${line}" in
            \[tamper_monitor\])
                line="[tamper]"
                copying="1"
                ;;
            \[*\])
                copying="0"
                ;;
            esac

            [ "${copying}" -ne 0 ] && echo "${line}" >> "${svc_new}"
        done < "${svc}"

    done

    rm -rf "/etc/data-conv-if-cd11" "/etc/init.local/data-conv-if-cd11".*
    sed -e "s/data-conv-if-cd11/gdi2cd11/" -i "/etc/init.local/autostart"
fi



#########################
####  Misc. modules  ####
#########################



if [ -d "/etc/data-out-gsms" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-out-gsms/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing data-out-gsms modules. Please set up gsms-out instead.
EOF
    fi

    rm -rf "/etc/data-out-gsms" "/etc/init.local/data-out-gsms".*
    autostart_remove "data-out-gsms"
fi



if [ -d "/etc/data-out-qscd" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-out-qscd/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing data-out-qscd modules. Please set up qscd-out instead.
EOF
    fi

    rm -rf "/etc/data-out-qscd" "/etc/init.local/data-out-qscd".*
    autostart_remove "data-out-qscd"
fi



if [ -d "/etc/data-out-win" ]
then
    # avoid worrying user if directory is empty
    exists="0"
    for t in "/etc/data-out-win/"*
    do
        if [ -e "${t}" ]
        then
            exists="1"
            break
        fi
    done

    if [ "${exists}" -ne "0" ]
    then
        show_gdi_upgrade_message
        cat <<EOF

 * Removing data-out-win modules. Please set up win-out instead.
EOF
    fi

    rm -rf "/etc/data-out-win" "/etc/init.local/data-out-win".*
    autostart_remove "data-out-win"
fi



############################
###  Disk space cleanup  ###
############################



rm -rf "/var/spool/data-mux-gcf"*
rm -rf "/var/spool/data-journaller"*



# vim: ts=4:sw=4:expandtab
