#!/bin/sh

AddPackages()
{
PKGDIR="/usr/share/siduction-scripts/packages"
PKGSRC_LOCAL="/usr/share/siduction-scripts/clone"
PKGSRC_REMOTE="https://github.com/siduction/pyfll.git"

if [ -e /etc/default/distro ]; then
    . /etc/default/distro
fi

settings_version=$(echo $FLL_WALLPAPER | cut -d / -f 5)

xorg_installed=$(dpkg -l | grep Xorg)

if [ -z "${xorg_installed}" ]; then
    dest_platform=nox
    else
    dest_platform=xorg
fi

case $1 in
        cinnamon)
        PACK_PROFILE=cinnamon
        ;;
        gnome)
        PACK_PROFILE=gnome
        ;;
        kde)
        PACK_PROFILE=kde
        ;;
        plasma5)
        PACK_PROFILE=plasma5
        ;;
        lxde)
        PACK_PROFILE=lxde
        ;;
        lxqt)
        PACK_PROFILE=lxqt
        ;;
        mate)
        PACK_PROFILE=mate
        ;;
        siducation-full)
        PACK_PROFILE=siducation-full
        ;;
        siducation-lite)
        PACK_PROFILE=siducation-lite
        ;;
        xfce)
        PACK_PROFILE=xfce
        ;;
        custom)
        PACK_PROFILE=custom
        ;;
        *)
        ;;
esac

if [ "$PACK_PROFILE" = "custom" ]; then
    if [ ! -f ${PKGDIR}/custom ] ; then
        echo '######################################################'
        echo 'WARNING custom pkglist file not found!'
        echo 'Please add file /usr/share/siduction-scripts/custom.'
        echo 'Please use as template /usr/share/siduction-scripts/custom.ex file.'
        echo "WARNING don't add inexistent packages names to don't get in trouble."
        echo 'Thank you !'
        echo '######################################################'
        exit 1
    else
	# moves custom pkg list to custom-pklist.orig and show a message about it
	echo '######################################################'
	echo "Cpying custom pkglist  to custom.orig"
	echo "If you don't need it anymore you should remove it."
	echo '######################################################'
	awk '/^packages/,/^"""/' ${PKGDIR}/${PACK_PROFILE}  | grep -v '"""' | grep -v '#' > ${PKGDIR}/${PACK_PROFILE}-pkglist
	cp ${PKGDIR}/$PACK_PROFILE ${PKGDIR}/${PACK_PROFILE}.orig
    fi
else

