#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

# download of kernel files is assumed to save time
# delete any stale man pages if exist
rm -rf /usr/local/share/man
export CFLAGS="-mtune=generic -Os -pipe"
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe "

K=/usr/src/linux-4.19.10
BUILD=/lib/modules/4.19.10-tinycore64/build
CONF=config-4.19.10-tinycore64
SYM=Module.symvers-4.19.10-tinycore64
P=OSS
V=v4.2-build2019
SRC=oss-$V-src-gpl
PD=$SRC/os_cmd/Linux/ossdetect
DIR=/tmp/$SRC
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc  gtk2-dev xz bc bash advcomp libvorbis-dev \
adwaita-icon-theme clang "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done
###########################################
# kernel prep
###########################################
xz -d linux-4.19.10-patched.txz && tar xvf linux-4.19.10-patched.tar
rm -rf *tar
mkdir /usr/src
mv linux-4.19.10  /usr/src
gunzip $SYM.gz
cd $K
make mrproper
cp /tmp/$CONF  .config
make oldconfig
make prepare
make modules_prepare
cp /tmp/$SYM Module.symvers
make SUBDIRS=scripts/mod
ln -s /usr/src/linux-4.19.10/ $BUILD
cd /tmp
############################################################

su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://www.opensound.com/developer/sources/stable/gpl/$SRC.tar.bz2  \
https://www.dropbox.com/s/hg4e4ysqmqorqka/ossdetect.c?dl=0" $USER 

tar jxvf oss*2
###################################################################
# due to this make error.....I modified ossdetect.c due to internet research
# ossdetect.c:(.text+0xe0f): undefined reference to `makedev'
# the file below does not download properly by script....had to press enter
# DL=http://ossnext.trueinstruments.com/forum/download
# su -c "/usr/local/bin/wget -nc --no-check-certificate \
# $DL/file.php?id=195&sid=b08ce12f260c6d4807de3c8c70806bbd " $USER 
# hence the dropbox file you can diff them to see only one line added as below echo command
#################################################################
mv $PD/ossdetect.c $PD/ossdetect.c-raw
mv oss*0 $PD/ossdetect.c
cat $PD/ossdetect.c | grep "#include <sys/sysmacros.h>"
echo '^^^  is the line I added as per ossnext site'
cd $SRC
mkdir build && cd build
$DIR/configure --config-midi=YES --config-vmix=NO --enable-libsalsa=NO 
make build  # takes one minute
make install # takes one minute
cd /tmp
##### Warning: No libsalsa library compiled
##### OSS build environment set up for REGPARM kernels

# OSS-mixer
##############
mkdir -p $P-mixer/usr/bin
mv /usr/bin/ossxmix $P-mixer/usr/bin
mkdir -p $P-mixer/usr/local/share/pixmaps
LOOP=/tmp/tcloop/adwaita-icon-theme/usr/local/share/icons/Adwaita/48x48/apps/multimedia-volume-control-symbolic.symbolic.png
cp $LOOP $P-mixer/usr/local/share/pixmaps/ossxmix.png

APP=$P-mixer/usr/local/share/applications
mkdir -p $APP
echo '[Desktop Entry]
Name=OSS-mixer
Exec=/usr/bin/ossxmix
Type=Application
X-FullPathIcon=/usr/local/share/pixmaps/ossxmix.png
Icon=ossxmix
Categories=System; ' > $APP/$P-mixer.desktop

# modules --no stripping
# module dir oss is lowercase
######################
mkdir -p $P-modules-4.19.10-tinycore64/lib/modules/4.19.10-tinycore64/kernel
mv /lib/modules/4.19.10-tinycore64/kernel/oss $P-modules-4.19.10-tinycore64/lib/modules/4.19.10-tinycore64/kernel

# main
######
mkdir -p $P/usr/bin $P/usr/sbin $P/etc $P/usr/lib $P/usr/local/share/$P

