#!/bin/sh

me="[$(printf $0 | xargs basename)]"
# This file has priority over the ordinary template and is used
# for the developer-only packages to override the password
PRIO_SHADOW_TEMPLATE=/opt/userdata/devicestack/shadow.template

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

#$1 template file
#$2 result file
copy_template()
{
  TEMPLATE_FILE=$1
  RESULT_FILE=$2
  
  if [ "${TEMPLATE_FILE}" != "" ] && [ "${RESULT_FILE}" != "" ]; then
    printf "${me} Checking ${RESULT_FILE} \\n"
    if [ -f "${RESULT_FILE}" ] || [ -d "${RESULT_FILE}" ]; then
      printf "${me} ${RESULT_FILE} found.\\n"
    else
      printf "${me} ${RESULT_FILE} not found.\\n"
      if [ -e "${TEMPLATE_FILE}" ]; then
        printf "${me} Copying ${TEMPLATE_FILE} \\n"
        if ! cp -a  "${TEMPLATE_FILE}" "${RESULT_FILE}"; then
          printf "${me} Copying ${TEMPLATE_FILE} failed\\n"
        fi
      else
        printf "${me} Templatefile: ${TEMPLATE_FILE} not found.\\n"
      fi
    fi
  fi
}

case "$1" in
  start)
    # Parse kernel commandline options.
    #
    root=""
    for o in $(cat /proc/cmdline); do
      case $o in
        root=*)
        root=${o#root=}
        ;;
      esac
    done

    if ! [ "${root}" = "/dev/ram0" ]; then
      printf "${me} Restoring shadow from template ... \\n"
      SHADOWLINK=$(readlink /etc/shadow)
      printf "${me} Creating folder ${SHADOWLINK} \\n"
      mkdir -p "$(dirname ${SHADOWLINK})"
      if [ -f "${PRIO_SHADOW_TEMPLATE}" ]; then
        install -m 600 "${PRIO_SHADOW_TEMPLATE}" "${SHADOWLINK}"
        printf "${me} install from userdata \\n"
      else
        install -m 600 "${SHADOW_TEMPLATE}" "${SHADOWLINK}"
        printf "${me} install from system \\n"
      fi
      printf "${me} Restoring shadow from template done \\n"

      printf "${me} Checking existence of default device user \\n"
      if [ -f "${DEFAULT_DEVICE_USER_FILE}" ]; then
        printf "${me} file: ${DEFAULT_DEVICE_USER_FILE} found\\n"
      else
        printf "${me} file: ${DEFAULT_DEVICE_USER_FILE} not found\\n"
        mkdir -p "${DEVICE_USER_DIR}"

        # If device has initial device password
        if [ -f /opt/extparam/fddp ]; then
          printf "${me} Creating device user with initial device password \\n"
          install -m 600 /dev/null "${DEFAULT_DEVICE_USER_FILE}"
          printf "pwd:" > "${DEFAULT_DEVICE_USER_FILE}"
          IGPW=$(cat /opt/extparam/fddp)
          if ! /opt/gira/bin/encode-pw.sh "${IGPW}" >> "${DEFAULT_DEVICE_USER_FILE}"; then
             printf "${me} encode-pw.sh failed.\\n"
          fi
        else
          printf "${me} Creating default device user \\n"
          if ! printf "pwd:2PAf6byL4EDpvcB167lea4ihXzZUk8anCJYAY5sfWTEmvFgpWAM5F61XbEsihZ+6Q" > "${DEFAULT_DEVICE_USER_FILE}"; then
            printf "${me} Creating default device user failed \\n"
          fi
        fi
      fi
      copy_template ${APPCONFIG_FILE_TEMPLATE} ${APPCONFIG_FILE}    
      copy_template ${DSCONFIG_FILE_TEMPLATE} ${DSCONFIG_FILE}
      copy_template ${COSTOM_CHANNEL_DIR_TEMPLATE} ${COSTOM_CHANNEL_DIR}
      copy_template ${CUSTOM_PROJECT_DEFINITION_DIR_TEMPLATE} ${CUSTOM_PROJECT_DEFINITION_DIR}
      copy_template ${OPENVPN_TEMPLATE_DIR} ${OPENVPN_CONFIG_DIR}
    else
     printf "${me} we have /dev/ram0 as root device we are in commissioning mode.\\n"
    fi
    ;;
  stop)
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

exit 0