# TODO settings
#case ${settings_version} in
#     december)
#     echo "        siduction-settings-${PACK_PROFILE}-${settings_version}" > ${PKGDIR}/${PACK_PROFILE}-pkglist
#     echo "        siduction-art-${PACK_PROFILE}-${settings_version}" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
#     ;;
#     indiansummer)
#     echo "        siduction-settings-${PACK_PROFILE}-${settings_version}" > ${PKGDIR}/${PACK_PROFILE}-pkglist
#     echo "        siduction-art-${PACK_PROFILE}-${settings_version}" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
#     echo "        siduction-art-wallpaper-${settings_version}" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
#     ;;
#     paintitblack)
#     echo "        siduction-settings-${PACK_PROFILE}-paintitblack" > ${PKGDIR}/${PACK_PROFILE}-pkglist
#      echo "        siduction-art-${PACK_PROFILE}-paintitblack" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
#     echo "        siduction-art-wallpaper-paintitblack" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
#     ;;
#     *)
#     echo " There aren't supported settings for ${PACK_PROFILE} until now"
#     ;;
#esac

    # clone pyfll git and get requested PACK_PROFILE or show a message for custom pkg list
    echo "\nCloning pyfll."
    git clone "${PKGSRC_REMOTE}" "${PKGSRC_LOCAL}"
    sleep 2
    echo "\nFetching siduction ${PACK_PROFILE}  profile list."
    cp "${PKGSRC_LOCAL}/pyfll/packages/${PACK_PROFILE}" "${PKGDIR}/${PACK_PROFILE}"

    # Reads packages from packages profile
    awk '/^packages/,/^"""/' ${PKGDIR}/${PACK_PROFILE}  | grep -v '"""' | grep -v '#' >> ${PKGDIR}/${PACK_PROFILE}-pkglist

    # Search main file package for include dependecies
    # and build an depends file ( depends )
    awk '/^deps/,/^"""/' ${PKGDIR}/${PACK_PROFILE} | grep -v '"""' | grep -v '#' > ${PKGDIR}/${PACK_PROFILE}-deps

    # Search if we have NOX or XORG 
    
   case ${dest_platform} in
        xorg)
        cat ${PKGDIR}/${PACK_PROFILE}-deps | grep -v 200 | grep -v 100 | grep -v 300 |grep -v 500 >  ${PKGDIR}/${PACK_PROFILE}-depends
        ;;
        nox)
        cat ${PKGDIR}/${PACK_PROFILE}-deps | grep -v 200 | grep -v 100 | grep -v 300 | grep -v live | grep -v fll | grep -v installer >  ${PKGDIR}/${PACK_PROFILE}-depends
        echo "        siduction-settings-lightdm-gtk-${settings_version}" >> ${PKGDIR}/${PACK_PROFILE}-pkglist
        ;;
        *)
        echo "You can use addpkg only over on installed NOX or XORG siduction."
        ;;
    esac

    while read pkgdepends ; do
        echo "Fetching for ${PACK_PROFILE} profile $pkgdepends depend." 
        cp "${PKGSRC_LOCAL}/pyfll/packages/packages.d/$pkgdepends" "${PKGDIR}/$pkgdepends-depfiles" 
    done < ${PKGDIR}/${PACK_PROFILE}-depends 

    for i in ${PKGDIR}/*-depfiles; do
     awk '/^packages/,/^"""/' ${i} | grep -v '"""' | grep -v '#'  >> ${PKGDIR}/${PACK_PROFILE}-pkglist
    done


    # Removes temporary files
    if [ -f ${PKGDIR}/${PACK_PROFILE}-pkglist ] ; then
        rm -f ${PKGDIR}/*-dep*
        rm -f ${PKGDIR}/${PACK_PROFILE}
    fi

 
fi


# install files
apt-get install $(cat ${PKGDIR}/${PACK_PROFILE}-pkglist)

# purge meta files
# originally a hard-coded list: siduction-meta-xorg siduction-meta-xsiduction siduction-meta-xdebian siduction-meta-mediabase
purgelist="$(dpkg -l | awk '/siduction-meta/ {print $2}')"
if [ -z "${purgelist}" ] ; then
    printf "%s\n" "Nothing to purge!"
else
    printf "%s\n" "Now purging meta packages: ${purgelist}"
    apt-get purge --assume-yes "${purgelist}"
fi

# removes built package list and pyfll git clone
rm -f /usr/share/siduction-scripts/packages/${PACK_PROFILE}-pkglist
rm -Rf "${PKGSRC_LOCAL}"

# Settings message
if [ "$PACK_PROFILE" = "custom" ]; then
    echo "You should customize your's new added flavour."
else
    echo "\nYour installed settings for existing flavours are ${settings_version}."
    echo "Please add your desired settings for ${PACK_PROFILE} if exists in siduction repos. \n"
    echo "Otherwise you can try siduction-settings-${PACK_PROFILE}-paintitblack
    siduction-art-${PACK_PROFILE}-paintitblack and siduction-art-wallpaper-paintitblack. \n"
    sleep 2
    fi
}

case "$1" in
    cinnamon|custom|gnome|kde|plasma5|lxde|lxqt|mate|siducation-full|siducation-lite|xfce)
    AddPackages $1
    ;;
    *)
        echo "Usage: addpkg {cinnamon|custom|gnome|kde|plasma5|lxde|lxqt|mate|rqt|siducation-full|siducation-lite|xfce}"
        exit 1
    ;;
esac
