#!/usr/bin/sh

OLD_LOCATION="/var/spool/abrt"
NEW_LOCATION="/var/tmp/abrt"

systemctl status abrtd.service >/dev/null && {
    echo "Please, stop abrtd.service before continue."
    exit 1
}

test -d "$OLD_LOCATION" || {
    echo "Nothing to move. Old dump location doesn't exist: $OLD_LOCATION"
    exit 0
}

echo "Ensure existence of the new dump location"
# recreate new location with correct SELinux context
mkdir -p "$NEW_LOCATION"
restorecon "$NEW_LOCATION"

echo "Moving all files from the old location to the new one"
# move all files from OLD_LOCATION to NEW_LOCATION
find "$OLD_LOCATION" -maxdepth 1 -type f -exec mv '{}' "$NEW_LOCATION" \;

# move dump all directories
for DD in `find $OLD_LOCATION -maxdepth 1 -type d`; do
    # skip dump location self
    if [ "$DD" == "$OLD_LOCATION" ]; then
        echo "Not moving'$DD': it is the old dump location"
        continue
    fi

    # skip symlinks
    if [ -L $DD ]; then
        echo "Not moving '$DD': it is a symbolic link"
        continue
    fi

    # check if time element exists, if so then consider a directory as dump
    # directory and move it to the new location
    if [ -f $DD/time ]; then
        NEW_DD=$NEW_LOCATION/`basename $DD`

        # to be sure we do not override anything
        if [ -d "$NEW_DD" ]; then
            echo "Not moving '$DD': it already exists here '$NEW_DD'"
            continue
        fi

        echo "Moving: '$DD' to '$NEW_DD'"
        if mv -- "$DD" "$NEW_DD"; then
            # owner of dump dir is identified by his group
            # so get the owner of dump dir
            OWNER_GID=`stat -c "%g" "$DD"`
            
            # use group's user in case where group contains only single user
            if [ $(getent passwd | cut -f3,4,5 -d: | grep -c ":$OWNER_GID:") -eq 1 ]; then
                OWNER_UID=`getent passwd | cut -f3,4,5 -d: | grep ":$OWNER_GID:" | cut -f1 -d:`
            else
                echo "Can't use group($OWNER_GID) as the owner. The group has more than one user. Using the user from dump dir."
                # otherwise get owner from dd's uid element
                OWNER_UID=`cat $NEW_DD/uid`
            fi

            getent passwd $OWNER_UID >/dev/null || {
                echo "Can't use UID($OWNER_UID). The user doesn't exist. Using root as the owner."
                # if user doesn't exist pass the ownership to root
                OWNER_UID=0
            }

            # set new schema of ownership
            chown --recursive $OWNER_UID:abrt "$NEW_DD" || {
                echo "Can't change ownership of '$NEW_DD' and it's files. Exiting"
                exit 1
            }

            chmod 770 "$NEW_DD" || {
                echo "Can't change mode of '$NEW_DD'. Exiting"
                exit 1
            }

            cd $NEW_DD && {
                chmod 660 * || {
                    echo "Can't change mode of files in '$NEW_DD'. Exiting"
                    exit 1
                }
            }
        fi
    else
        echo "Not moving '$DD': it is not a dump directory (time element doesn't exist)"
    fi
done

echo "Job successfully finished. You can safely delete '$OLD_LOCATION'"