mv /usr/bin/oss* $P/usr/bin
mv /usr/sbin/oss* $P/usr/sbin
mv /usr/sbin/savemixer $P/usr/sbin
mv /usr/sbin/soundon $P/usr/sbin
mv /usr/sbin/soundoff $P/usr/sbin
mv /usr/sbin/vmixctl $P/usr/sbin
mv /usr/lib/oss  $P/usr/lib
mv /etc/oss.conf $P/etc/
# shared objects not stripped
strip --strip-unneeded  $P/usr/lib/oss/lib/*so

# configs need to be writeable @ usr/lib/oss/conf
mv $P/usr/lib/oss/conf $P/usr/local/share/$P/

# system install fixes to NOT save my devices
#################################
cd $P/usr/lib/oss/etc
rm -rf installed_drivers legacy_devices userdefs
cd /tmp

# create sym links later
rm -rf $P/usr/lib/oss/modules
rm -rf $P/usr/lib/oss/objects

#########
# tce.install  
#########
mkdir -p $P/usr/local/tce.installed

echo '#!/bin/sh
# create missing sym links 
LOOP1=/tmp/tcloop/OSS/usr/lib/oss
[  -h /usr/lib/oss/modules ] ||  ln -s $LOOP1/modules.regparm /usr/lib/oss/modules
[  -h /usr/lib/oss/objects ] ||  ln -s $LOOP1/objects.regparm /usr/lib/oss/objects
# make configs writeable then start OSS
LOOP2=/tmp/tcloop/OSS/usr/local/share/OSS/conf
[ -d /usr/lib/oss/conf ] || (mkdir /usr/lib/oss/conf && cp $LOOP2/* /usr/lib/oss/conf/)
depmod -a
/usr/sbin/soundon 
echo " Please read the info file if you need to change any configs"   
'   > $P/usr/local/tce.installed/$P 

chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
cp $P/usr/lib/oss/sysfiles.list $P-doc/usr/local/share/doc/$P/
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P/
mv $SRC/tutorials $P-doc/usr/local/share/doc/$P/
mv /usr/share/man $P-doc/usr/local/share

# no dev TCE use script
#########################
# TCZ them
##########
LIST="$P $P-doc  $P-mixer "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
        find etc -not -type d >> /tmp/$Z.tcz.list
        sed 's|etc|/etc|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

LIST="$P-modules-4.19.10-tinycore64 "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find lib -not -type d > /tmp/$Z.tcz.list
        sed 's|lib|/lib|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          OSS.tcz
Description:    Open Sound System
Version:        v4.2-build2019
Author:         4front-tech.com
Original-site:  http://www.4front-tech.com/oss.html
Copying-policy: GPLv2
Size:           1.6M		
Extension_by:   aus9 at gmx dot com
Tags:           sound OSS
Comments:       Sound with no support for Alsa nor pulseaudio
                Unable to test on my new hardware. Refer to the wiki
                Install script will start OSS then run $ossinfo to see if 
                any audio devices are found.  (=sound out devices)

                If you load any usb device, after loading TCE then restart OSS by
                $ sudo soundoff && sudo soundon

                To see which devices are supported see devices.list 
             
                Optional dep is GUI mixer called OSS-mixer.tcz
                ###########  ossmix or ossxmix may show sound muted by default
                libvorbis.tcz might offer ogg support but unable to test

                Persistence files use control panel to add any that you need please
                #############
                1) if you run $sudo savemixer ---file to backup will be 
                           usr/lib/oss/etc/mixer.state
                2) For Dir=usr/lib/oss/etc save 3 files if found they are:
                     installed_drivers legacy_devices userdefs
                3) If you change any config under usr/lib/oss/etc/conf/
                        backup the entire dir please, due to the my poor 
                        design of tce.installed which re-creates entire dir.
                 So your backup would be usr/lib/oss/etc/conf

                  Compiled for 10x  Changelog combined 32/64 bit             
Change-log:     2008/07/06 Original for 2.6.24.7-dsl  (Curaga until TC7)
                2008/07/23 Updated for 2.6.26-tinycore, added menu items
                2008/07/27 Updated menu items for the new /tmp/tcz.menu
                2008/10/10 Updated menu items to JWM format
                2009/04/11 Update to 4.1 for 2.6.29.1-tinycore, gtk2 mixer
                2009/12/17 Updated startup script to handle non-standard module location
                2010/01/25 Added /etc/asound.conf, separated gtk2 mixer
                2010/03/25 Updated config files writable
                2010/05/12 Update to 4.2 for TC 3, modules separated
                2010/05/19 Made usr/lib/oss/etc writable
                2011/09/09 Update to 4.2 b2005 for TC 4
                2016/12/29 Updated for TC 7 ( Coreplayer2)
Current:        2019/03/07 Updated for TC 10 - need root powers to write to usr/lib/oss/etc/conf (aus9)  ' > $P.tcz.info

echo 'Title:          OSS-modules-4.19.10-tinycore64.tcz
Description:    OSS kernel modules  
Version:        v4.2-build2019
Author:         4front-tech.com
Original-site:  http://www.4front-tech.com/oss.html
Copying-policy: GPLv2
Size:           952K		
Extension_by:   aus9 at gmx dot com
Tags:           sound OSS
Comments:       OSS modules for 10x kernel
                Previous maintainers Curaga, coreplayer2, gordon64
                for earlier Kernel versions
                
                Compiled for 10x 64 bit combined 32/64 changelog              
Change-log:     2010/05/12 modules separated from OSS/oss (Curaga)
                2011/09/09 Update for TC 4 (Curaga)
                2015/09/20 Update for TC 6 (gordon64)
                2016/12/29 Updated for TC 7 ( Coreplayer2)
Current:        2019/03/07 Updated for TC 10 (aus9) ' > $P-modules-4.19.10-tinycore64.tcz.info

echo 'Title:          OSS-doc.tcz
Description:    OSS docs
Version:        v4.2-build2019
Author:         4front-tech.com
Original-site:  http://www.4front-tech.com/oss.html
Copying-policy: GPLv2
Size:           244K		
Extension_by:   aus9 at gmx dot com
Tags:           sound OSS
Comments:       Man pages and docs

                Compiled for 10x              
Change-log:     2015/09/20 TC 6  (gordon64)
Current:        2019/03/07 Updated for TC 10 (aus9) ' > $P-doc.tcz.info

echo 'Title:          OSS-mixer.tcz
Description:    gtk2 GUI mixer
Version:        v4.2-build2019
Author:         4front-tech.com
Original-site:  http://www.4front-tech.com/oss.html
Copying-policy: GPLv2
Size:           28K		
Extension_by:   aus9 at gmx dot com
Tags:           sound OSS mixer
Comments:       OSS contains a terminal mixer=ossmix
                           This App is for the Gtk2 mixer for OSS
                           =ossxmix 

                Compiled for 10x  combined 32/64 bit changelog           
Change-log:     2010/01/25 separated gtk2 mixer from OSS/oss (Curaga)
                2010/05/12 Update for TC 3 (Curaga) 
                2011/09/09 Update for TC 4 (Curaga)
                2016/12/29 Update for TC 7 ( Coreplayer2)
Current:        2019/03/07 Update for TC 10 (aus9) ' > OSS-mixer.tcz.info

echo 'OSS.tcz
gtk2.tcz' > $P-mixer.tcz.dep
echo 'OSS-modules-4.19.10-tinycore64.tcz  ' > $P.tcz.dep
echo 'man-db.tcz' > $P-doc.tcz.dep

submitqc --libs

# ignore all warning about copyright as its gpl v2 and kernel mods not stripped