last fixes
[iso2ps2.git] / iso2ps2
CommitLineData
1de93811
ML
1#!/bin/bash
2
a8671a5d
ML
3PROGNAME="$(basename $0)"
4PWD=$(dirname $0)
5[ "$PWD" ] || PWD=$(pwd)
6PWD=$(cd $PWD; pwd)
7
1de93811 8# default values
a8671a5d
ML
9CACHE=$PWD/cache
10CONFIGS=$PWD/CONFIGS
4ed64574 11KLICENSE="e4e54fd67c16c316f47829a30484d843"
a8671a5d
ML
12PS2CLASSIC=$PWD/ps2classic/ps2classic
13PSNPACKAGE=$PWD/psn_package_npdrm/psn_package_npdrm
e7f7fd80
ML
14# HAN HEN
15MODEL="HEN"
a8671a5d
ML
16
17# formating functions
72730076 18function title () { echo -e "\033[0;1m=== $* ===\033[0;0m"; }
a8671a5d
ML
19function pass () { echo -e "\033[1;32m$*\033[0;0m"; }
20function warn () { echo -e "\033[1;33m$*\033[0;0m"; }
21function fail () { echo -e "\033[1;31m$*\033[0;0m"; exit 1; }
1de93811
ML
22
23# defaut values
24id=0
25
26# get last values
27[ -f .$PROGNAME ] && source .$PROGNAME
28
29# init
30file="$1"
e7f7fd80 31[ -f "$file" ] || fail "can't find file"
959bb95c 32file "$file" | grep -q UDF || fail "file is not an UDF iso"
1de93811
ML
33
34# get serial
35serial=$(isoinfo -f -i "$file" | \
36 grep "^/[A-Z][A-Z][A-Z][A-Z]_[0-9][0-9][0-9]\.[0-9][0-9];" | \
37 sed 's,[/.],,g;s,_,-,;s,;.*,,')
38[ "$serial" ] || { serial=$(printf "PS2U-1%04d" $id); let id++; }
1de93811
ML
39
40# store last value
41echo > .$PROGNAME
42echo "id=$id" >> .$PROGNAME
43
44# get name
45function getname () {
46 [ -f jlist2.html ] || wget https://psxdatacenter.com/psx2/jlist2.html
47 [ -f plist2.html ] || wget https://psxdatacenter.com/psx2/plist2.html
48 [ -f ulist2.html ] || wget https://psxdatacenter.com/psx2/ulist2.html
a8671a5d 49 awk '/>'$file'</ {
1de93811
ML
50 getline; gsub(/<[^<]*>/, "")
51 sub(/&nbsp;/, " ")
52 gsub(/^[ \t]*/, "")
53 gsub(/[ \t]*$/, "")
54 print
55 }' [jpu]list2.html
56}
57
58# get game title
c631faff
ML
59[ -f "$CACHE/PS2-GAMEID-TITLE-MASTER.csv" ] || ( cd "$CACHE"; wget https://raw.githubusercontent.com/VTSTech/PS2-OPL-CFG/master/test/PS2-GAMEID-TITLE-MASTER.csv )
60title=$(awk -F\; '"'$serial'" == $1 {print $2}' "$CACHE/PS2-GAMEID-TITLE-MASTER.csv")
1de93811
ML
61[ "$title" ] || title=$(basename "$file" | sed 's/ *(.*//')
62
a8671a5d
ML
63# create CID
64title "create CID"
65cid=2P0001-${serial/-}_00-$(echo ${title}0000111122223333 | \
66 tr '[a-z]' '[A-Z]' | \
67 sed 's/[^A-Z0-9]//g;s/\(................\).*$/\1/')
68
1de93811 69# main process
a8671a5d 70title "description"
1de93811
ML
71echo "File: '$file'"
72echo "Serial: $serial"
73echo "Title: '$title'"
a8671a5d 74echo "CID: $cid"
1de93811 75
a8671a5d
ML
76# install skeleton
77title "install skeleton"
1de93811 78mkdir $serial
c631faff 79tar -C $serial -xzf $PWD/skeleton.tgz
8ae7c948 80[ -d "$CACHE" ] || mkdir "$CACHE"
1de93811 81
1de93811
ML
82# images:
83# - ICON0.PNG 320x176
84# - PIC0.PNG 1000x560
85# - PIC1.PNG 1920x1080
86
a8671a5d
ML
87# get cover image
88title "get cover image"
8ae7c948
ML
89if [ -f "$CACHE/$serial-ICON0.PNG" ]; then
90 cp "$CACHE/$serial-ICON0.PNG" $serial/ICON0.PNG
a8671a5d
ML
91else
92 wget https://psxdatacenter.com/psx2/images2/covers/$serial.jpg
93 if [ -f $serial.jpg ]; then
94 convert $serial.jpg tmp.png
95 mogrify -resize 320x176 tmp.png
96 mogrify -background transparent -gravity center -extent 320x176 tmp.png
97 mv tmp.png $serial/ICON0.PNG
98 rm $serial.jpg
8ae7c948 99 cp $serial/ICON0.PNG "$CACHE/$serial-ICON0.PNG"
a8671a5d
ML
100 else
101 warn "can't find cover"
102 fi
1de93811
ML
103fi
104
a8671a5d
ML
105# get screenshot
106title "get screenshot"
8ae7c948
ML
107if [ -f "$CACHE/$serial-PIC0.PNG" ]; then
108 cp "$CACHE/$serial-PIC0.PNG" $serial/PIC0.PNG
a8671a5d
ML
109else
110 touch .touch
111 find $HOME/.config/PCSX2/snaps/ -type f | xargs rm .touch
112 pcsx2 "$file"
113 capture=$(find $HOME/.config/PCSX2/snaps/ -type f | sort)
114 if [ "$capture" ]; then
115 cp "$capture" tmp.png
116 mogrify -scale 1000x560 tmp.png
117 mogrify -background transparent -gravity center -extent 1000x560 tmp.png
118 mv tmp.png $serial/PIC0.PNG
8ae7c948 119 cp $serial/PIC0.PNG "$CACHE/$serial-PIC0.PNG"
a8671a5d
ML
120 else
121 warn "can't find screenshot"
122 fi
1de93811
ML
123fi
124
a8671a5d
ML
125# create PARAM.SFO
126title "create PARAM.SFO"
1de93811
ML
127# parameters
128# - file: PARAM.SFO
a8671a5d
ML
129# 0x120: title
130# 0x1a0: ${serial/-}
1de93811 131# - file: USRDIR/ISO.BIN.EDAT
a8671a5d
ML
132# 0x017: ${serial/-}
133echo -n $title | xxd | xxd -r -s 0x120 - $serial/PARAM.SFO
134echo -n ${serial/-} | xxd | xxd -r -s 0x1a0 - $serial/PARAM.SFO
135echo -n ${serial/-} | xxd | xxd -r -s 0x17- $serial/USRDIR/ISO.BIN.EDAT
1de93811 136
2484479b 137# generate key
a8671a5d 138title "generate key"
4ed64574 139dd bs=1 count=$(expr ${#KLICENSE} / 2) if=/dev/zero of=$serial/ps2.key
a8671a5d
ML
140echo "00000000 $KLICENSE" | xxd -r - $serial/ps2.key
141
142# find config file
143title "find config file"
e7f7fd80
ML
144if [ ! -f "$CACHE/$serial.CONFIG" ]; then
145 confname=$(echo $serial | sed 's/-/_/;s/_\(...\)/_\1./')
09459eff 146 case "$MODEL" in
e7f7fd80
ML
147 HAN)
148 confname=$confname.CONFIG
149 config=$(cd $CONFIGS; find NET GX SOFTCUSTOM -name $confname | head -1)
09459eff 150 ;;
e7f7fd80
ML
151 HEN)
152 confname=$confname.ENC
153 config=$(cd $CONFIGS; find ENC -name $confname | head -1)
09459eff
ML
154 ;;
155 esac
156 if [ "$config" ]; then
e7f7fd80 157 cp $CONFIGS/$config "$CACHE/$serial.CONFIG"
09459eff
ML
158 fi
159fi
e7f7fd80 160[ -f "$CACHE/$serial.CONFIG" ] && cp "$CACHE/$serial.CONFIG" $serial/USRDIR/CONFIG || warn "can't find config"
a8671a5d
ML
161
162# create package config
163title "create package config"
164cat > $serial/package.conf << EOF
165Content-ID = $cid
2484479b
ML
166k_licensee = 0x$KLICENSE
167DRM_Type = Free
a8671a5d 168Content_Type = PS2_Classics_Game
2484479b
ML
169PackageVersion = 01.01
170EOF
171
a8671a5d
ML
172# prepare iso
173title "prepare iso"
09459eff 174$PS2CLASSIC prepare "$file" || fail "can't prepare iso"
a8671a5d 175
2484479b 176# convert iso
a8671a5d 177title "convert iso"
09459eff 178$PS2CLASSIC e cex $serial/ps2.key "$file" $serial/USRDIR/ISO.BIN.ENC ISO.BIN.ENC $cid || fail "can't convert iso"
2484479b
ML
179
180# create package
a8671a5d 181title "create package"
e7f7fd80 182$PSNPACKAGE $serial || fail "can't create package"
959bb95c 183mv $serial/$cid.pkg .
a8671a5d 184
c631faff
ML
185# create rap file
186title "create rap file"
187cp $PWD/iso2ps2.rap $cid.rap
188
a8671a5d 189# cleaning
09459eff
ML
190title "cleaning"
191rm -rf $serial