# https://ring.cx/en/download/gnu-linux

wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

APP=Ring
LOWERAPP=${APP,,}

sudo sh -c "echo 'deb http://nightly.apt.ring.cx/ubuntu_14.04/ ring main' > /etc/apt/sources.list.d/ring-nightly-man.list"
sudo apt-key adv --keyserver pgp.mit.edu --recv-keys A295D773307D25A33AE72F2F64CD5FA175348F84
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get -y install ring-gnome
sudo apt-get clean

sudo apt-get -y install $LOWERAPP

mkdir -p $APP/$APP.AppDir
cd $APP/$APP.AppDir
find /var/cache/apt/archives/*$LOWERAPP* -exec dpkg -x {} . \; || true
find /var/cache/apt/archives/*-data* -exec dpkg -x {} . \; || true

cp ./usr/share/applications/$LOWERAPP.desktop .
rm -rf ./usr/share/icons/48x48/apps || true
find ./usr/share/icons -path *256* -name $LOWERAPP.png -exec cp {} . \; || true
find ./usr/share/icons -path *512* -name $LOWERAPP.png -exec cp {} . \; || true

get_apprun

# Copy in the indirect dependencies
copy_deps

move_lib

delete_blacklisted

# patch_usr
# Patching only the executable files seems not to be enough for some apps
find usr/ -type f -exec sed -i -e "s|/usr|././|g" {} \;

VERSION=$(find /var/cache/apt/archives/ -name $LOWERAPP"_*" | head -n 1 | cut -d "~" -f 1 | cut -d "_" -f 2 | cut -d "-" -f 1 | sed -e 's|1%3a||g' )
echo $VERSION

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
