#!/bin/bash

currentScriptPath=$(pwd)

help="${0} : here are all the options (which can be used like this : \"-c\" for \"--channel\") and their default value :
${0} --group group --team laundry --project revolution --distribution photomaton --channel prod --basefolder /home/..../dependencies"

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in    
    -d|--distribution)
    distribution="$2"
    shift
    shift
    ;;
    -t|--team)
    team="$2"
    shift
    shift
    ;;
    -p|--project)
    project="$2"
    shift
    shift
    ;;
    -g|--group)
    group="$2"
    shift
    shift
    ;;
    -f|--basefolder)
    basefolder="$2"
    shift
    shift
    ;;
    -c|--channel)
    channel="$2"
    shift
    shift
    ;;
    -h|--help)
    echo "${help}"
    exit 0
    shift
    shift
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done

set -- "${POSITIONAL[@]}" # restore positional parameters

if [[ -z $channel ]] ; then
    echo "Missing channel: "
    echo "${help}"
    exit 0
fi

if [[ -z $basefolder ]] ; then
    pwd=$("pwd")
    cd ../../../../../dependencies
    newFolder=$("pwd")
    cd "${pwd}"
    basefolder="${newFolder}"
    echo "Using default basefolder value : ${basefolder}"
fi

if [[ -z $distribution ]] ; then
  distribution="photomaton"
  echo "Using default distribution value : ${distribution}"
fi

if [[ -z $team ]] ; then
  team="laundry"
  echo "Using default team value : ${team}"
fi

if [[ -z $project ]] ; then
  project="revolution"
  echo "Using default project value : ${project}"
fi

if [[ -z $group ]] ; then
  group="group"
  echo "Using default group value : ${group}"
fi

# FUNCTIONS ===========================================================

update_dependencies()
{
    aptlydependencies=$1
    dependencies=$2

    echo "# Save current configuration"

    config_line=$(cat "${dependencies}" | grep "kis-configuration")

    echo "removing ${dependencies}"
    rm "${dependencies}"

    # Read aptly_dependencies
    while IFS= read -r aptlyline; do

        package=$(echo $aptlyline | cut -d'=' -f1)
        packagefullversion=$(echo $aptlyline | cut -d'=' -f2)
        
        # if kis-configuration, replace with saved one
        if [[ $package == "kis-configuration" ]]; then
            echo "${config_line}" >> "${dependencies}"
        else
            echo "${aptlyline}" >> "${dependencies}"
        fi
    done < "${aptlydependencies}"

    # Remove last \n of the file to remove last empty line
    # sed -i -z '$ s/\n$//' $dependencies
}

# =====================================================================

project_folder="${basefolder}/${group}/${team}/${project}"
echo "Project folder ${project_folder}"
distribution_folder="${project_folder}/${distribution}"
echo "Distribution folder ${distribution_folder}"

channel_folder="${distribution_folder}/${channel}"
aptly_dependencies="${channel_folder}/aptly_dependencies"

echo "# Reference file : ${aptly_dependencies}"
echo "# Process each distribution"

distribution_list=$(ls -l "${project_folder}" | grep '^d' | awk -F' ' '{print $NF}')
for current_distr in $distribution_list; do

    current_distr__folder="${project_folder}/${current_distr}"

    # Ignore ref
    if [[ $current_distr != $distribution ]]; then

        echo "# Processing distribution ${current_distr}"

        channel_folder="${current_distr__folder}/${channel}"
        echo "channel folder ${channel_folder}"

        dependencies_file="${channel_folder}/aptly_dependencies"

        update_dependencies "${aptly_dependencies}" "${dependencies_file}"
    fi

done
