#!/bin/sh

#
# Davide Airaghi
# 
# Skype4Slackware
#
# "Skype for Linux" Slackware package builder
#
#

TAR=$(which tar 2>/dev/null)
WGET=$(which wget 2>/dev/null)
AR=$(which ar 2>/dev/null)
MAKEPKG=$(which makepkg 2>/dev/null)

DOWNLOAD="https://repo.skype.com/latest/skypeforlinux-64.deb"
FILE="linux.deb"
DIR="skypeforlinux"

ARCH=$( arch 2>/dev/null || uname -m )

PGKNAME=""

echo "Skype4Slackware ..."

if [ "$MAKEPKG" = "" ]; then
	echo "program 'makepkg' not found"
	exit 2
fi

if [ "$TAR" = "" ]; then
	echo "program 'tar' not found"
	exit 2
fi

if [ "$WGET" = "" ]; then
	echo "program 'wget' not found"
	exit 3
fi

if [ "$AR" = "" ]; then
	echo "program 'ar' not found"
	exit 4
fi

case "$ARCH" in

	x86_64)
		echo "Architecture '$ARCH' is ok"
		;;
	*)
		echo "Architecture '$ARCH' is not supported"
		exit 5
		;;

esac


cd /tmp

[ -e $FILE ] && rm -f $FILE
$WGET --quiet $DOWNLOAD -O $FILE

DATAFILE=""
$AR x $FILE data.tar.gz 2>/dev/null >/dev/null 
if [ -e data.tar.gz ]; then
	DATAFILE="data.tar.gz"
else
	$AR x $FILE data.tar.xz 2>/dev/null >/dev/null 
	if [ -e data.tar.xz ]; then
		DATAFILE="data.tar.xz"
	fi
fi

if [ "$DATAFILE" = "" ]; then
	echo "Uname to extract data.tar.gz or data.tar.xz"
	exit 6
fi

[ -e $DIR ] && rm -rf $DIR
mkdir $DIR

cd $DIR

tar xf ../$DATAFILE
mkdir -p usr/share/doc/skypeforlinux
mkdir -p install/

echo "             |-----handy-ruler------------------------------------------------------|" > install/slack-desc
echo "skypeforlinux: SkypeForLinux (Free Internet telephony)" >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc     
echo "skypeforlinux: Skype is a peer-to-peer Internet telephony network. It competes" >> install/slack-desc
echo "skypeforlinux: against existing open VoIP protocols such as SIP, IAX, and H.323." >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc
echo "skypeforlinux: https://www.skype.com/download-skype/skype-for-computer/" >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc
echo "skypeforlinux: " >> install/slack-desc


VER=$(cat usr/share/skypeforlinux/version | sed -es/^v// )
PKGNAME="skypeforlinux-${VER}-x86_64-1airaghi.txz"

$MAKEPKG --linkadd y --chown y /tmp/$PKGNAME 

cd /tmp

[ -e $DATAFILE ] && rm -f $DATAFILE 2>/dev/null >/dev/null
[ -e $FILE ] && fm -f $FILE 2>/dev/null >/dev/null
[ -e $DIR ] && rm -rf $DIR 2>/dev/null >/dev/null

exit 0

