#! /bin/sh
# ==============================================================================
# Script to migrate configuration from flnews major versions 0 to 1
#
# Copyright (c) 2021 by the developers. See the LICENSE file for details.

# Program name
CFG_NAME="flnews"

# Nodename (strip potential domain part and whitespace)
export CFG_NODENAME=$(uname -n | sed 's/\..*//' | sed 's/ //g')

# Abort on error
set -u
set -e

# Headline
printf "\n%s\n\n" "Migrate configuration from $CFG_NAME major versions 0 to 1"

# Check POSIX.2 version
printf "%s" "Check for POSIX.2 version ... "
# Use POSIX2_VERSION with no leading underscore, to support old systems too
POSIX2=$(getconf POSIX2_VERSION)
printf "%s\n" "$POSIX2"
if test $POSIX2 -lt 1
then
   printf "%s\n" "Error: POSIX.2 utilities are required"
   exit 1
fi

# Change directory (awk scripts are used without path)
CFG_WORKDIR=$(dirname "$0")
printf "%s%s\n" "Working directory ... " "$CFG_WORKDIR"
cd "$CFG_WORKDIR"

# Print CFG_NODENAME
printf "%s%s\n" "Nodename of machine ... " "$CFG_NODENAME"

# Check for HOME variable
printf "%s" "Check for variable HOME ... "
set +u
TMP=$HOME
set -u
if test "x" = "x$TMP"
then
   printf "\n"
   printf "%s\n" "Error: Variable HOME is unset or empty"
   exit 1
else
   printf "%s\n" "$HOME"
fi

# Old configuration directory
DIR_OLD="$HOME/.$CFG_NAME"

# New configuration directory
printf "%s" "Check for variable XDG_CONFIG_HOME ... "
set +u
TMP=$XDG_CONFIG_HOME
set -u
if test "x" = "x$TMP"
then
   printf "\n"
   XDG_CONFIG_HOME="$HOME/.config"
else
   printf "%s\n" "$XDG_CONFIG_HOME"
fi
if ! test -d "$XDG_CONFIG_HOME"
then
   printf "%s\n" "Create path for new configuration directory"
   mkdir -p "$XDG_CONFIG_HOME"
fi
DIR_NEW="$XDG_CONFIG_HOME/$CFG_NAME"

# Check whether old configuration directory exists
printf "%s\n" "Old configuration directory: $DIR_OLD"
if ! test -d "$DIR_OLD"
then
   printf "%s\n" "Error: Old configuration directory not found"
   exit 1
fi
# Check whether new configuration directory already exists
printf "%s\n" "New configuration directory: $DIR_NEW"
if test -d "$DIR_NEW"
then
   printf "%s\n" "Error: New configuration directory already exists"
   printf "%s\n" "(remove it manually to run this script again)"
   exit 1
fi

# Copy old configuration to new location
printf "%s\n" "Copy old configuration to new location ..."
if test $POSIX2 -lt 200112
then
   # The -P option is not defined by older versions of POSIX.2
   cp -R -p "$DIR_OLD" "$DIR_NEW"
else
   # Do not follow symbolic links with -P option
   cp -R -P -p "$DIR_OLD" "$DIR_NEW"
fi

# Convert configfile semantics
printf "%s\n" "Convert configfile semantics ..."
awk -f config.awk "$DIR_NEW/configfile" >"$DIR_NEW/configfile.new"
mv -f "$DIR_NEW/configfile.new" "$DIR_NEW/configfile"

# Convert scorefile syntax
printf "%s\n" "Convert scorefile syntax ..."
awk -f score.awk "$DIR_NEW/scorefile" >"$DIR_NEW/scorefile.new"
mv -f "$DIR_NEW/scorefile.new" "$DIR_NEW/scorefile"

# Check for "scorerc" entry in configfile
TMP=$(awk '/^scorerc:[ ].+/ { print "Found" ; exit }' "$DIR_NEW/configfile" \
      2>/dev/null)
if test "xFound" = "x$TMP"
then
   # Print note because scorefile syntax has changed
   printf "%s\n" "================================================"
   printf "%s\n" "Attention: Change \"scorerc\" entry in configfile!"
   printf "%s\n" "================================================"
fi

# Success
printf "%s\n" "Operation successfully completed"
exit 0


# EOF
